From cc944b0e9f8d4675d62c88f079e9ab518f5d5174 Mon Sep 17 00:00:00 2001 From: Ming Hu Date: Tue, 17 Nov 2026 09:42:07 +0800 Subject: [PATCH] fix(kvdb): skip a status-only first KV header when caching free space An interrupted first KV write can leave the trailing-FF boundary inside its status table. Caching this boundary as free space allows recovery to allocate a replacement inside the incomplete header. A subsequent search can mark the residual header ERR_HDR at a byte that also belongs to the replacement's DELETED slot. Committing WRITE does not clear that marker, and deleting the source leaves no readable copy. Advance the initial cached free address past the complete first KV header when the FF boundary falls within its status-table interval. Keep the cache-hit condition, capacity calculation, and on-flash format unchanged, without adding flash reads or writes. --- src/fdb_kvdb.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/fdb_kvdb.c b/src/fdb_kvdb.c index ad75d22..80b94ea 100644 --- a/src/fdb_kvdb.c +++ b/src/fdb_kvdb.c @@ -516,6 +516,11 @@ static fdb_err_t read_sector_info(fdb_kvdb_t db, uint32_t addr, kv_sec_info_t se #endif sector->empty_kv = _fdb_continue_ff_addr((fdb_db_t)db, sector->addr + SECTOR_HDR_DATA_SIZE, sector->addr + db_sec_size(db)); + /* Skip a status-only first KV header. */ + if (sector->empty_kv > sector->addr + SECTOR_HDR_DATA_SIZE && + sector->empty_kv <= sector->addr + SECTOR_HDR_DATA_SIZE + KV_MAGIC_OFFSET) { + sector->empty_kv = sector->addr + SECTOR_HDR_DATA_SIZE + KV_HDR_DATA_SIZE; + } sector->remain = db_sec_size(db) - (sector->empty_kv - sector->addr); } else { sector->empty_kv = FAILED_ADDR;