From f99e2dc6b2ba7be034ed7d90c7f6a21df83fd720 Mon Sep 17 00:00:00 2001 From: "sonika.rathi" Date: Thu, 16 Jul 2026 09:29:59 +0200 Subject: [PATCH] fix(wear_levelling): shorten zero-size guard comments --- components/wear_levelling/WL_Flash.cpp | 8 ++------ components/wear_levelling/host_test/main/test_wl.cpp | 7 +------ 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/components/wear_levelling/WL_Flash.cpp b/components/wear_levelling/WL_Flash.cpp index f5d58d884e0..cc1f1d589e9 100644 --- a/components/wear_levelling/WL_Flash.cpp +++ b/components/wear_levelling/WL_Flash.cpp @@ -578,10 +578,7 @@ esp_err_t WL_Flash::write(size_t dest_addr, const void *src, size_t size) return ESP_ERR_INVALID_STATE; } if (size == 0) { - // Nothing to do. Guard this explicitly: size is unsigned, so - // `size - 1` below would otherwise wrap around to SIZE_MAX and turn - // "count" into a huge page count, walking far past the caller's - // buffer (see components/wear_levelling/host_test). + // size==0: (size-1) unsigned underflow would OOB the caller buffer. return ESP_OK; } ESP_LOGD(TAG, "%s - dest_addr= 0x%08" PRIx32 ", size= 0x%08" PRIx32 , __func__, (uint32_t) dest_addr, (uint32_t) size); @@ -604,8 +601,7 @@ esp_err_t WL_Flash::read(size_t src_addr, void *dest, size_t size) return ESP_ERR_INVALID_STATE; } if (size == 0) { - // See the matching guard in WL_Flash::write() above: size==0 must - // not be allowed to reach the `size - 1` computation below. + // Same size==0 guard as write(); avoid (size-1) underflow below. return ESP_OK; } ESP_LOGD(TAG, "%s - src_addr= 0x%08" PRIx32 ", size= 0x%08" PRIx32 , __func__, (uint32_t) src_addr, (uint32_t) size); diff --git a/components/wear_levelling/host_test/main/test_wl.cpp b/components/wear_levelling/host_test/main/test_wl.cpp index ce9819a8743..012945df5ab 100644 --- a/components/wear_levelling/host_test/main/test_wl.cpp +++ b/components/wear_levelling/host_test/main/test_wl.cpp @@ -110,12 +110,7 @@ TEST_CASE("write and read with zero size are safe no-ops", "[wear_levelling]") result = wl_mount(partition, &wl_handle); REQUIRE(result == ESP_OK); - // wl_write()/wl_read() do not document size==0 as invalid (analogous to - // POSIX write()/read() with count==0), so it must not be treated as an - // out-of-bounds request. Previously, WL_Flash::write()/read() computed - // `(size - 1) / wl_page_size` without checking for size==0 first; since - // size is unsigned, size==0 wrapped this to a huge page count and walked - // far past the caller-provided buffer. + // Zero-length wl_write/read must be no-ops; size==0 used to underflow (size-1) and OOB the buffer. uint8_t dummy = 0xAA; result = wl_write(wl_handle, 0, &dummy, 0); REQUIRE(result == ESP_OK);