From 95b56600edafd59472b388a0620f9f3369fd0053 Mon Sep 17 00:00:00 2001 From: Matheus Deodato Date: Wed, 19 Feb 2025 14:28:45 -0300 Subject: [PATCH] fix(modem): handle nullptr in DTE constructors to prevent invalid access --- components/esp_modem/src/esp_modem_dte.cpp | 47 ++++++++++++++-------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/components/esp_modem/src/esp_modem_dte.cpp b/components/esp_modem/src/esp_modem_dte.cpp index 8345c6fe8b..0ca8604ba3 100644 --- a/components/esp_modem/src/esp_modem_dte.cpp +++ b/components/esp_modem/src/esp_modem_dte.cpp @@ -14,38 +14,53 @@ using namespace esp_modem; static const size_t dte_default_buffer_size = 1000; -DTE::DTE(const esp_modem_dte_config *config, std::unique_ptr terminal): - buffer(config->dte_buffer_size), - cmux_term(nullptr), primary_term(std::move(terminal)), secondary_term(primary_term), - mode(modem_mode::UNDEF) +DTE::DTE(const esp_modem_dte_config *config, std::unique_ptr terminal) + : buffer(config->dte_buffer_size), + cmux_term(nullptr), + primary_term(std::move(terminal)), + secondary_term(primary_term), + mode(modem_mode::UNDEF) { + ESP_MODEM_THROW_IF_FALSE(primary_term != nullptr, "Invalid argument: terminal cannot be null"); set_command_callbacks(); } -DTE::DTE(std::unique_ptr terminal): - buffer(dte_default_buffer_size), - cmux_term(nullptr), primary_term(std::move(terminal)), secondary_term(primary_term), - mode(modem_mode::UNDEF) +DTE::DTE(std::unique_ptr terminal) + : buffer(dte_default_buffer_size), + cmux_term(nullptr), + primary_term(std::move(terminal)), + secondary_term(primary_term), + mode(modem_mode::UNDEF) { + ESP_MODEM_THROW_IF_FALSE(primary_term != nullptr, "Invalid argument: terminal cannot be null"); set_command_callbacks(); } -DTE::DTE(const esp_modem_dte_config *config, std::unique_ptr t, std::unique_ptr s): - buffer(config->dte_buffer_size), - cmux_term(nullptr), primary_term(std::move(t)), secondary_term(std::move(s)), - mode(modem_mode::DUAL_MODE) +DTE::DTE(const esp_modem_dte_config *config, std::unique_ptr t, std::unique_ptr s) + : buffer(config->dte_buffer_size), + cmux_term(nullptr), + primary_term(std::move(t)), + secondary_term(std::move(s)), + mode(modem_mode::DUAL_MODE) { + ESP_MODEM_THROW_IF_FALSE(primary_term != nullptr, "Invalid argument: primary terminal cannot be null"); + ESP_MODEM_THROW_IF_FALSE(secondary_term != nullptr, "Invalid argument: secondary terminal cannot be null"); set_command_callbacks(); } -DTE::DTE(std::unique_ptr t, std::unique_ptr s): - buffer(dte_default_buffer_size), - cmux_term(nullptr), primary_term(std::move(t)), secondary_term(std::move(s)), - mode(modem_mode::DUAL_MODE) +DTE::DTE(std::unique_ptr t, std::unique_ptr s) + : buffer(dte_default_buffer_size), + cmux_term(nullptr), + primary_term(std::move(t)), + secondary_term(std::move(s)), + mode(modem_mode::DUAL_MODE) { + ESP_MODEM_THROW_IF_FALSE(primary_term != nullptr, "Invalid argument: primary terminal cannot be null"); + ESP_MODEM_THROW_IF_FALSE(secondary_term != nullptr, "Invalid argument: secondary terminal cannot be null"); set_command_callbacks(); } + void DTE::set_command_callbacks() { primary_term->set_read_cb([this](uint8_t *data, size_t len) {