Skip to contentSkip to conversation

dtc: Fix NULL pointer use in dtlabel + dtref case

PATCH Superseded Stephen Boyd 2 participants 2 messages Jan 25, 2017 – Jan 30, 2017 1 message not in the archive

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dtc: Fix NULL pointer use in dtlabel + dtref case

If we have a construct like this:

	label: &handle {
		...
	};

Running dtc on it will cause a segfault, because we use 'target' when it could be NULL. Move the add_label() call into the if statement to fix this potentially bad use of a NULL pointer.

---
 dtc-parser.y | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
dtc-parser.y +3 -3
index b2fd4d155839..ca3f5003427c 100644
@@ -171,10 +171,10 @@ devicetree:
 		{
 			struct node *target = get_node_by_ref($1, $3);
 
-			add_label(&target->labels, $2);
-			if (target)
+			if (target) {
+				add_label(&target->labels, $2);
 				merge_nodes(target, $4);
-			else
+			} else
 				ERROR(&@3, "Label or path %s not found", $3);
 			$$ = $1;
 		}
Signature · 1 line
2.10.0.297.gf6727b0

A message that is not in the archive (sent off-list or not yet received): 20170125192724.11989-1-stephen.boyd-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org

#2 lore raw
Quoted 11 lines · Stephen Boyd

If we have a construct like this:

	label: &handle {
		...
	};

Running dtc on it will cause a segfault, because we use 'target' when it could be NULL. Move the add_label() call into the if statement to fix this potentially bad use of a NULL pointer.

Looks good, but could you please add a testcase, so we don't make the same mistake in future.

Quoted 22 lines
---
 dtc-parser.y | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
dtc-parser.y +3 -3
index b2fd4d155839..ca3f5003427c 100644
@@ -171,10 +171,10 @@ devicetree:
 		{
 			struct node *target = get_node_by_ref($1, $3);
 
-			add_label(&target->labels, $2);
-			if (target)
+			if (target) {
+				add_label(&target->labels, $2);
 				merge_nodes(target, $4);
-			else
+			} else
 				ERROR(&@3, "Label or path %s not found", $3);
 			$$ = $1;
 		}
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson