Merge branch 'bugfix/protocol_examples_check_alloc_ret' into 'master'

fix: Check memory allocation failures in protocol examples

See merge request espressif/esp-idf!28237
This commit is contained in:
Mahavir Jain 2024-01-08 11:50:18 +08:00
commit f94785fcd7
4 changed files with 14 additions and 0 deletions

View File

@ -4,6 +4,7 @@
#include <esp_log.h>
#include <esp_system.h>
#include <esp_http_server.h>
#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) {

View File

@ -15,6 +15,7 @@
#include "esp_netif.h"
#include "esp_eth.h"
#include "protocol_examples_common.h"
#include "esp_check.h"
#include <esp_http_server.h>
@ -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;

View File

@ -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 <esp_wifi.h>
@ -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};

View File

@ -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) {