Skip to content

Commit bf51415

Browse files
committed
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
2 parents 3420672 + 56de1f4 commit bf51415

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

components/esp_http_server/src/httpd_txrx.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,13 @@ esp_err_t httpd_resp_send(httpd_req_t *r, const char *buf, ssize_t buf_len)
257257
ESP_LOGE(TAG, "Unable to allocate httpd send buffer");
258258
return ESP_ERR_HTTPD_ALLOC_MEM;
259259
}
260-
ESP_LOGD(TAG, "httpd send buffer size = %d", strlen(res_buf));
261260

262261
esp_err_t ret = snprintf(res_buf, required_size, httpd_hdr_str, ra->status, ra->content_type, buf_len);
263262
if (ret < 0 || ret >= required_size) {
264263
free(res_buf);
265264
return ESP_ERR_HTTPD_RESP_HDR;
266265
}
266+
ESP_LOGD(TAG, "httpd send buffer size = %d", strlen(res_buf));
267267
ret = httpd_send_all(r, res_buf, strlen(res_buf));
268268
free(res_buf);
269269
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
332332
/* Request headers are no longer available */
333333
ra->req_hdrs_count = 0;
334334

335-
/* Calculate the size of the headers. +1 for the null terminator */
336-
size_t required_size = snprintf(NULL, 0, httpd_chunked_hdr_str, ra->status, ra->content_type) + 1;
337-
if (required_size > ra->max_req_hdr_len) {
338-
return ESP_ERR_HTTPD_RESP_HDR;
339-
}
340-
char *res_buf = malloc(required_size); /* Temporary buffer to store the headers */
341-
if (res_buf == NULL) {
342-
ESP_LOGE(TAG, "Unable to allocate httpd send chunk buffer");
343-
return ESP_ERR_HTTPD_ALLOC_MEM;
344-
}
345-
ESP_LOGD(TAG, "httpd send chunk buffer size = %d", strlen(res_buf));
346335
if (!ra->first_chunk_sent) {
336+
/* Calculate the size of the headers. +1 for the null terminator */
337+
size_t required_size = snprintf(NULL, 0, httpd_chunked_hdr_str, ra->status, ra->content_type) + 1;
338+
if (required_size > ra->max_req_hdr_len) {
339+
return ESP_ERR_HTTPD_RESP_HDR;
340+
}
341+
char *res_buf = malloc(required_size); /* Temporary buffer to store the headers */
342+
if (res_buf == NULL) {
343+
ESP_LOGE(TAG, "Unable to allocate httpd send chunk buffer");
344+
return ESP_ERR_HTTPD_ALLOC_MEM;
345+
}
347346
esp_err_t ret = snprintf(res_buf, required_size, httpd_chunked_hdr_str, ra->status, ra->content_type);
348347
if (ret < 0 || ret >= required_size) {
349348
free(res_buf);
350349
return ESP_ERR_HTTPD_RESP_HDR;
351350
}
351+
ESP_LOGD(TAG, "httpd send chunk buffer size = %d", strlen(res_buf));
352352
/* Size of essential headers is limited by scratch buffer size */
353353
ret = httpd_send_all(r, res_buf, strlen(res_buf));
354354
free(res_buf);

0 commit comments

Comments
 (0)