Merge branch 'fix/resource_leak_in_httpd_txrx' into 'master'

fix(http_txrx): Resource leak in http_txrx

Closes IDF-12529

See merge request espressif/esp-idf!37223
This commit is contained in:
Mahavir Jain
2025-02-28 17:42:30 +08:00

View File

@@ -257,13 +257,13 @@ esp_err_t httpd_resp_send(httpd_req_t *r, const char *buf, ssize_t buf_len)
ESP_LOGE(TAG, "Unable to allocate httpd send buffer"); ESP_LOGE(TAG, "Unable to allocate httpd send buffer");
return ESP_ERR_HTTPD_ALLOC_MEM; return ESP_ERR_HTTPD_ALLOC_MEM;
} }
ESP_LOGD(TAG, "httpd send buffer size = %d", strlen(res_buf));
esp_err_t ret = snprintf(res_buf, required_size, httpd_hdr_str, ra->status, ra->content_type, buf_len); esp_err_t ret = snprintf(res_buf, required_size, httpd_hdr_str, ra->status, ra->content_type, buf_len);
if (ret < 0 || ret >= required_size) { if (ret < 0 || ret >= required_size) {
free(res_buf); free(res_buf);
return ESP_ERR_HTTPD_RESP_HDR; return ESP_ERR_HTTPD_RESP_HDR;
} }
ESP_LOGD(TAG, "httpd send buffer size = %d", strlen(res_buf));
ret = httpd_send_all(r, res_buf, strlen(res_buf)); ret = httpd_send_all(r, res_buf, strlen(res_buf));
free(res_buf); free(res_buf);
if (ret != ESP_OK) { if (ret != ESP_OK) {
@@ -332,6 +332,7 @@ esp_err_t httpd_resp_send_chunk(httpd_req_t *r, const char *buf, ssize_t buf_len
/* Request headers are no longer available */ /* Request headers are no longer available */
ra->req_hdrs_count = 0; ra->req_hdrs_count = 0;
if (!ra->first_chunk_sent) {
/* Calculate the size of the headers. +1 for the null terminator */ /* Calculate the size of the headers. +1 for the null terminator */
size_t required_size = snprintf(NULL, 0, httpd_chunked_hdr_str, ra->status, ra->content_type) + 1; size_t required_size = snprintf(NULL, 0, httpd_chunked_hdr_str, ra->status, ra->content_type) + 1;
if (required_size > ra->max_req_hdr_len) { if (required_size > ra->max_req_hdr_len) {
@@ -342,13 +343,12 @@ esp_err_t httpd_resp_send_chunk(httpd_req_t *r, const char *buf, ssize_t buf_len
ESP_LOGE(TAG, "Unable to allocate httpd send chunk buffer"); ESP_LOGE(TAG, "Unable to allocate httpd send chunk buffer");
return ESP_ERR_HTTPD_ALLOC_MEM; return ESP_ERR_HTTPD_ALLOC_MEM;
} }
ESP_LOGD(TAG, "httpd send chunk buffer size = %d", strlen(res_buf));
if (!ra->first_chunk_sent) {
esp_err_t ret = snprintf(res_buf, required_size, httpd_chunked_hdr_str, ra->status, ra->content_type); esp_err_t ret = snprintf(res_buf, required_size, httpd_chunked_hdr_str, ra->status, ra->content_type);
if (ret < 0 || ret >= required_size) { if (ret < 0 || ret >= required_size) {
free(res_buf); free(res_buf);
return ESP_ERR_HTTPD_RESP_HDR; return ESP_ERR_HTTPD_RESP_HDR;
} }
ESP_LOGD(TAG, "httpd send chunk buffer size = %d", strlen(res_buf));
/* Size of essential headers is limited by scratch buffer size */ /* Size of essential headers is limited by scratch buffer size */
ret = httpd_send_all(r, res_buf, strlen(res_buf)); ret = httpd_send_all(r, res_buf, strlen(res_buf));
free(res_buf); free(res_buf);