Skip to content

Commit c6cc403

Browse files
committed
Merge branch 'bugfix/fix_memory_leak_on_http_header_fetch_failure' into 'master'
fix(esp_http_client): free header in case of ESP_ERR_HTTP_FETCH_HEADER See merge request espressif/esp-idf!40528
2 parents b65366d + 801ea1f commit c6cc403

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

components/esp_http_client/esp_http_client.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,7 @@ esp_err_t esp_http_client_cleanup(esp_http_client_handle_t client)
10751075
_clear_auth_data(client);
10761076
free(client->auth_data);
10771077
free(client->current_header_key);
1078+
free(client->current_header_value);
10781079
free(client->location);
10791080
free(client->auth_header);
10801081
free(client);

components/esp_http_client/lib/http_utils.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,16 @@ char *http_utils_append_string(char **str, const char *new_str, int len)
6868
}
6969
if (old_str) {
7070
old_len = strlen(old_str);
71-
old_str = realloc(old_str, old_len + l + 1);
72-
ESP_RETURN_ON_FALSE(old_str, NULL, TAG, "Memory exhausted");
71+
// old_str should not be reallocated directly, as in case of memory exhaustion,
72+
// it will be lost and we will not be able to free it.
73+
char *tmp = realloc(old_str, old_len + l + 1);
74+
if (tmp == NULL) {
75+
free(old_str);
76+
old_str = NULL;
77+
ESP_RETURN_ON_FALSE(tmp, NULL, TAG, "Memory exhausted");
78+
}
79+
old_str = tmp;
80+
// Ensure the new string is null-terminated
7381
old_str[old_len + l] = 0;
7482
} else {
7583
old_str = calloc(1, l + 1);

0 commit comments

Comments
 (0)