Skip to content

Commit 2d65c42

Browse files
GustavoARSilvaKAGA-KOKO
authored andcommitted
genirq/devres: Use struct_size() in devm_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; struct boo entry[]; }; size = sizeof(struct foo) + count * sizeof(struct boo); instance = devm_kzalloc(dev, size, GFP_KERNEL); Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper. instance = devm_kzalloc(dev, struct_size(instance, entry, count), GFP_KERNEL); This code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lkml.kernel.org/r/20190410170914.GA16161@embeddedor
1 parent bbba0e7 commit 2d65c42

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

kernel/irq/devres.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,8 @@ devm_irq_alloc_generic_chip(struct device *dev, const char *name, int num_ct,
220220
irq_flow_handler_t handler)
221221
{
222222
struct irq_chip_generic *gc;
223-
unsigned long sz = sizeof(*gc) + num_ct * sizeof(struct irq_chip_type);
224223

225-
gc = devm_kzalloc(dev, sz, GFP_KERNEL);
224+
gc = devm_kzalloc(dev, struct_size(gc, chip_types, num_ct), GFP_KERNEL);
226225
if (gc)
227226
irq_init_generic_chip(gc, name, num_ct,
228227
irq_base, reg_base, handler);

0 commit comments

Comments
 (0)