Skip to contentSkip to conversation

clk: npcm7xx: fix memory allocation

PATCH Applied Gustavo A. R. Silva 4 participants 4 messages Aug 23, 2018 – Aug 28, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 drivers/clk/clk-npcm7xx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/clk-npcm7xx.c +2 -2
index 740af90..c5edf8f 100644
@@ -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,
+				   NPCM7XX_NUM_CLOCKS), GFP_KERNEL);
 	if (!npcm7xx_clk_data)
 		goto npcm7xx_init_np_err;
 
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Gustavo A. R. Silva

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:

Quoted 8 lines

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.

-Kees

Quoted 22 lines
---
 drivers/clk/clk-npcm7xx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/clk-npcm7xx.c +2 -3
index 740af90..c5edf8f 100644
@@ -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,
+                                  NPCM7XX_NUM_CLOCKS), GFP_KERNEL);
        if (!npcm7xx_clk_data)
                goto npcm7xx_init_np_err;
 
--

2.7.4

Signature · 2 lines
Kees Cook
Pixel Security
maintainer Reviewed-by
#2 lore raw
Quoted 24 lines · Kees Cook
Quoted 21 lines · Gustavo A. R. Silva

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)

Quoted 8 lines
Quoted 6 lines

Fix this bug by using struct_size() in kzalloc()

This issue was detected with the help of Coccinelle.

Quoted 31 lines

-Kees

Quoted 20 lines
---
 drivers/clk/clk-npcm7xx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/clk-npcm7xx.c +2 -3
index 740af90..c5edf8f 100644
@@ -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,
+                                  NPCM7XX_NUM_CLOCKS), GFP_KERNEL);
        if (!npcm7xx_clk_data)
                goto npcm7xx_init_np_err;
 
--
Quoted 2 lines

2.7.4

--
Kees Cook
Pixel Security

Signature · 2 lines
Regards,
Avi
maintainer Applied
#3 lore raw
In reply to Gustavo A. R. Silva

Quoting Gustavo A. R. Silva (2018-08-23 16:06:54)

Quoted 31 lines

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 =3D 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 =3D 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()
=

Quoted 6 lines

This issue was detected with the help of Coccinelle. =

---

Applied to clk-fixes