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

mtd: cfi: use struct_size() helper for cfiq allocation

Documentation/process/deprecated.rst recommends against performing
dynamic size calculations in the arguments of memory allocator
function due to the risk of overflow. Such calculations can
wrap around and result in a smaller allocation than what the caller
was expecting.

Replace the size calculation in cfiq allocation with struct_size()
helper to make the code clearer and handle the overflows correctly.

Signed-off-by: Gopi Krishna Menon <krishnagopi487@gmail.com>
Reviewed-by: Vignesh Raghavendra <vigneshr@ti.com>
link: https://lore.kernel.org/linux-kernel-mentees/20250922071137.900508-1-rk0006818@gmail.com/T/#u
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>

authored by

Gopi Krishna Menon and committed by
Miquel Raynal
d496b6f4 9781c381

+1 -1
+1 -1
drivers/mtd/chips/cfi_probe.c
··· 208 208 if (!num_erase_regions) 209 209 return 0; 210 210 211 - cfi->cfiq = kmalloc(sizeof(struct cfi_ident) + num_erase_regions * 4, GFP_KERNEL); 211 + cfi->cfiq = kmalloc(struct_size(cfi->cfiq, EraseRegionInfo, num_erase_regions), GFP_KERNEL); 212 212 if (!cfi->cfiq) 213 213 return 0; 214 214