]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
mtd: jedec_probe: use struct_size() helper for cfiq allocation
authorRahul Kumar <rk0006818@gmail.com>
Mon, 22 Sep 2025 07:11:37 +0000 (12:41 +0530)
committerMiquel Raynal <miquel.raynal@bootlin.com>
Mon, 29 Sep 2025 16:01:43 +0000 (18:01 +0200)
Documentation/process/deprecated.rst recommends against performing
dynamic size calculations in the arguments of memory allocator
functions due to the risk of overflow. Such calculations can wrap
around and result in a smaller allocation than expected.

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

Signed-off-by: Rahul Kumar <rk0006818@gmail.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
drivers/mtd/chips/jedec_probe.c

index 3648348e92bedc9155c3def7ef3e8068b1c16fde..b285962eee2a92d14ad1d16644a1673363c4e62a 100644 (file)
@@ -1985,7 +1985,7 @@ static int cfi_jedec_setup(struct map_info *map, struct cfi_private *cfi, int in
 
        num_erase_regions = jedec_table[index].nr_regions;
 
-       cfi->cfiq = kmalloc(sizeof(struct cfi_ident) + num_erase_regions * 4, GFP_KERNEL);
+       cfi->cfiq = kmalloc(struct_size(cfi->cfiq, EraseRegionInfo, num_erase_regions), GFP_KERNEL);
        if (!cfi->cfiq) {
                //xx printk(KERN_WARNING "%s: kmalloc failed for CFI ident structure\n", map->name);
                return 0;