esp_http_client: fix memory leak in esp_http_client_set_username/password

Fix memory in case username/password was set before calling
esp_http_client_set_username/password.

Closes https://github.com/espressif/esp-idf/issues/4444
Fixes: 9fd16c6a5f ("fixes : set_url discards username and password")
Signed-off-by: Axel Lin <axel.lin@gmail.com>
This commit is contained in:
Axel Lin 2019-12-06 15:08:41 +08:00 committed by Mahavir Jain
parent 5f6fd238b6
commit 6fdc8d7f92

View file

@ -300,12 +300,10 @@ esp_err_t esp_http_client_set_username(esp_http_client_handle_t client, const ch
ESP_LOGE(TAG, "client must not be NULL"); ESP_LOGE(TAG, "client must not be NULL");
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
if (username == NULL && client->connection_info.username != NULL) { if (client->connection_info.username != NULL) {
free(client->connection_info.username); free(client->connection_info.username);
client->connection_info.username = NULL;
} else if (username != NULL) {
client->connection_info.username = strdup(username);
} }
client->connection_info.username = username ? strdup(username) : NULL;
return ESP_OK; return ESP_OK;
} }
@ -325,13 +323,11 @@ esp_err_t esp_http_client_set_password(esp_http_client_handle_t client, char *pa
ESP_LOGE(TAG, "client must not be NULL"); ESP_LOGE(TAG, "client must not be NULL");
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
if (password == NULL && client->connection_info.password != NULL) { if (client->connection_info.password != NULL) {
memset(client->connection_info.password, 0, strlen(client->connection_info.password)); memset(client->connection_info.password, 0, strlen(client->connection_info.password));
free(client->connection_info.password); free(client->connection_info.password);
client->connection_info.password = NULL;
} else if (password != NULL) {
client->connection_info.password = strdup(password);
} }
client->connection_info.password = password ? strdup(password) : NULL;
return ESP_OK; return ESP_OK;
} }