]> granicus.if.org Git - esp-idf/commitdiff
nvs_flash: delete Storage if init fails
authorIvan Grokhotkov <ivan@espressif.com>
Mon, 18 Sep 2017 14:34:16 +0000 (22:34 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Tue, 19 Sep 2017 02:39:44 +0000 (10:39 +0800)
Previously, nvs_flash_init_custom would not be called if Storage for a
particular partition was already created. This caused issues if the
first call to nvs_flash_init failed due to Storage init error. This issue
exhibited itself as random failures of NVS CI test.

With this change, storage object is deleted (and not added to storage
list) if initialization fails.

components/nvs_flash/src/nvs_api.cpp

index dba8e115a0364d777032e958f2bd84379fb10616..3502c6ca106178b66eb7b4e0c50843dc9956325d 100644 (file)
@@ -88,15 +88,22 @@ extern "C" void nvs_dump(const char *partName)
 extern "C" esp_err_t nvs_flash_init_custom(const char *partName, uint32_t baseSector, uint32_t sectorCount)
 {
     ESP_LOGD(TAG, "nvs_flash_init_custom partition=%s start=%d count=%d", partName, baseSector, sectorCount);
-    nvs::Storage* mStorage;
-
-    mStorage = lookup_storage_from_name(partName);
-    if (mStorage == NULL) {
-        mStorage = new nvs::Storage((const char *)partName);
-        s_nvs_storage_list.push_back(mStorage);
+    nvs::Storage* new_storage = NULL;
+    nvs::Storage* storage = lookup_storage_from_name(partName);
+    if (storage == NULL) {
+        new_storage = new nvs::Storage((const char *)partName);
+        storage = new_storage;
     }
 
-    return mStorage->init(baseSector, sectorCount);
+    esp_err_t err = storage->init(baseSector, sectorCount);
+    if (new_storage != NULL) {
+        if (err == ESP_OK) {
+            s_nvs_storage_list.push_back(new_storage);
+        } else {
+            delete new_storage;
+        }
+    }
+    return err;
 }
 
 #ifdef ESP_PLATFORM