Skip to content

Commit e395036

Browse files
Erick Archermiquelraynal
authored andcommitted
mtd: rawnand: Prefer struct_size over open coded arithmetic
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 [1] Link: KSPP#160 [2] Reviewed-by: Gustavo A. R. Silva <[email protected]> Signed-off-by: Erick Archer <[email protected]> Acked-by: Uwe Kleine-König <[email protected]> Reviewed-by: AngeloGioacchino Del Regno <[email protected]> Reviewed-by: Matthias Brugger <[email protected]> Signed-off-by: Miquel Raynal <[email protected]> Link: https://lore.kernel.org/linux-mtd/[email protected]
1 parent ef6f463 commit e395036

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

drivers/mtd/nand/raw/mtk_nand.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,7 @@ static int mtk_nfc_nand_chip_init(struct device *dev, struct mtk_nfc *nfc,
13561356
return -EINVAL;
13571357
}
13581358

1359-
chip = devm_kzalloc(dev, sizeof(*chip) + nsels * sizeof(u8),
1359+
chip = devm_kzalloc(dev, struct_size(chip, sels, nsels),
13601360
GFP_KERNEL);
13611361
if (!chip)
13621362
return -ENOMEM;

0 commit comments

Comments
 (0)