Skip to contentSkip to conversation

powerpc/powernv: add NULL check after kzalloc in opal_add_one_export

PATCH In review Qiujun Huang 3 participants 4 messages Apr 6, 2020 PowerPC

lore
Share & tools
lore
#0 lore raw

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

---

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(-)
arch/powerpc/platforms/powernv/opal.c +6 -3
index 2b3dfd0b6cdd..908d749bcef5 100644
@@ -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;
Signature · 1 line
2.17.1
#1 lore raw
Quoted 9 lines · Qiujun Huang

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

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

---

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

Quoted 41 lines

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(-)
arch/powerpc/platforms/powernv/opal.c +6 -3
index 2b3dfd0b6cdd..908d749bcef5 100644
@@ -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;
Signature · 1 line
2.17.1
#2 lore raw
Quoted 14 lines · Michael Ellerman
Quoted 9 lines · Qiujun Huang

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

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

---

Thanks for putting up with all the review comments :)

Quoted 7 lines

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.

Quoted 44 lines

cheers

Quoted 41 lines

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(-)
arch/powerpc/platforms/powernv/opal.c +6 -4
index 2b3dfd0b6cdd..908d749bcef5 100644
@@ -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

#3 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line

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.

Quoted 1 line
Quoted 1 line
Quoted 1 line

Issue was found by coccinelle.

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

Quoted 1 line
Quoted 1 line
Quoted 1 line

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

…

Quoted 1 line
Quoted 1 line
Quoted 1 line

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

I recommend to add the tag “Fixes”.

Quoted 1 line
Quoted 1 line

Thanks for putting up with all the review comments :)

This seems to become challenging here.

Quoted 1 line
Quoted 1 line

But I think this should actually be two patches now.

…

Quoted 1 line

Get that, I'll separate them.

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

Quoted 1 line
Quoted 1 line
Quoted 1 line

v3->v4:

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

Regards,
Markus