fix(http_txrx): Resource leak in http_txrx

res_buf was not freed for the chunk response type in case of
first_chunk_sent true condition.

This commit ensures that resp_buf is freed and few cosmetic
changes are made
This commit is contained in:
hrushikesh.bhosale
2025-02-24 10:58:51 +05:30
parent 877057db3d
commit 56de1f4ed1

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);