mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-04 13:14:32 +02:00
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:
@@ -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,23 +332,23 @@ 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;
|
||||||
|
|
||||||
/* 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;
|
|
||||||
if (required_size > ra->max_req_hdr_len) {
|
|
||||||
return ESP_ERR_HTTPD_RESP_HDR;
|
|
||||||
}
|
|
||||||
char *res_buf = malloc(required_size); /* Temporary buffer to store the headers */
|
|
||||||
if (res_buf == NULL) {
|
|
||||||
ESP_LOGE(TAG, "Unable to allocate httpd send chunk buffer");
|
|
||||||
return ESP_ERR_HTTPD_ALLOC_MEM;
|
|
||||||
}
|
|
||||||
ESP_LOGD(TAG, "httpd send chunk buffer size = %d", strlen(res_buf));
|
|
||||||
if (!ra->first_chunk_sent) {
|
if (!ra->first_chunk_sent) {
|
||||||
|
/* 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;
|
||||||
|
if (required_size > ra->max_req_hdr_len) {
|
||||||
|
return ESP_ERR_HTTPD_RESP_HDR;
|
||||||
|
}
|
||||||
|
char *res_buf = malloc(required_size); /* Temporary buffer to store the headers */
|
||||||
|
if (res_buf == NULL) {
|
||||||
|
ESP_LOGE(TAG, "Unable to allocate httpd send chunk buffer");
|
||||||
|
return ESP_ERR_HTTPD_ALLOC_MEM;
|
||||||
|
}
|
||||||
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);
|
||||||
|
Reference in New Issue
Block a user