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
Fixes: ("fixes : set_url discards username and password")
@@ -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");
return ESP_ERR_INVALID_ARG;
}
- if (username == NULL && client->connection_info.username != NULL) {
+ if (client->connection_info.username != NULL) {
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;
}
@@ -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");
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));
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;
}
