From a9fbfad1a6d4132cea9250300cfa980c1a992559 Mon Sep 17 00:00:00 2001 From: Dirk Petrautzki Date: Fri, 25 Sep 2026 22:09:17 +0200 Subject: [PATCH] Fixed uninitialized mapped sector state in NOR erase search The NOR erase search can obtain an obsolete count from the cache or block metadata without scanning the mapping table. It then copied an uninitialized mapped count into candidate state, which GCC could report as maybe-uninitialized. The search now initializes the availability flag and resets the mapped count for each block. It still scans the chosen block before returning a mapped count when one was unavailable. A simulator regression checks a cached block with one obsolete and fourteen mapped sectors. GCC 15 with -O2 -Werror=maybe-uninitialized rejected the base revision and built the change. CMake/Ninja regression tests passed 3/3 in each of the obsolete count cache, combined cache, and default coverage configurations. Not run on hardware. Assisted-by: Codex (GPT-6) --- .../lx_nor_flash_next_block_to_erase_find.c | 8 ++-- test/regression/levelx_nor_flash_test_cache.c | 38 +++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/common/src/lx_nor_flash_next_block_to_erase_find.c b/common/src/lx_nor_flash_next_block_to_erase_find.c index b57b0c5..77ac3e6 100644 --- a/common/src/lx_nor_flash_next_block_to_erase_find.c +++ b/common/src/lx_nor_flash_next_block_to_erase_find.c @@ -103,7 +103,7 @@ ULONG max_logical_sector; UINT status; #endif UINT obsolete_sectors_available; -UINT mapped_sectors_available; +UINT mapped_sectors_available = LX_FALSE; /* Setup the block word pointer to the first word of the search block. */ @@ -171,9 +171,10 @@ UINT mapped_sectors_available; if (erase_count > max_system_block_erase_count) max_system_block_erase_count = erase_count; - /* Initialize the obsolete and mapped sector count available flags. */ + /* Initialize the mapped sector count and the obsolete/mapped sector count availability flags. */ obsolete_sectors_available = LX_FALSE; mapped_sectors_available = LX_FALSE; + mapped_sectors = 0; #ifdef LX_NOR_ENABLE_OBSOLETE_COUNT_CACHE @@ -469,5 +470,4 @@ UINT mapped_sectors_available; } /* Return success. */ return(LX_SUCCESS); -} - +} \ No newline at end of file diff --git a/test/regression/levelx_nor_flash_test_cache.c b/test/regression/levelx_nor_flash_test_cache.c index d0b24d0..d5f595b 100644 --- a/test/regression/levelx_nor_flash_test_cache.c +++ b/test/regression/levelx_nor_flash_test_cache.c @@ -9,6 +9,8 @@ /* SPDX-License-Identifier: MIT */ /***************************************************************************/ +/* Portions of this file were generated with AI assistance. */ + /* Basic NOR flash tests... */ #include @@ -82,6 +84,12 @@ ULONG i, j, sector; UINT status; ULONG *word_ptr; +#ifdef LX_NOR_ENABLE_OBSOLETE_COUNT_CACHE +ULONG erase_block = LX_ALL_ONES; +ULONG erase_count = LX_ALL_ONES; +ULONG mapped_sectors = LX_ALL_ONES; +ULONG obsolete_sectors = LX_ALL_ONES; +#endif /* Initialize LevelX. */ @@ -1660,6 +1668,36 @@ status += lx_nor_flash_extended_cache_enable(&nor_sim_flash, nor_cache_memory2, } } +#ifdef LX_NOR_ENABLE_OBSOLETE_COUNT_CACHE + /* Verify the mapped sector count when the obsolete count comes from the cache. */ + printf("\nTest 7: Cached obsolete count with mapped sectors.."); + + status = lx_nor_flash_sector_release(&nor_sim_flash, 0); + if (status == LX_SUCCESS) + { + status = _lx_nor_flash_next_block_to_erase_find(&nor_sim_flash, &erase_block, &erase_count, + &mapped_sectors, &obsolete_sectors); + } + + if ((status != LX_SUCCESS) || + (nor_sim_flash.lx_nor_flash_extended_cache_obsolete_count_max_block == 0) || + (nor_sim_flash.lx_nor_flash_extended_cache_obsolete_count[0] != 1) || + (erase_block != 0) || + (erase_count != nor_sim_flash.lx_nor_flash_minimum_erase_count) || + (mapped_sectors != (nor_sim_flash.lx_nor_flash_physical_sectors_per_block - 1)) || + (obsolete_sectors != 1)) + { + printf("FAILED!\n"); +#ifdef BATCH_TEST + exit(1); +#endif + while(1) + { + } + } + printf("SUCCESS!\n"); +#endif + #ifdef BATCH_TEST exit(0); #endif