forked from espressif/esp-mqtt
fix possible double free of client config if init fails
This commit is contained in:
@ -402,8 +402,8 @@ esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_mqtt_cl
|
|||||||
// Set uri at the end of config to override separately configured uri elements
|
// Set uri at the end of config to override separately configured uri elements
|
||||||
if (config->uri) {
|
if (config->uri) {
|
||||||
if (esp_mqtt_client_set_uri(client, cfg->uri) != ESP_OK) {
|
if (esp_mqtt_client_set_uri(client, cfg->uri) != ESP_OK) {
|
||||||
esp_mqtt_destroy_config(client);
|
err = ESP_FAIL;
|
||||||
return ESP_FAIL;
|
goto _mqtt_set_config_failed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -419,6 +419,9 @@ _mqtt_set_config_failed:
|
|||||||
static esp_err_t esp_mqtt_destroy_config(esp_mqtt_client_handle_t client)
|
static esp_err_t esp_mqtt_destroy_config(esp_mqtt_client_handle_t client)
|
||||||
{
|
{
|
||||||
mqtt_config_storage_t *cfg = client->config;
|
mqtt_config_storage_t *cfg = client->config;
|
||||||
|
if (cfg == NULL) {
|
||||||
|
return ESP_ERR_INVALID_STATE;
|
||||||
|
}
|
||||||
free(cfg->host);
|
free(cfg->host);
|
||||||
free(cfg->uri);
|
free(cfg->uri);
|
||||||
free(cfg->path);
|
free(cfg->path);
|
||||||
@ -441,6 +444,7 @@ static esp_err_t esp_mqtt_destroy_config(esp_mqtt_client_handle_t client)
|
|||||||
#endif
|
#endif
|
||||||
memset(cfg, 0, sizeof(mqtt_config_storage_t));
|
memset(cfg, 0, sizeof(mqtt_config_storage_t));
|
||||||
free(client->config);
|
free(client->config);
|
||||||
|
client->config = NULL;
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user