From 21d51208c4884af47eee944df228768eb2622210 Mon Sep 17 00:00:00 2001 From: "nilesh.kale" Date: Mon, 15 Apr 2024 12:08:48 +0530 Subject: [PATCH] fix(esp_https_server): fix memory leak during configuring http server This MR This restructured code to prevent memory leak during the starting HTTP server. Closes https://github.com/espressif/esp-idf/issues/13526 --- components/esp_https_server/src/https_server.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/components/esp_https_server/src/https_server.c b/components/esp_https_server/src/https_server.c index f8693ffb9461..058faab30e80 100644 --- a/components/esp_https_server/src/https_server.c +++ b/components/esp_https_server/src/https_server.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2018-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2018-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -366,7 +366,6 @@ static esp_err_t create_secure_context(const struct httpd_ssl_config *config, ht free((void *) cfg->cacert_buf); } free(cfg); - free(*ssl_ctx); return ret; } @@ -379,14 +378,17 @@ esp_err_t httpd_ssl_start(httpd_handle_t *pHandle, struct httpd_ssl_config *conf ESP_LOGI(TAG, "Starting server"); esp_err_t ret = ESP_OK; + httpd_ssl_ctx_t *ssl_ctx = NULL; + if (HTTPD_SSL_TRANSPORT_SECURE == config->transport_mode) { - httpd_ssl_ctx_t *ssl_ctx = calloc(1, sizeof(httpd_ssl_ctx_t)); + ssl_ctx = calloc(1, sizeof(httpd_ssl_ctx_t)); if (!ssl_ctx) { return ESP_ERR_NO_MEM; } ret = create_secure_context(config, &ssl_ctx); if (ret != ESP_OK) { + free(ssl_ctx); return ret; } @@ -411,7 +413,11 @@ esp_err_t httpd_ssl_start(httpd_handle_t *pHandle, struct httpd_ssl_config *conf httpd_handle_t handle = NULL; ret = httpd_start(&handle, &config->httpd); - if (ret != ESP_OK) return ret; + if (ret != ESP_OK) { + free(ssl_ctx); + ssl_ctx = NULL; + return ret; + } *pHandle = handle;