diff --git a/components/nvs_flash/src/nvs_pagemanager.cpp b/components/nvs_flash/src/nvs_pagemanager.cpp index d74bda5e4e..c967aa73f1 100644 --- a/components/nvs_flash/src/nvs_pagemanager.cpp +++ b/components/nvs_flash/src/nvs_pagemanager.cpp @@ -235,6 +235,7 @@ esp_err_t PageManager::fillStats(nvs_stats_t& nvsStats) nvsStats.free_entries += mFreePageList.size() * Page::ENTRY_COUNT; // calculate available entries from free entries by applying reserved page size + // avoid overflow of size_t declared available_entries in case of free_entries being too low nvsStats.available_entries = (nvsStats.free_entries >= Page::ENTRY_COUNT) ? nvsStats.free_entries - Page::ENTRY_COUNT : 0; return err; diff --git a/components/nvs_flash/test_apps/main/test_nvs.c b/components/nvs_flash/test_apps/main/test_nvs.c index dec3eaaa59..45abee6831 100644 --- a/components/nvs_flash/test_apps/main/test_nvs.c +++ b/components/nvs_flash/test_apps/main/test_nvs.c @@ -314,7 +314,7 @@ TEST_CASE("calculate used and free space", "[nvs]") TEST_ASSERT_TRUE(stat1.total_entries == stat2.total_entries); TEST_ASSERT_TRUE(stat1.used_entries == 7); - TEST_ASSERT_TRUE(stat1.total_entries == (stat1.used_entries + stat1.free_entries + 126)); // one Page::ENTRY_COUNT is reserved + TEST_ASSERT_TRUE(stat1.total_entries == (stat1.used_entries + stat1.free_entries)); // amount valid pair in namespace 2 size_t h2_count_entries;