linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] pinctrl: imx: Check for memory allocation failure
@ 2017-05-06  8:23 Christophe JAILLET
  2017-05-06 19:40 ` Stafford Horne
  2017-05-12  9:11 ` Linus Walleij
  0 siblings, 2 replies; 5+ messages in thread
From: Christophe JAILLET @ 2017-05-06  8:23 UTC (permalink / raw)
  To: linus.walleij, gary.bisson, vladimir_zapolskiy, tony, vivien.didelot
  Cc: linux-gpio, linux-kernel, kernel-janitors, Christophe JAILLET

If 'devm_kzalloc' fails, a NULL pointer will be dereferenced.
Return -ENOMEM instead, as done for the other memory allocation just a
few lines below.
BTW, change the 'devm_kzalloc' into a 'devm_kcalloc'.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
 drivers/pinctrl/freescale/pinctrl-imx.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/pinctrl/freescale/pinctrl-imx.c b/drivers/pinctrl/freescale/pinctrl-imx.c
index 74bd90dfd7b1..90a946c028ff 100644
--- a/drivers/pinctrl/freescale/pinctrl-imx.c
+++ b/drivers/pinctrl/freescale/pinctrl-imx.c
@@ -581,9 +581,10 @@ static int imx_pinctrl_parse_functions(struct device_node *np,
 		dev_err(info->dev, "no groups defined in %s\n", np->full_name);
 		return -EINVAL;
 	}
-	func->group_names = devm_kzalloc(info->dev,
-					 func->num_group_names *
+	func->group_names = devm_kcalloc(info->dev, func->num_group_names,
 					 sizeof(char *), GFP_KERNEL);
+	if (!func->group_names)
+		return -ENOMEM;
 
 	for_each_child_of_node(np, child) {
 		func->group_names[i] = child->name;
-- 
2.11.0

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] pinctrl: imx: Check for memory allocation failure
  2017-05-06  8:23 [PATCH] pinctrl: imx: Check for memory allocation failure Christophe JAILLET
@ 2017-05-06 19:40 ` Stafford Horne
  2017-05-09  7:27   ` Dan Carpenter
  2017-05-12  9:11 ` Linus Walleij
  1 sibling, 1 reply; 5+ messages in thread
From: Stafford Horne @ 2017-05-06 19:40 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: linus.walleij, gary.bisson, vladimir_zapolskiy, tony,
	vivien.didelot, linux-gpio, linux-kernel, kernel-janitors

Hi Christophe,

On Sat, May 06, 2017 at 10:23:59AM +0200, Christophe JAILLET wrote:
> If 'devm_kzalloc' fails, a NULL pointer will be dereferenced.
> Return -ENOMEM instead, as done for the other memory allocation just a
> few lines below.

This looks fine.

> BTW, change the 'devm_kzalloc' into a 'devm_kcalloc'.

Any reason for the devm_kcalloc change?  It looks like the next for loop
does set all of the group_name values.

-Stafford

> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> ---
>  drivers/pinctrl/freescale/pinctrl-imx.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/pinctrl/freescale/pinctrl-imx.c b/drivers/pinctrl/freescale/pinctrl-imx.c
> index 74bd90dfd7b1..90a946c028ff 100644
> --- a/drivers/pinctrl/freescale/pinctrl-imx.c
> +++ b/drivers/pinctrl/freescale/pinctrl-imx.c
> @@ -581,9 +581,10 @@ static int imx_pinctrl_parse_functions(struct device_node *np,
>  		dev_err(info->dev, "no groups defined in %s\n", np->full_name);
>  		return -EINVAL;
>  	}
> -	func->group_names = devm_kzalloc(info->dev,
> -					 func->num_group_names *
> +	func->group_names = devm_kcalloc(info->dev, func->num_group_names,
>  					 sizeof(char *), GFP_KERNEL);
> +	if (!func->group_names)
> +		return -ENOMEM;
>  
>  	for_each_child_of_node(np, child) {
>  		func->group_names[i] = child->name;
> -- 
> 2.11.0
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] pinctrl: imx: Check for memory allocation failure
  2017-05-06 19:40 ` Stafford Horne
@ 2017-05-09  7:27   ` Dan Carpenter
  2017-05-09 12:31     ` Stafford Horne
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2017-05-09  7:27 UTC (permalink / raw)
  To: Stafford Horne
  Cc: Christophe JAILLET, linus.walleij, gary.bisson,
	vladimir_zapolskiy, tony, vivien.didelot, linux-gpio,
	linux-kernel, kernel-janitors

On Sun, May 07, 2017 at 04:40:38AM +0900, Stafford Horne wrote:
> Hi Christophe,
> 
> On Sat, May 06, 2017 at 10:23:59AM +0200, Christophe JAILLET wrote:
> > If 'devm_kzalloc' fails, a NULL pointer will be dereferenced.
> > Return -ENOMEM instead, as done for the other memory allocation just a
> > few lines below.
> 
> This looks fine.
> 
> > BTW, change the 'devm_kzalloc' into a 'devm_kcalloc'.
> 
> Any reason for the devm_kcalloc change?  It looks like the next for loop
> does set all of the group_name values.
> 

The advantage of kcalloc() over kzalloc() is the integer overflow
checking.  There is kmalloc_array() if we don't need to zero the memory.

regards,
dan carpenter

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] pinctrl: imx: Check for memory allocation failure
  2017-05-09  7:27   ` Dan Carpenter
@ 2017-05-09 12:31     ` Stafford Horne
  0 siblings, 0 replies; 5+ messages in thread
From: Stafford Horne @ 2017-05-09 12:31 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Christophe JAILLET, linus.walleij, gary.bisson,
	vladimir_zapolskiy, tony, vivien.didelot, linux-gpio,
	linux-kernel, kernel-janitors

On Tue, May 09, 2017 at 10:27:20AM +0300, Dan Carpenter wrote:
> On Sun, May 07, 2017 at 04:40:38AM +0900, Stafford Horne wrote:
> > Hi Christophe,
> > 
> > On Sat, May 06, 2017 at 10:23:59AM +0200, Christophe JAILLET wrote:
> > > If 'devm_kzalloc' fails, a NULL pointer will be dereferenced.
> > > Return -ENOMEM instead, as done for the other memory allocation just a
> > > few lines below.
> > 
> > This looks fine.
> > 
> > > BTW, change the 'devm_kzalloc' into a 'devm_kcalloc'.
> > 
> > Any reason for the devm_kcalloc change?  It looks like the next for loop
> > does set all of the group_name values.
> > 
> 
> The advantage of kcalloc() over kzalloc() is the integer overflow
> checking.  There is kmalloc_array() if we don't need to zero the memory.

Right, usually its good to have a reason why in the commit log. i.e.

 BTW, change the 'devm_kzalloc' into a 'devm_kcalloc' for overflow checking.

Or switch to devm_kmalloc_array() and say: for overflow checking and no
need for zeroing.

-Stafford

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] pinctrl: imx: Check for memory allocation failure
  2017-05-06  8:23 [PATCH] pinctrl: imx: Check for memory allocation failure Christophe JAILLET
  2017-05-06 19:40 ` Stafford Horne
@ 2017-05-12  9:11 ` Linus Walleij
  1 sibling, 0 replies; 5+ messages in thread
From: Linus Walleij @ 2017-05-12  9:11 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: Gary Bisson, Vladimir Zapolskiy, ext Tony Lindgren,
	Vivien Didelot, linux-gpio, linux-kernel, kernel-janitors

On Sat, May 6, 2017 at 10:23 AM, Christophe JAILLET
<christophe.jaillet@wanadoo.fr> wrote:

> If 'devm_kzalloc' fails, a NULL pointer will be dereferenced.
> Return -ENOMEM instead, as done for the other memory allocation just a
> few lines below.
> BTW, change the 'devm_kzalloc' into a 'devm_kcalloc'.
>
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>

Patch applied.

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-05-12  9:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-06  8:23 [PATCH] pinctrl: imx: Check for memory allocation failure Christophe JAILLET
2017-05-06 19:40 ` Stafford Horne
2017-05-09  7:27   ` Dan Carpenter
2017-05-09 12:31     ` Stafford Horne
2017-05-12  9:11 ` Linus Walleij

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).