Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mtd: diskonchip: work around ubsan link failure

I ran into a randconfig build failure with UBSAN using gcc-13.2:

arm-linux-gnueabi-ld: error: unplaced orphan section `.bss..Lubsan_data31' from `drivers/mtd/nand/raw/diskonchip.o'

I'm not entirely sure what is going on here, but I suspect this has something
to do with the check for the end of the doc_locations[] array that contains
an (unsigned long)0xffffffff element, which is compared against the signed
(int)0xffffffff. If this is the case, we should get a runtime check for
undefined behavior, but we instead get an unexpected build-time error.

I would have expected this to work fine on 32-bit architectures despite the
signed integer overflow, though on 64-bit architectures this likely won't
ever work.

Changing the contition to instead check for the size of the array makes the
code safe everywhere and avoids the ubsan check that leads to the link
error. The loop code goes back to before 2.6.12.

Cc: stable@vger.kernel.org
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20240405143015.717429-1-arnd@kernel.org

authored by

Arnd Bergmann and committed by
Miquel Raynal
21c9fb61 b61bb5bc

+2 -2
+2 -2
drivers/mtd/nand/raw/diskonchip.c
··· 53 53 0xe8000, 0xea000, 0xec000, 0xee000, 54 54 #endif 55 55 #endif 56 - 0xffffffff }; 56 + }; 57 57 58 58 static struct mtd_info *doclist = NULL; 59 59 ··· 1554 1554 if (ret < 0) 1555 1555 return ret; 1556 1556 } else { 1557 - for (i = 0; (doc_locations[i] != 0xffffffff); i++) { 1557 + for (i = 0; i < ARRAY_SIZE(doc_locations); i++) { 1558 1558 doc_probe(doc_locations[i]); 1559 1559 } 1560 1560 }