From e636c680a5a5cb2f336f35d5530dc25c4a03bc51 Mon Sep 17 00:00:00 2001 From: Harshit Malpani Date: Wed, 3 Jan 2024 11:25:27 +0530 Subject: [PATCH] fix: Check memory allocation failures in protocol examples --- examples/protocols/http_server/advanced_tests/main/tests.c | 3 +++ .../protocols/http_server/persistent_sockets/main/main.c | 4 ++++ examples/protocols/http_server/simple/main/main.c | 5 +++++ .../https_server/wss_server/main/wss_server_example.c | 2 ++ 4 files changed, 14 insertions(+) diff --git a/examples/protocols/http_server/advanced_tests/main/tests.c b/examples/protocols/http_server/advanced_tests/main/tests.c index 3780670a53..916f7470b3 100644 --- a/examples/protocols/http_server/advanced_tests/main/tests.c +++ b/examples/protocols/http_server/advanced_tests/main/tests.c @@ -4,6 +4,7 @@ #include #include #include +#include "esp_check.h" #include "tests.h" @@ -221,6 +222,7 @@ static esp_err_t adder_post_handler(httpd_req_t *req) if (! req->sess_ctx) { ESP_LOGI(TAG, "/adder allocating new session"); req->sess_ctx = malloc(sizeof(int)); + ESP_RETURN_ON_FALSE(req->sess_ctx, ESP_ERR_NO_MEM, TAG, "Failed to allocate sess_ctx"); req->free_ctx = adder_free_func; *(int *)req->sess_ctx = 0; } @@ -286,6 +288,7 @@ static esp_err_t async_get_handler(httpd_req_t *req) * socket again */ struct async_resp_arg *resp_arg = malloc(sizeof(struct async_resp_arg)); + ESP_RETURN_ON_FALSE(resp_arg, ESP_ERR_NO_MEM, TAG, "Failed to allocate resp_arg"); resp_arg->hd = req->handle; resp_arg->fd = httpd_req_to_sockfd(req); if (resp_arg->fd < 0) { diff --git a/examples/protocols/http_server/persistent_sockets/main/main.c b/examples/protocols/http_server/persistent_sockets/main/main.c index 92be4422a1..56eb0f749f 100644 --- a/examples/protocols/http_server/persistent_sockets/main/main.c +++ b/examples/protocols/http_server/persistent_sockets/main/main.c @@ -15,6 +15,7 @@ #include "esp_netif.h" #include "esp_eth.h" #include "protocol_examples_common.h" +#include "esp_check.h" #include @@ -61,6 +62,7 @@ static esp_err_t adder_post_handler(httpd_req_t *req) if (! req->sess_ctx) { ESP_LOGI(TAG, "/adder allocating new session"); req->sess_ctx = malloc(sizeof(int)); + ESP_RETURN_ON_FALSE(req->sess_ctx, ESP_ERR_NO_MEM, TAG, "Failed to allocate sess_ctx"); req->free_ctx = adder_free_func; *(int *)req->sess_ctx = 0; } @@ -88,6 +90,7 @@ static esp_err_t adder_get_handler(httpd_req_t *req) if (! req->sess_ctx) { ESP_LOGI(TAG, "/adder GET allocating new session"); req->sess_ctx = malloc(sizeof(int)); + ESP_RETURN_ON_FALSE(req->sess_ctx, ESP_ERR_NO_MEM, TAG, "Failed to allocate sess_ctx"); req->free_ctx = adder_free_func; *(int *)req->sess_ctx = 0; } @@ -168,6 +171,7 @@ static esp_err_t adder_put_handler(httpd_req_t *req) if (! req->sess_ctx) { ESP_LOGI(TAG, "/adder PUT allocating new session"); req->sess_ctx = malloc(sizeof(int)); + ESP_RETURN_ON_FALSE(req->sess_ctx, ESP_ERR_NO_MEM, TAG, "Failed to allocate sess_ctx"); req->free_ctx = adder_free_func; } *(int *)req->sess_ctx = val; diff --git a/examples/protocols/http_server/simple/main/main.c b/examples/protocols/http_server/simple/main/main.c index 4a0678a39c..d23969c591 100644 --- a/examples/protocols/http_server/simple/main/main.c +++ b/examples/protocols/http_server/simple/main/main.c @@ -21,6 +21,7 @@ #include "esp_event.h" #include "esp_netif.h" #include "esp_tls.h" +#include "esp_check.h" #if !CONFIG_IDF_TARGET_LINUX #include @@ -177,6 +178,7 @@ static esp_err_t hello_get_handler(httpd_req_t *req) buf_len = httpd_req_get_hdr_value_len(req, "Host") + 1; if (buf_len > 1) { buf = malloc(buf_len); + ESP_RETURN_ON_FALSE(buf, ESP_ERR_NO_MEM, TAG, "buffer alloc failed"); /* Copy null terminated value string into buffer */ if (httpd_req_get_hdr_value_str(req, "Host", buf, buf_len) == ESP_OK) { ESP_LOGI(TAG, "Found header => Host: %s", buf); @@ -187,6 +189,7 @@ static esp_err_t hello_get_handler(httpd_req_t *req) buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-2") + 1; if (buf_len > 1) { buf = malloc(buf_len); + ESP_RETURN_ON_FALSE(buf, ESP_ERR_NO_MEM, TAG, "buffer alloc failed"); if (httpd_req_get_hdr_value_str(req, "Test-Header-2", buf, buf_len) == ESP_OK) { ESP_LOGI(TAG, "Found header => Test-Header-2: %s", buf); } @@ -196,6 +199,7 @@ static esp_err_t hello_get_handler(httpd_req_t *req) buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-1") + 1; if (buf_len > 1) { buf = malloc(buf_len); + ESP_RETURN_ON_FALSE(buf, ESP_ERR_NO_MEM, TAG, "buffer alloc failed"); if (httpd_req_get_hdr_value_str(req, "Test-Header-1", buf, buf_len) == ESP_OK) { ESP_LOGI(TAG, "Found header => Test-Header-1: %s", buf); } @@ -207,6 +211,7 @@ static esp_err_t hello_get_handler(httpd_req_t *req) buf_len = httpd_req_get_url_query_len(req) + 1; if (buf_len > 1) { buf = malloc(buf_len); + ESP_RETURN_ON_FALSE(buf, ESP_ERR_NO_MEM, TAG, "buffer alloc failed"); if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK) { ESP_LOGI(TAG, "Found URL query => %s", buf); char param[EXAMPLE_HTTP_QUERY_KEY_MAX_LEN], dec_param[EXAMPLE_HTTP_QUERY_KEY_MAX_LEN] = {0}; diff --git a/examples/protocols/https_server/wss_server/main/wss_server_example.c b/examples/protocols/https_server/wss_server/main/wss_server_example.c index 8b045d20d2..cc27539f8a 100644 --- a/examples/protocols/https_server/wss_server/main/wss_server_example.c +++ b/examples/protocols/https_server/wss_server/main/wss_server_example.c @@ -167,6 +167,7 @@ bool check_client_alive_cb(wss_keep_alive_t h, int fd) { ESP_LOGD(TAG, "Checking if client (fd=%d) is alive", fd); struct async_resp_arg *resp_arg = malloc(sizeof(struct async_resp_arg)); + assert(resp_arg != NULL); resp_arg->hd = wss_keep_alive_get_user_ctx(h); resp_arg->fd = fd; @@ -269,6 +270,7 @@ static void wss_server_send_messages(httpd_handle_t* server) if (httpd_ws_get_fd_info(*server, sock) == HTTPD_WS_CLIENT_WEBSOCKET) { ESP_LOGI(TAG, "Active client (fd=%d) -> sending async message", sock); struct async_resp_arg *resp_arg = malloc(sizeof(struct async_resp_arg)); + assert(resp_arg != NULL); resp_arg->hd = *server; resp_arg->fd = sock; if (httpd_queue_work(resp_arg->hd, send_hello, resp_arg) != ESP_OK) {