Skip to content

Commit 76a84fa

Browse files
committed
fix(net_connect): improve error handling and memory safety
Addresses code review feedback from GitHub cursor bot.
1 parent c18c062 commit 76a84fa

File tree

8 files changed

+71
-22
lines changed

8 files changed

+71
-22
lines changed

components/net_connect/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ endif()
3333

3434
idf_component_register(SRCS "${srcs}"
3535
INCLUDE_DIRS "include"
36-
PRIV_REQUIRES esp_netif esp_driver_gpio esp_driver_uart esp_wifi vfs console openthread)
36+
PRIV_REQUIRES esp_netif esp_driver_gpio esp_driver_uart esp_wifi vfs openthread)
3737

3838
if(CONFIG_NET_CONNECT_PROVIDE_WIFI_CONSOLE_CMD)
3939
idf_component_optional_requires(PRIVATE console)

components/net_connect/addr_from_stdin.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,26 @@ esp_err_t get_addr_from_stdin(int port, int sock_type, int *ip_protocol, int *ad
3030

3131
// ignore empty or LF only string (could receive from DUT class)
3232
do {
33-
fgets(host_ip, HOST_IP_SIZE, stdin);
33+
if (fgets(host_ip, HOST_IP_SIZE, stdin) == NULL) {
34+
// EOF or error occurred
35+
return ESP_FAIL;
36+
}
3437
len = strlen(host_ip);
3538
} while (len <= 1 && host_ip[0] == '\n');
36-
host_ip[len - 1] = '\0';
39+
40+
// Remove trailing newline if present
41+
if (len > 0 && host_ip[len - 1] == '\n') {
42+
host_ip[len - 1] = '\0';
43+
}
3744

3845
struct addrinfo hints, *addr_list, *cur;
3946
memset(&hints, 0, sizeof(hints));
4047

4148
// run getaddrinfo() to decide on the IP protocol
4249
hints.ai_family = AF_UNSPEC;
4350
hints.ai_socktype = sock_type;
44-
hints.ai_protocol = IPPROTO_TCP;
51+
// Set protocol based on socket type, or 0 to let getaddrinfo infer it
52+
hints.ai_protocol = (sock_type == SOCK_DGRAM) ? IPPROTO_UDP : IPPROTO_TCP;
4553
if (getaddrinfo(host_ip, NULL, &hints, &addr_list) != 0) {
4654
return ESP_FAIL;
4755
}

components/net_connect/console_cmd.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ static int cmd_do_wifi_connect(int argc, char **argv)
4343
wifi_config.sta.channel = (uint8_t)(connect_args.channel->ival[0]);
4444
}
4545
const char *ssid = connect_args.ssid->sval[0];
46-
const char *pass = connect_args.password->sval[0];
46+
const char *pass = NULL;
47+
if (connect_args.password->count > 0) {
48+
pass = connect_args.password->sval[0];
49+
}
4750
strlcpy((char *) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
4851
if (pass) {
4952
strlcpy((char *) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password));

components/net_connect/eth_connect.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,10 @@ esp_err_t net_connect_ethernet_connect(void)
151151
#if CONFIG_NET_CONNECT_IPV6
152152
s_semph_get_ip6_addrs = xSemaphoreCreateBinary();
153153
if (s_semph_get_ip6_addrs == NULL) {
154+
#if CONFIG_NET_CONNECT_IPV4
154155
vSemaphoreDelete(s_semph_get_ip_addrs);
156+
s_semph_get_ip_addrs = NULL;
157+
#endif
155158
return ESP_ERR_NO_MEM;
156159
}
157160
#endif

components/net_connect/ppp_connect.c

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ static void cdc_rx_callback(int itf, cdcacm_event_t *event)
115115
// Not our channel
116116
return;
117117
}
118+
// Check if netif is still valid (not destroyed during shutdown)
119+
if (s_netif == NULL) {
120+
return;
121+
}
118122
esp_err_t ret = tinyusb_cdcacm_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE, &rx_size);
119123
if (ret == ESP_OK) {
120124
ESP_LOG_BUFFER_HEXDUMP(TAG, buf, rx_size, ESP_LOG_VERBOSE);
@@ -149,22 +153,29 @@ static void ppp_task(void *args)
149153
ESP_ERROR_CHECK(uart_set_rx_timeout(UART_NUM_1, 1));
150154

151155
char *buffer = (char*)malloc(BUF_SIZE);
156+
if (buffer == NULL) {
157+
ESP_LOGE(TAG, "Failed to allocate memory for UART buffer");
158+
uart_driver_delete(UART_NUM_1);
159+
vTaskDelete(NULL);
160+
return;
161+
}
152162
uart_event_t event;
153163
esp_event_handler_register(IP_EVENT, IP_EVENT_PPP_GOT_IP, esp_netif_action_connected, s_netif);
154164
esp_netif_action_start(s_netif, 0, 0, 0);
155165
esp_netif_action_connected(s_netif, 0, 0, 0);
156166
while (!s_stop_task) {
157-
xQueueReceive(event_queue, &event, pdMS_TO_TICKS(1000));
158-
if (event.type == UART_DATA) {
159-
size_t len;
160-
uart_get_buffered_data_len(UART_NUM_1, &len);
161-
if (len) {
162-
len = uart_read_bytes(UART_NUM_1, buffer, BUF_SIZE, 0);
163-
ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, len, ESP_LOG_VERBOSE);
164-
esp_netif_receive(s_netif, buffer, len, NULL);
167+
if (xQueueReceive(event_queue, &event, pdMS_TO_TICKS(1000)) == pdTRUE) {
168+
if (event.type == UART_DATA) {
169+
size_t len;
170+
uart_get_buffered_data_len(UART_NUM_1, &len);
171+
if (len) {
172+
len = uart_read_bytes(UART_NUM_1, buffer, BUF_SIZE, 0);
173+
ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, len, ESP_LOG_VERBOSE);
174+
esp_netif_receive(s_netif, buffer, len, NULL);
175+
}
176+
} else {
177+
ESP_LOGW(TAG, "Received UART event: %d", event.type);
165178
}
166-
} else {
167-
ESP_LOGW(TAG, "Received UART event: %d", event.type);
168179
}
169180
}
170181
free(buffer);
@@ -243,7 +254,12 @@ esp_err_t net_connect_ppp_connect(void)
243254
void net_connect_ppp_shutdown(void)
244255
{
245256
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, on_ip_event));
246-
#if CONFIG_NET_CONNECT_PPP_DEVICE_UART
257+
#if CONFIG_NET_CONNECT_PPP_DEVICE_USB
258+
// Unregister CDC line state callback to prevent access to destroyed netif
259+
// Note: RX callback is set via config and cannot be unregistered, but cdc_rx_callback
260+
// has a NULL check to prevent crashes if called after shutdown
261+
tinyusb_cdcacm_register_callback(TINYUSB_CDC_ACM_0, CDC_EVENT_LINE_STATE_CHANGED, NULL);
262+
#elif CONFIG_NET_CONNECT_PPP_DEVICE_UART
247263
s_stop_task = true;
248264
vTaskDelay(pdMS_TO_TICKS(1000)); // wait for the ppp task to stop
249265
#endif

components/net_connect/stdin_out.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "driver/uart.h"
1111
#include "sdkconfig.h"
1212

13-
esp_err_t example_configure_stdin_stdout(void)
13+
esp_err_t net_configure_stdin_stdout(void)
1414
{
1515
if (uart_is_driver_installed((uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM)) {
1616
return ESP_OK;

components/net_connect/thread_connect.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ static void ot_task_worker(void *aContext)
8686
/* tear down connection, release resources */
8787
void net_connect_thread_shutdown(void)
8888
{
89-
vTaskDelete(s_ot_task_handle);
89+
if (s_ot_task_handle != NULL) {
90+
vTaskDelete(s_ot_task_handle);
91+
s_ot_task_handle = NULL;
92+
}
9093
esp_openthread_netif_glue_deinit();
9194
esp_netif_destroy(s_openthread_netif);
9295
esp_vfs_eventfd_unregister();

components/net_connect/wifi_connect.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,19 @@ void net_connect_wifi_stop(void)
185185
esp_err_t net_connect_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait)
186186
{
187187
if (wait) {
188+
#if CONFIG_NET_CONNECT_IPV4
188189
s_semph_get_ip_addrs = xSemaphoreCreateBinary();
189190
if (s_semph_get_ip_addrs == NULL) {
190191
return ESP_ERR_NO_MEM;
191192
}
193+
#endif
192194
#if CONFIG_NET_CONNECT_IPV6
193195
s_semph_get_ip6_addrs = xSemaphoreCreateBinary();
194196
if (s_semph_get_ip6_addrs == NULL) {
197+
#if CONFIG_NET_CONNECT_IPV4
195198
vSemaphoreDelete(s_semph_get_ip_addrs);
199+
s_semph_get_ip_addrs = NULL;
200+
#endif
196201
return ESP_ERR_NO_MEM;
197202
}
198203
#endif
@@ -293,18 +298,29 @@ esp_err_t net_connect_wifi(void)
293298
net_configure_stdin_stdout();
294299
char buf[sizeof(wifi_config.sta.ssid) + sizeof(wifi_config.sta.password) + 2] = {0};
295300
ESP_LOGI(TAG, "Please input ssid password:");
296-
fgets(buf, sizeof(buf), stdin);
301+
if (fgets(buf, sizeof(buf), stdin) == NULL) {
302+
ESP_LOGE(TAG, "Failed to read SSID/password from stdin (EOF or error)");
303+
return ESP_ERR_INVALID_STATE;
304+
}
297305
int len = strlen(buf);
298-
buf[len - 1] = '\0'; /* removes '\n' */
306+
if (len > 0) {
307+
buf[len - 1] = '\0'; /* removes '\n' */
308+
}
299309
memset(wifi_config.sta.ssid, 0, sizeof(wifi_config.sta.ssid));
300310

301311
char *rest = NULL;
302312
char *temp = strtok_r(buf, " ", &rest);
303-
strncpy((char*)wifi_config.sta.ssid, temp, sizeof(wifi_config.sta.ssid));
313+
if (temp == NULL) {
314+
ESP_LOGE(TAG, "SSID is empty or invalid");
315+
return ESP_ERR_INVALID_ARG;
316+
}
317+
strncpy((char*)wifi_config.sta.ssid, temp, sizeof(wifi_config.sta.ssid) - 1);
318+
wifi_config.sta.ssid[sizeof(wifi_config.sta.ssid) - 1] = '\0';
304319
memset(wifi_config.sta.password, 0, sizeof(wifi_config.sta.password));
305320
temp = strtok_r(NULL, " ", &rest);
306321
if (temp) {
307-
strncpy((char*)wifi_config.sta.password, temp, sizeof(wifi_config.sta.password));
322+
strncpy((char*)wifi_config.sta.password, temp, sizeof(wifi_config.sta.password) - 1);
323+
wifi_config.sta.password[sizeof(wifi_config.sta.password) - 1] = '\0';
308324
} else {
309325
wifi_config.sta.threshold.authmode = WIFI_AUTH_OPEN;
310326
}

0 commit comments

Comments
 (0)