]> granicus.if.org Git - esp-idf/commitdiff
nvs_flash: Multi-page blob erased using nvs_erase_key should be cleaned properly
authornegativekelvin <negativekelvin@github.com>
Wed, 27 Mar 2019 14:44:56 +0000 (20:14 +0530)
committerbot <bot@espressif.com>
Sun, 28 Apr 2019 03:49:30 +0000 (03:49 +0000)
Earlier eraseItem function in Storage class would do lazy cleanup of
multi-page blobs if called using type "ANY" instead of "BLOB". It used to
just delete BLOB data and index would remain as is. Any subsequent read
would delete index entry as well. This however would return a valid
length without error if nvs_get_blob API was just used for finding
length and not reading the complete blob. This change fixes this issue.

Closes https://github.com/espressif/esp-idf/issues/3255

components/nvs_flash/src/nvs_storage.cpp
components/nvs_flash/test_nvs_host/test_nvs.cpp

index 5cb34b27a169b4289c40fc77beaf6a6ed87d9a0b..d1b4b01bebb8222e96ed5545907945c3103e5b3f 100644 (file)
@@ -593,6 +593,12 @@ esp_err_t Storage::eraseItem(uint8_t nsIndex, ItemType datatype, const char* key
         return err;
     }
 
+#ifdef CONFIG_MP_BLOB_SUPPORT
+    if (item.datatype == ItemType::BLOB_DATA || item.datatype == ItemType::BLOB_IDX) {
+        return eraseMultiPageBlob(nsIndex, key);
+    }
+#endif 
+
     return findPage->eraseItem(nsIndex, datatype, key);
 }
 
index cbdd3daaf25a330d6abf8c24315a01b8a9a708c2..287c8cb9775454ee9b5403ed02433881cf99a298 100644 (file)
@@ -1776,6 +1776,22 @@ TEST_CASE("Modification from  multi-page to single page", "[nvs]")
     nvs_close(handle);
 }
 
+TEST_CASE("Multi-page blob erased using nvs_erase_key should not be found when probed for just length", "[nvs]")
+{
+    const size_t blob_size = Page::CHUNK_MAX_SIZE *3;
+    uint8_t blob[blob_size] = {0};
+    size_t read_size = blob_size;
+    SpiFlashEmulator emu(5);
+    TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, 0, 5));
+    nvs_handle handle;
+    TEST_ESP_OK(nvs_open("Test", NVS_READWRITE, &handle));
+    TEST_ESP_OK(nvs_set_blob(handle, "abc", blob, blob_size));
+    TEST_ESP_OK(nvs_erase_key(handle, "abc"));
+    TEST_ESP_ERR(nvs_get_blob(handle, "abc", NULL, &read_size), ESP_ERR_NVS_NOT_FOUND); 
+    TEST_ESP_OK(nvs_commit(handle));
+    nvs_close(handle);
+}
+
 
 TEST_CASE("Check that orphaned blobs are erased during init", "[nvs]")
 {