Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1682750 > unrolled thread

[PATCH] iio: multiplexer: add NULL check on devm_kzalloc() return value

Started by"Gustavo A. R. Silva" <garsilva@embeddedor.com>
First post2017-07-07 00:10 +0200
Last post2017-07-07 07:00 +0200
Articles 3 — 1 participant

Back to article view | Back to linux.kernel


Contents

  [PATCH] iio: multiplexer: add NULL check on devm_kzalloc() return  value "Gustavo A. R. Silva" <garsilva@embeddedor.com> - 2017-07-07 00:10 +0200
    Re: [PATCH] iio: multiplexer: add NULL check on devm_kzalloc()  return value "Gustavo A. R. Silva" <garsilva@embeddedor.com> - 2017-07-07 06:40 +0200
      Re: [PATCH] iio: multiplexer: add NULL check on devm_kzalloc()  return value "Gustavo A. R. Silva" <garsilva@embeddedor.com> - 2017-07-07 07:00 +0200

#1682750 — [PATCH] iio: multiplexer: add NULL check on devm_kzalloc() return value

From"Gustavo A. R. Silva" <garsilva@embeddedor.com>
Date2017-07-07 00:10 +0200
Subject[PATCH] iio: multiplexer: add NULL check on devm_kzalloc() return value
Message-ID<u0n8e-3bI-19@gated-at.bofh.it>
Check return value from call to devm_kzalloc()
in order to prevent a NULL pointer dereference.

This issue was detected using Coccinelle and the following semantic patch:

@@
expression x;
identifier fld;
@@

* x = devm_kzalloc(...);
  ... when != x == NULL
  x->fld


Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
---
 drivers/iio/multiplexer/iio-mux.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/iio/multiplexer/iio-mux.c b/drivers/iio/multiplexer/iio-mux.c
index 37ba007..a8d672b 100644
--- a/drivers/iio/multiplexer/iio-mux.c
+++ b/drivers/iio/multiplexer/iio-mux.c
@@ -285,6 +285,9 @@ static int mux_configure_channel(struct device *dev, struct mux *mux,
 	child->ext_info_cache = devm_kzalloc(dev,
 					     sizeof(*child->ext_info_cache) *
 					     num_ext_info, GFP_KERNEL);
+	if (!child->ext_info_cache)
+		return -ENOMEM;
+
 	for (i = 0; i < num_ext_info; ++i) {
 		child->ext_info_cache[i].size = -1;
 
-- 
2.5.0

[toc] | [next] | [standalone]


#1682900 — Re: [PATCH] iio: multiplexer: add NULL check on devm_kzalloc() return value

From"Gustavo A. R. Silva" <garsilva@embeddedor.com>
Date2017-07-07 06:40 +0200
SubjectRe: [PATCH] iio: multiplexer: add NULL check on devm_kzalloc() return value
Message-ID<u0tdD-7dv-3@gated-at.bofh.it>
In reply to#1682750
Hi Peter,

Quoting Peter Rosin <peda@axentia.se>:

> On 2017-07-07 00:08, Gustavo A. R. Silva wrote:
>> Check return value from call to devm_kzalloc()
>> in order to prevent a NULL pointer dereference.
>
> Right, thanks for finding that one! There's another one inside the
> for loop that is just starting in the context of this patch. Care
> to fix checking the return value of that devm_kmemdup as well?
>

Sure, I'll send a new patch shortly.

> And someone should perhaps teach Coccinelle about devm_kmemdup...
>

Good catch, I just implemented that script.

>> This issue was detected using Coccinelle and the following semantic patch:
>>
>> @@
>> expression x;
>> identifier fld;
>> @@
>>
>> * x = devm_kzalloc(...);
>>   ... when != x == NULL
>>   x->fld
>>
>>
>
> One of these blank lines should perhaps be a "Fixes:" tag?
>

mmm, I don't get this...

> Cheers,
> peda
>
>> Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
>> ---
>>  drivers/iio/multiplexer/iio-mux.c | 3 +++
>>  1 file changed, 3 insertions(+)
>>
>> diff --git a/drivers/iio/multiplexer/iio-mux.c  
>> b/drivers/iio/multiplexer/iio-mux.c
>> index 37ba007..a8d672b 100644
>> --- a/drivers/iio/multiplexer/iio-mux.c
>> +++ b/drivers/iio/multiplexer/iio-mux.c
>> @@ -285,6 +285,9 @@ static int mux_configure_channel(struct device  
>> *dev, struct mux *mux,
>>  	child->ext_info_cache = devm_kzalloc(dev,
>>  					     sizeof(*child->ext_info_cache) *
>>  					     num_ext_info, GFP_KERNEL);
>> +	if (!child->ext_info_cache)
>> +		return -ENOMEM;
>> +
>>  	for (i = 0; i < num_ext_info; ++i) {
>>  		child->ext_info_cache[i].size = -1;
>>
>>

Thanks!
--
Gustavo A. R. Silva

[toc] | [prev] | [next] | [standalone]


#1682911 — Re: [PATCH] iio: multiplexer: add NULL check on devm_kzalloc() return value

From"Gustavo A. R. Silva" <garsilva@embeddedor.com>
Date2017-07-07 07:00 +0200
SubjectRe: [PATCH] iio: multiplexer: add NULL check on devm_kzalloc() return value
Message-ID<u0twZ-7l9-5@gated-at.bofh.it>
In reply to#1682900
Quoting Peter Rosin <peda@axentia.se>:

> On 2017-07-07 06:35, Gustavo A. R. Silva wrote:
>> Hi Peter,
>>
>> Quoting Peter Rosin <peda@axentia.se>:
>>
>>> On 2017-07-07 00:08, Gustavo A. R. Silva wrote:
>>>> Check return value from call to devm_kzalloc()
>>>> in order to prevent a NULL pointer dereference.
>>>
>>> Right, thanks for finding that one! There's another one inside the
>>> for loop that is just starting in the context of this patch. Care
>>> to fix checking the return value of that devm_kmemdup as well?
>>>
>>
>> Sure, I'll send a new patch shortly.
>>
>>> And someone should perhaps teach Coccinelle about devm_kmemdup...
>>>
>>
>> Good catch, I just implemented that script.
>>
>>>> This issue was detected using Coccinelle and the following semantic patch:
>>>>
>>>> @@
>>>> expression x;
>>>> identifier fld;
>>>> @@
>>>>
>>>> * x = devm_kzalloc(...);
>>>>   ... when != x == NULL
>>>>   x->fld
>>>>
>>>>
>>>
>>> One of these blank lines should perhaps be a "Fixes:" tag?
>>>
>>
>> mmm, I don't get this...
>
> If you add a Fixes-tag, like below, you help the stable kernel maintainers
> decide what to look at. In this case it might be overkill since the thing
> you fix is so fresh and does not apply to any old kernel. But I think it
> is a good habit...
>
> Fixes: 7ba9df54b091 ("iio: multiplexer: new iio category and iio-mux driver")
>
> (and it is a bit unusual to see two blank lines before the SoB-tag)
>
> Sorry for not spelling it out the first time.
>

Oh, I get it now. Thank you very much for clarifying. I will  
definitely keep that in mind for future fixes when that could apply.

Thanks!
--
Gustavo A. R. Silva

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web