Skip to content

Commit 450b6b9

Browse files
GustavoARSilvabebarino
authored andcommitted
clk: npcm7xx: fix memory allocation
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); Notice that, currently, there is a bug during the allocation: sizeof(npcm7xx_clk_data) should be sizeof(*npcm7xx_clk_data) Fix this bug by using struct_size() in kzalloc() This issue was detected with the help of Coccinelle. Cc: stable@vger.kernel.org Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com> Reviewed-by: Kees Cook <keescook@chromium.org> Reviewed-by: Avi Fishman <avifishman70@gmail.com> Signed-off-by: Stephen Boyd <sboyd@kernel.org>
1 parent 5b394b2 commit 450b6b9

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

drivers/clk/clk-npcm7xx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,8 @@ static void __init npcm7xx_clk_init(struct device_node *clk_np)
558558
if (!clk_base)
559559
goto npcm7xx_init_error;
560560

561-
npcm7xx_clk_data = kzalloc(sizeof(*npcm7xx_clk_data->hws) *
562-
NPCM7XX_NUM_CLOCKS + sizeof(npcm7xx_clk_data), GFP_KERNEL);
561+
npcm7xx_clk_data = kzalloc(struct_size(npcm7xx_clk_data, hws,
562+
NPCM7XX_NUM_CLOCKS), GFP_KERNEL);
563563
if (!npcm7xx_clk_data)
564564
goto npcm7xx_init_np_err;
565565

0 commit comments

Comments
 (0)