Skip to contentSkip to conversation

of: unittest: fix possible null-pointer dereferences in of_unittest_property_copy()

PATCH Applied Tuo Li 2 participants 2 messages Jan 5 Device tree

lore
Share & tools
lore

Tuo Li

author
#0 lore raw

PATCH 1/1 of: unittest: fix possible null-pointer dereferences in of_unittest_property_copy()

This function first duplicates p1 and p2 into new, and then checks whether the duplication succeeds. However, if the duplication fails (e.g., kzalloc() returns NULL in __of_prop_dup()), new will be NULL but is still dereferenced in __of_prop_free(). To ensure that the unit test continues to run even when duplication fails, add a NULL check before calling __of_prop_free().

Fixes 1c5e3d9bf33b ("of: Add a helper to free property struct")
---
 drivers/of/unittest.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/of/unittest.c +4 -2
index 388e9ec2cccf..ea650a9ca2d9 100644
@@ -804,11 +804,13 @@ static void __init of_unittest_property_copy(void)
 
 	new = __of_prop_dup(&p1, GFP_KERNEL);
 	unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
-	__of_prop_free(new);
+	if (new)
+		__of_prop_free(new);
 
 	new = __of_prop_dup(&p2, GFP_KERNEL);
 	unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
-	__of_prop_free(new);
+	if (new)
+		__of_prop_free(new);
 #endif
 }
 
Signature · 1 line
2.43.0
maintainer Applied
#1 lore raw
Quoted 7 lines · Tuo Li

This function first duplicates p1 and p2 into new, and then checks whether the duplication succeeds. However, if the duplication fails (e.g., kzalloc() returns NULL in __of_prop_dup()), new will be NULL but is still dereferenced in __of_prop_free(). To ensure that the unit test continues to run even when duplication fails, add a NULL check before calling __of_prop_free().

Quoted 6 lines
Fixes 1c5e3d9bf33b ("of: Add a helper to free property struct")
---
 drivers/of/unittest.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Applied, thanks!