Skip to contentSkip to conversation

mtd: rawnand: Prefer struct_size over open coded arithmetic

PATCH Superseded Erick Archer 4 participants 4 messages Feb 10, 2024 – Feb 12, 2024

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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 more safer.

---
 drivers/mtd/nand/raw/mtk_nand.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/nand/raw/mtk_nand.c +1 -1
index 60198e33d2d5..17477bb2d48f 100644
@@ -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;

--
2.25.1

Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Erick Archer

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.

Quoted 5 lines

This way, the code is more readable and more safer.

LGTM:

Thanks!

Signature · 1 line
Gustavo
Quoted 21 lines
---
  drivers/mtd/nand/raw/mtk_nand.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/nand/raw/mtk_nand.c +1 -2
index 60198e33d2d5..17477bb2d48f 100644
@@ -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;
--

2.25.1

#2 lore raw
In reply to Erick Archer
Quoted 11 lines · Erick Archer

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);
};
Quoted 5 lines

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 more safer.

Unless you intend this non-English comparative: s/more safer/safer/

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Erick Archer

Il 10/02/24 17:16, Erick Archer ha scritto:

Quoted 11 lines

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);
};
Quoted 5 lines

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 more safer.

This way, the code is more readable and safer.

Apart from that,

Quoted 24 lines
---
  drivers/mtd/nand/raw/mtk_nand.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/nand/raw/mtk_nand.c +1 -2
index 60198e33d2d5..17477bb2d48f 100644
@@ -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;
--

2.25.1