mtd: rawnand: Prefer struct_size over open coded arithmetic
authorErick Archer <erick.archer@gmx.com>
Sun, 11 Feb 2024 09:16:33 +0000 (10:16 +0100)
committerMiquel Raynal <miquel.raynal@bootlin.com>
Mon, 26 Feb 2024 10:36:17 +0000 (11:36 +0100)
This is an effort to get rid of all multiplications from allocation
functions in order to prevent integer overflows [1].

As the "chip" variable is a pointer to "struct mtk_nfc_nand_chip" and
this structure ends in a flexible array:

struct mtk_nfc_nand_chip {
[...]
u8 sels[] __counted_by(nsels);
};

the preferred way in the kernel is to use the struct_size() helper to
do the arithmetic instead of the argument "size + count * size" in the
devm_kzalloc() function.

This way, the code is more readable and safer.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments
Link: https://github.com/KSPP/linux/issues/160
Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Erick Archer <erick.archer@gmx.com>
Acked-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Reviewed-by: Matthias Brugger <matthias.bgg@gmail.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20240211091633.4545-1-erick.archer@gmx.com
drivers/mtd/nand/raw/mtk_nand.c

index 60198e3..17477bb 100644 (file)
@@ -1356,7 +1356,7 @@ static int mtk_nfc_nand_chip_init(struct device *dev, struct mtk_nfc *nfc,
                return -EINVAL;
        }
 
-       chip = devm_kzalloc(dev, sizeof(*chip) + nsels * sizeof(u8),
+       chip = devm_kzalloc(dev, struct_size(chip, sels, nsels),
                            GFP_KERNEL);
        if (!chip)
                return -ENOMEM;