fix(esp_netif): Prevent running esp_netif_sntp_init() multiple times

Closes https://github.com/espressif/esp-idf/issues/12854
This commit is contained in:
David Cermak
2024-02-07 09:09:59 +01:00
parent 37f0447961
commit feef14b43b
4 changed files with 13 additions and 4 deletions
@@ -91,6 +91,7 @@ void esp_netif_sntp_renew_servers(void *handler_args, esp_event_base_t base, int
esp_err_t esp_netif_sntp_init(const esp_sntp_config_t * config)
{
esp_err_t ret = ESP_OK;
ESP_RETURN_ON_FALSE(s_storage == NULL, ESP_ERR_INVALID_STATE, TAG, "esp_netif_sntp already initialized");
s_storage = calloc(1, sizeof(sntp_storage_t) + // allocate space for servers only if we are supposed to refresh the settings
(config->renew_servers_after_new_IP ? config->num_of_servers * sizeof(char*) : 0));
ESP_GOTO_ON_FALSE(s_storage != NULL, ESP_ERR_NO_MEM, err, TAG, "Failed to allocate SNTP storage");
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
@@ -48,7 +48,15 @@ TEST(esp_netif, init_and_destroy_sntp)
{
esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG("127.0.0.1");
config.start = false;
esp_netif_sntp_init(&config);
TEST_ESP_OK(esp_netif_sntp_init(&config));
// Cannot initialize multiple times
TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_sntp_init(&config));
// Try again to see that the state didn't change
TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_sntp_init(&config));
esp_netif_sntp_deinit();
// Can initialize again once it's destroyed
TEST_ESP_OK(esp_netif_sntp_init(&config));
esp_netif_sntp_deinit();
}