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
This commit is contained in:
Mahavir Jain
2025-07-17 14:26:31 +05:30
2 changed files with 11 additions and 2 deletions

View File

@@ -1075,6 +1075,7 @@ esp_err_t esp_http_client_cleanup(esp_http_client_handle_t client)
_clear_auth_data(client);
free(client->auth_data);
free(client->current_header_key);
free(client->current_header_value);
free(client->location);
free(client->auth_header);
free(client);

View File

@@ -68,8 +68,16 @@ char *http_utils_append_string(char **str, const char *new_str, int len)
}
if (old_str) {
old_len = strlen(old_str);
old_str = realloc(old_str, old_len + l + 1);
ESP_RETURN_ON_FALSE(old_str, NULL, TAG, "Memory exhausted");
// old_str should not be reallocated directly, as in case of memory exhaustion,
// it will be lost and we will not be able to free it.
char *tmp = realloc(old_str, old_len + l + 1);
if (tmp == NULL) {
free(old_str);
old_str = NULL;
ESP_RETURN_ON_FALSE(tmp, NULL, TAG, "Memory exhausted");
}
old_str = tmp;
// Ensure the new string is null-terminated
old_str[old_len + l] = 0;
} else {
old_str = calloc(1, l + 1);