2020-04-06 09:40:38

by Qiujun Huang

[permalink] [raw]
Subject: [PATCH v4] powerpc/powernv: add NULL check after kzalloc in opal_add_one_export

Here needs a NULL check as kzalloc may fail returning NULL.

Issue was found by coccinelle.
Generated by: scripts/coccinelle/null/kmerr.cocci

Signed-off-by: Qiujun Huang <[email protected]>
Reviewed-by: Oliver O'Halloran <[email protected]>

---

v3->v4:
Added the information about coccinelle script.
Added change log.
Added Oliver's Reviewed-by.
v2->v3:
Removed redundant assignment to 'attr' and 'name'.
v1->v2:
Just return -ENOMEM if attr is NULL.
---
arch/powerpc/platforms/powernv/opal.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/arch/powerpc/platforms/powernv/opal.c b/arch/powerpc/platforms/powernv/opal.c
index 2b3dfd0b6cdd..908d749bcef5 100644
--- a/arch/powerpc/platforms/powernv/opal.c
+++ b/arch/powerpc/platforms/powernv/opal.c
@@ -801,16 +801,19 @@ static ssize_t export_attr_read(struct file *fp, struct kobject *kobj,
static int opal_add_one_export(struct kobject *parent, const char *export_name,
struct device_node *np, const char *prop_name)
{
- struct bin_attribute *attr = NULL;
- const char *name = NULL;
+ struct bin_attribute *attr;
+ const char *name;
u64 vals[2];
int rc;

rc = of_property_read_u64_array(np, prop_name, &vals[0], 2);
if (rc)
- goto out;
+ return rc;

attr = kzalloc(sizeof(*attr), GFP_KERNEL);
+ if (!attr)
+ return -ENOMEM;
+
name = kstrdup(export_name, GFP_KERNEL);
if (!name) {
rc = -ENOMEM;
--
2.17.1


2020-04-06 10:31:23

by Michael Ellerman

[permalink] [raw]
Subject: Re: [PATCH v4] powerpc/powernv: add NULL check after kzalloc in opal_add_one_export

Qiujun Huang <[email protected]> writes:
> Here needs a NULL check as kzalloc may fail returning NULL.
>
> Issue was found by coccinelle.
> Generated by: scripts/coccinelle/null/kmerr.cocci
>
> Signed-off-by: Qiujun Huang <[email protected]>
> Reviewed-by: Oliver O'Halloran <[email protected]>
>
> ---

Thanks for putting up with all the review comments :)

But I think this should actually be two patches now.

The first patch should change the goto after
of_property_read_u64_array() into a return and drop the redundant
assignments.

Then the second patch can add the NULL check for attr.

cheers

> v3->v4:
> Added the information about coccinelle script.
> Added change log.
> Added Oliver's Reviewed-by.
> v2->v3:
> Removed redundant assignment to 'attr' and 'name'.
> v1->v2:
> Just return -ENOMEM if attr is NULL.
> ---
> arch/powerpc/platforms/powernv/opal.c | 9 ++++++---
> 1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/arch/powerpc/platforms/powernv/opal.c b/arch/powerpc/platforms/powernv/opal.c
> index 2b3dfd0b6cdd..908d749bcef5 100644
> --- a/arch/powerpc/platforms/powernv/opal.c
> +++ b/arch/powerpc/platforms/powernv/opal.c
> @@ -801,16 +801,19 @@ static ssize_t export_attr_read(struct file *fp, struct kobject *kobj,
> static int opal_add_one_export(struct kobject *parent, const char *export_name,
> struct device_node *np, const char *prop_name)
> {
> - struct bin_attribute *attr = NULL;
> - const char *name = NULL;
> + struct bin_attribute *attr;
> + const char *name;
> u64 vals[2];
> int rc;
>
> rc = of_property_read_u64_array(np, prop_name, &vals[0], 2);
> if (rc)
> - goto out;
> + return rc;
>
> attr = kzalloc(sizeof(*attr), GFP_KERNEL);
> + if (!attr)
> + return -ENOMEM;
> +
> name = kstrdup(export_name, GFP_KERNEL);
> if (!name) {
> rc = -ENOMEM;
> --
> 2.17.1

2020-04-06 10:44:52

by Qiujun Huang

[permalink] [raw]
Subject: Re: [PATCH v4] powerpc/powernv: add NULL check after kzalloc in opal_add_one_export

On Mon, Apr 6, 2020 at 6:30 PM Michael Ellerman <[email protected]> wrote:
>
> Qiujun Huang <[email protected]> writes:
> > Here needs a NULL check as kzalloc may fail returning NULL.
> >
> > Issue was found by coccinelle.
> > Generated by: scripts/coccinelle/null/kmerr.cocci
> >
> > Signed-off-by: Qiujun Huang <[email protected]>
> > Reviewed-by: Oliver O'Halloran <[email protected]>
> >
> > ---
>
> Thanks for putting up with all the review comments :)
>
> But I think this should actually be two patches now.
>
> The first patch should change the goto after
> of_property_read_u64_array() into a return and drop the redundant
> assignments.
>
> Then the second patch can add the NULL check for attr.

Get that, I'll separate them.

>
> cheers
>
> > v3->v4:
> > Added the information about coccinelle script.
> > Added change log.
> > Added Oliver's Reviewed-by.
> > v2->v3:
> > Removed redundant assignment to 'attr' and 'name'.
> > v1->v2:
> > Just return -ENOMEM if attr is NULL.
> > ---
> > arch/powerpc/platforms/powernv/opal.c | 9 ++++++---
> > 1 file changed, 6 insertions(+), 3 deletions(-)
> >
> > diff --git a/arch/powerpc/platforms/powernv/opal.c b/arch/powerpc/platforms/powernv/opal.c
> > index 2b3dfd0b6cdd..908d749bcef5 100644
> > --- a/arch/powerpc/platforms/powernv/opal.c
> > +++ b/arch/powerpc/platforms/powernv/opal.c
> > @@ -801,16 +801,19 @@ static ssize_t export_attr_read(struct file *fp, struct kobject *kobj,
> > static int opal_add_one_export(struct kobject *parent, const char *export_name,
> > struct device_node *np, const char *prop_name)
> > {
> > - struct bin_attribute *attr = NULL;
> > - const char *name = NULL;
> > + struct bin_attribute *attr;
> > + const char *name;
> > u64 vals[2];
> > int rc;
> >
> > rc = of_property_read_u64_array(np, prop_name, &vals[0], 2);
> > if (rc)
> > - goto out;
> > + return rc;
> >
> > attr = kzalloc(sizeof(*attr), GFP_KERNEL);
> > + if (!attr)
> > + return -ENOMEM;
> > +
> > name = kstrdup(export_name, GFP_KERNEL);
> > if (!name) {
> > rc = -ENOMEM;
> > --
> > 2.17.1

2020-04-06 12:01:19

by Markus Elfring

[permalink] [raw]
Subject: Re: [PATCH v4] powerpc/powernv: add NULL check after kzalloc in opal_add_one_export

>>> Here needs a NULL check as kzalloc may fail returning NULL.

I find this wording potentially confusing.

* Such function calls will usually succeed to return a pointer.

* The desired memory allocation can fail.

* Please choose an imperative wording for the change description.


>>> Issue was found by coccinelle.

Please omit this line after the addition for the reference to the SmPL script.


>>> Generated by: scripts/coccinelle/null/kmerr.cocci

>>> Reviewed-by: Oliver O'Halloran <[email protected]>

I wonder about this tag because of requested changes for the shown patch approach.

I recommend to add the tag “Fixes”.


>> Thanks for putting up with all the review comments :)

This seems to become challenging here.


>> But I think this should actually be two patches now.


> Get that, I'll separate them.

I wonder why it was not directly tried in this patch version.


>>> v3->v4:

I suggest to apply a shorter version numbering format (without an arrow).

Regards,
Markus