[PATCH 06/15] mtd: cfi-flash: Fix compiler warning

Sascha Hauer s.hauer at pengutronix.de
Mon May 17 11:54:15 PDT 2021


sector >= 0 is always true for the unsigned type flash_sect_t. This
means the loop to find the sector will only behave correctly when we
actually find the sector, but not in the error case. The error case
is not expected though and will not happen when the code is correct,
so just catch it with a BUG().

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 drivers/mtd/nor/cfi_flash.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/nor/cfi_flash.c b/drivers/mtd/nor/cfi_flash.c
index ba0bd1b4eb..ffd29d80a7 100644
--- a/drivers/mtd/nor/cfi_flash.c
+++ b/drivers/mtd/nor/cfi_flash.c
@@ -466,9 +466,16 @@ flash_sect_t find_sector(struct flash_info *info, unsigned long addr)
 {
 	flash_sect_t sector;
 
-	for (sector = info->sector_count - 1; sector >= 0; sector--) {
+	sector = info->sector_count - 1;
+
+	while (1) {
 		if (addr >= info->start[sector])
 			break;
+
+		if (sector == 0)
+			BUG();
+
+		sector--;
 	}
 
 	return sector;
-- 
2.29.2




More information about the barebox mailing list