Skip to content

Commit f1e5b62

Browse files
GustavoARSilvaliu-song-6
authored andcommitted
md-linear: use struct_size() in kzalloc()
One of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example: struct foo { int stuff; void *entry[]; }; instance = kzalloc(sizeof(struct foo) + sizeof(void *) * count, GFP_KERNEL); Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper: instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL); This code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com> Signed-off-by: Song Liu <songliubraving@fb.com>
1 parent bb94aea commit f1e5b62

1 file changed

Lines changed: 1 addition & 2 deletions

File tree

‎drivers/md/md-linear.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks)
9696
int i, cnt;
9797
bool discard_supported = false;
9898

99-
conf = kzalloc (sizeof (*conf) + raid_disks*sizeof(struct dev_info),
100-
GFP_KERNEL);
99+
conf = kzalloc(struct_size(conf, disks, raid_disks), GFP_KERNEL);
101100
if (!conf)
102101
return NULL;
103102

0 commit comments

Comments
 (0)