Skip to contentSkip to conversation

drivers: base: swnode: check if swnode is null before dereferencing it

PATCH Applied Colin King 3 participants 3 messages Dec 22, 2018 – Jan 2, 2019

Subsystems (3)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 drivers: base: swnode: check if swnode is null before dereferencing it

From: Colin Ian King <[email protected]>

The macro to_software_mode can potentially return NULL, so also add a null check on the swnode before dereferencing it to avoid any null pointer dereferences.

Detected by CoverityScan, CID#1476052 ("Explicit null dereferenced")

Fixes 59abd83672f7 ("drivers: base: Introducing software nodes to the firmware node framework")
---
 drivers/base/swnode.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/base/swnode.c +2 -1
index 306bb93287af..204aa7d049e0 100644
@@ -477,7 +477,8 @@ software_node_get_parent(const struct fwnode_handle *fwnode)
 {
 	struct software_node *swnode = to_software_node(fwnode);
 
-	return swnode->parent ? &swnode->parent->fwnode : NULL;
+	return swnode ? (swnode->parent ? &swnode->parent->fwnode : NULL) :
+			NULL;
 }
 
 struct fwnode_handle *
Signature · 1 line
2.19.1
maintainer Applied
#1 lore raw
Quoted 20 lines · Colin King

From: Colin Ian King <[email protected]>

The macro to_software_mode can potentially return NULL, so also add a null check on the swnode before dereferencing it to avoid any null pointer dereferences.

Detected by CoverityScan, CID#1476052 ("Explicit null dereferenced")

Fixes 59abd83672f7 ("drivers: base: Introducing software nodes to the firmware node framework")
---
 drivers/base/swnode.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/base/swnode.c +0 -0
index 306bb93287af..204aa7d049e0 100644

drivers/base/swnode.c software_node_get_parent(const struct fwnode_handle *fwnode)

Quoted 10 lines
drivers/base/swnode.c +2 -2
@@ -477,7 +477,8 @@ software_node_get_parent(const struct fwnode_handle *fwnode)
 {
        struct software_node *swnode = to_software_node(fwnode);
 
-       return swnode->parent ? &swnode->parent->fwnode : NULL;
+       return swnode ? (swnode->parent ? &swnode->parent->fwnode : NULL) :
+                       NULL;
 }
 
 struct fwnode_handle *
--

Applied, thanks!

#2 lore raw
In reply to Colin King
Quoted 10 lines · Colin King

From: Colin Ian King <[email protected]>

The macro to_software_mode can potentially return NULL, so also add a null check on the swnode before dereferencing it to avoid any null pointer dereferences.

Detected by CoverityScan, CID#1476052 ("Explicit null dereferenced")

Fixes 59abd83672f7 ("drivers: base: Introducing software nodes to the firmware node framework")

to_software_mode() can't return NULL though...

We shouldn't change the code just to make the static checker happy.

Sometimes if we just silence every static checker warning maybe we will fix some bugs in the middle of silencing all the false positive, but Smatch is almost at the stage of being able to parse this code correctly. Maybe by the end of the year. So let's hold off and then think about taking the fix everything approach next year.

regards,
dan carpenter