2015-12-16 18:28:43

by Brian Norris

[permalink] [raw]
Subject: [PATCH] clk: gpio: fix memory leak in of_gpio_clk_setup()

Reported by Coverity.

Fixes: f66541ba02d5 ("clk: gpio: Get parent clk names in of_gpio_clk_setup()")
Signed-off-by: Brian Norris <[email protected]>
Cc: Jyri Sarha <[email protected]>
Cc: Sergej Sawazki <[email protected]>
Cc: Stephen Boyd <[email protected]>
---
drivers/clk/clk-gpio.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/clk-gpio.c b/drivers/clk/clk-gpio.c
index 335322dc403f..c1baa89cbf91 100644
--- a/drivers/clk/clk-gpio.c
+++ b/drivers/clk/clk-gpio.c
@@ -294,8 +294,10 @@ static void __init of_gpio_clk_setup(struct device_node *node,
num_parents = of_clk_get_parent_count(node);

parent_names = kcalloc(num_parents, sizeof(char *), GFP_KERNEL);
- if (!parent_names)
+ if (!parent_names) {
+ kfree(data);
return;
+ }

for (i = 0; i < num_parents; i++)
parent_names[i] = of_clk_get_parent_name(node, i);
--
2.6.0.rc2.230.g3dd15c0


2015-12-16 18:35:26

by Brian Norris

[permalink] [raw]
Subject: [PATCH] clk: gpio: handle error codes for of_clk_get_parent_count()

We might make bad memory allocations if we get (e.g.) -ENOSYS from
of_clk_get_parent_count().

Noticed by Coverity.

Fixes: f66541ba02d5 ("clk: gpio: Get parent clk names in of_gpio_clk_setup()")
Signed-off-by: Brian Norris <[email protected]>
Cc: Jyri Sarha <[email protected]>
Cc: Sergej Sawazki <[email protected]>
Cc: Stephen Boyd <[email protected]>
---
Sorry, I noticed Coverity was complaining about 1 more.

drivers/clk/clk-gpio.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/clk/clk-gpio.c b/drivers/clk/clk-gpio.c
index c1baa89cbf91..1767b9e3ca93 100644
--- a/drivers/clk/clk-gpio.c
+++ b/drivers/clk/clk-gpio.c
@@ -287,12 +287,14 @@ static void __init of_gpio_clk_setup(struct device_node *node,
const char **parent_names;
int i, num_parents;

+ num_parents = of_clk_get_parent_count(node);
+ if (num_parents < 0)
+ return;
+
data = kzalloc(sizeof(*data), GFP_KERNEL);
if (!data)
return;

- num_parents = of_clk_get_parent_count(node);
-
parent_names = kcalloc(num_parents, sizeof(char *), GFP_KERNEL);
if (!parent_names) {
kfree(data);
--
2.6.0.rc2.230.g3dd15c0