Commit 350192f4 authored by Gustavo A. R. Silva's avatar Gustavo A. R. Silva Committed by Greg Kroah-Hartman
Browse files

clk: npcm7xx: fix memory allocation

commit 450b6b9b


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,

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.

Signed-off-by: default avatarGustavo A. R. Silva <>
Reviewed-by: default avatarKees Cook <>
Reviewed-by: default avatarAvi Fishman <>
Signed-off-by: default avatarStephen Boyd <>
Signed-off-by: default avatarGreg Kroah-Hartman <>
parent a8b0c3c7
......@@ -558,8 +558,8 @@ static void __init npcm7xx_clk_init(struct device_node *clk_np)
if (!clk_base)
goto npcm7xx_init_error;
npcm7xx_clk_data = kzalloc(sizeof(*npcm7xx_clk_data->hws) *
NPCM7XX_NUM_CLOCKS + sizeof(npcm7xx_clk_data), GFP_KERNEL);
npcm7xx_clk_data = kzalloc(struct_size(npcm7xx_clk_data, hws,
if (!npcm7xx_clk_data)
goto npcm7xx_init_np_err;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment