Skip to content

Commit 3ef4600

Browse files
Erick Archermiquelraynal
authored andcommitted
mtd: maps: sa1100-flash: 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][2]. As the "info" variable is a pointer to "struct sa_info" and this structure ends in a flexible array: struct sa_info { [...] struct sa_subdev_info subdev[]; }; the preferred way in the kernel is to use the struct_size() helper to do the arithmetic instead of the calculation "size + size * count" in the kzalloc() function. This way, the code is more readable and safer. This code was detected with the help of Coccinelle, and audited and modified manually. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1] Link: KSPP#160 [2] Signed-off-by: Erick Archer <[email protected]> Signed-off-by: Miquel Raynal <[email protected]> Link: https://lore.kernel.org/linux-mtd/AS8PR02MB7237AC633B0D1D2EBD3C40E98B392@AS8PR02MB7237.eurprd02.prod.outlook.com
1 parent 5043e55 commit 3ef4600

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

drivers/mtd/maps/sa1100-flash.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ static struct sa_info *sa1100_setup_mtd(struct platform_device *pdev,
153153
struct flash_platform_data *plat)
154154
{
155155
struct sa_info *info;
156-
int nr, size, i, ret = 0;
156+
int nr, i, ret = 0;
157157

158158
/*
159159
* Count number of devices.
@@ -167,12 +167,10 @@ static struct sa_info *sa1100_setup_mtd(struct platform_device *pdev,
167167
goto out;
168168
}
169169

170-
size = sizeof(struct sa_info) + sizeof(struct sa_subdev_info) * nr;
171-
172170
/*
173171
* Allocate the map_info structs in one go.
174172
*/
175-
info = kzalloc(size, GFP_KERNEL);
173+
info = kzalloc(struct_size(info, subdev, nr), GFP_KERNEL);
176174
if (!info) {
177175
ret = -ENOMEM;
178176
goto out;

0 commit comments

Comments
 (0)