Skip to content

Commit 37f84ee

Browse files
authored
Merge pull request #768 from deodatomatheus/fix/handle-nullptr-dte-constructors
fix(modem): handle nullptr in DTE constructors to prevent invalid access (IDFGH-14688)
2 parents a22c3da + 95b5660 commit 37f84ee

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

components/esp_modem/src/esp_modem_dte.cpp

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,53 @@ using namespace esp_modem;
1414

1515
static const size_t dte_default_buffer_size = 1000;
1616

17-
DTE::DTE(const esp_modem_dte_config *config, std::unique_ptr<Terminal> terminal):
18-
buffer(config->dte_buffer_size),
19-
cmux_term(nullptr), primary_term(std::move(terminal)), secondary_term(primary_term),
20-
mode(modem_mode::UNDEF)
17+
DTE::DTE(const esp_modem_dte_config *config, std::unique_ptr<Terminal> terminal)
18+
: buffer(config->dte_buffer_size),
19+
cmux_term(nullptr),
20+
primary_term(std::move(terminal)),
21+
secondary_term(primary_term),
22+
mode(modem_mode::UNDEF)
2123
{
24+
ESP_MODEM_THROW_IF_FALSE(primary_term != nullptr, "Invalid argument: terminal cannot be null");
2225
set_command_callbacks();
2326
}
2427

25-
DTE::DTE(std::unique_ptr<Terminal> terminal):
26-
buffer(dte_default_buffer_size),
27-
cmux_term(nullptr), primary_term(std::move(terminal)), secondary_term(primary_term),
28-
mode(modem_mode::UNDEF)
28+
DTE::DTE(std::unique_ptr<Terminal> terminal)
29+
: buffer(dte_default_buffer_size),
30+
cmux_term(nullptr),
31+
primary_term(std::move(terminal)),
32+
secondary_term(primary_term),
33+
mode(modem_mode::UNDEF)
2934
{
35+
ESP_MODEM_THROW_IF_FALSE(primary_term != nullptr, "Invalid argument: terminal cannot be null");
3036
set_command_callbacks();
3137
}
3238

33-
DTE::DTE(const esp_modem_dte_config *config, std::unique_ptr<Terminal> t, std::unique_ptr<Terminal> s):
34-
buffer(config->dte_buffer_size),
35-
cmux_term(nullptr), primary_term(std::move(t)), secondary_term(std::move(s)),
36-
mode(modem_mode::DUAL_MODE)
39+
DTE::DTE(const esp_modem_dte_config *config, std::unique_ptr<Terminal> t, std::unique_ptr<Terminal> s)
40+
: buffer(config->dte_buffer_size),
41+
cmux_term(nullptr),
42+
primary_term(std::move(t)),
43+
secondary_term(std::move(s)),
44+
mode(modem_mode::DUAL_MODE)
3745
{
46+
ESP_MODEM_THROW_IF_FALSE(primary_term != nullptr, "Invalid argument: primary terminal cannot be null");
47+
ESP_MODEM_THROW_IF_FALSE(secondary_term != nullptr, "Invalid argument: secondary terminal cannot be null");
3848
set_command_callbacks();
3949
}
4050

41-
DTE::DTE(std::unique_ptr<Terminal> t, std::unique_ptr<Terminal> s):
42-
buffer(dte_default_buffer_size),
43-
cmux_term(nullptr), primary_term(std::move(t)), secondary_term(std::move(s)),
44-
mode(modem_mode::DUAL_MODE)
51+
DTE::DTE(std::unique_ptr<Terminal> t, std::unique_ptr<Terminal> s)
52+
: buffer(dte_default_buffer_size),
53+
cmux_term(nullptr),
54+
primary_term(std::move(t)),
55+
secondary_term(std::move(s)),
56+
mode(modem_mode::DUAL_MODE)
4557
{
58+
ESP_MODEM_THROW_IF_FALSE(primary_term != nullptr, "Invalid argument: primary terminal cannot be null");
59+
ESP_MODEM_THROW_IF_FALSE(secondary_term != nullptr, "Invalid argument: secondary terminal cannot be null");
4660
set_command_callbacks();
4761
}
4862

63+
4964
void DTE::set_command_callbacks()
5065
{
5166
primary_term->set_read_cb([this](uint8_t *data, size_t len) {

0 commit comments

Comments
 (0)