Skip to content

Commit b45bee2

Browse files
fix(modem): null terminate output buffer if no response in esp_modem_at functions
1 parent c413fa7 commit b45bee2

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

components/esp_modem/src/esp_modem_c_api.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,12 @@ extern "C" esp_err_t esp_modem_at(esp_modem_dce_t *dce_wrap, const char *at, cha
217217
std::string out;
218218
std::string at_str(at);
219219
auto ret = command_response_to_esp_err(dce_wrap->dce->at(at_str, out, timeout));
220-
if ((p_out != NULL) && (!out.empty())) {
221-
strlcpy(p_out, out.c_str(), CONFIG_ESP_MODEM_C_API_STR_MAX);
220+
if ((p_out != NULL)) {
221+
if (!out.empty()) {
222+
strlcpy(p_out, out.c_str(), CONFIG_ESP_MODEM_C_API_STR_MAX);
223+
} else {
224+
p_out[0] = '\0'; // Clear output buffer if no response
225+
}
222226
}
223227
return ret;
224228
}
@@ -251,8 +255,12 @@ extern "C" esp_err_t esp_modem_at_raw(esp_modem_dce_t *dce_wrap, const char *cmd
251255
}
252256
std::string out;
253257
auto ret = command_response_to_esp_err(dce_wrap->dce->at_raw(cmd, out, pass, fail, timeout));
254-
if ((p_out != NULL) && (!out.empty())) {
255-
strlcpy(p_out, out.c_str(), CONFIG_ESP_MODEM_C_API_STR_MAX);
258+
if ((p_out != NULL)) {
259+
if (!out.empty()) {
260+
strlcpy(p_out, out.c_str(), CONFIG_ESP_MODEM_C_API_STR_MAX);
261+
} else {
262+
p_out[0] = '\0'; // Clear output buffer if no response
263+
}
256264
}
257265
return ret;
258266
}

0 commit comments

Comments
 (0)