Skip to contentSkip to conversation

libfdt: Fix bug in fdt_subnode_offset_namelen()

PATCH Applied David Gibson 2 participants 2 messages Oct 30, 2008 – Nov 5, 2008

lore
Share & tools
lore
#0 lore raw

There's currently an off-by-one bug in fdt_subnode_offset_namelen() which causes it to keep searching after it's finished the subnodes of the given parent, and into the subnodes of siblings of the original node which come after it in the tree.

This patch fixes the bug. It also extends the subnode_offset testcase (updating all of the 'test_tree1' example trees in the process) to catch it.

tests/test_tree1.dts +6 -0
Index: dtc/tests/test_tree1.dts
===================================================================
@@ -16,6 +16,9 @@
 			compatible = "subsubnode1", "subsubnode";
 			prop-int = <0xdeadbeef>;
 		};
+
+		ss1 {
+		};
 	};
 
 	subnode@2 {
@@ -27,5 +30,8 @@
 			compatible = "subsubnode2", "subsubnode";
 			prop-int = <0726746425>;
 		};
+
+		ss2 {
+		};
 	};
 };
tests/subnode_offset.c +11 -0
Index: dtc/tests/subnode_offset.c
===================================================================
@@ -60,6 +60,7 @@ int main(int argc, char *argv[])
 	void *fdt;
 	int subnode1_offset, subnode2_offset;
 	int subsubnode1_offset, subsubnode2_offset, subsubnode2_offset2;
+	int ss11_off, ss12_off, ss21_off, ss22_off;
 
 	test_init(argc, argv);
 	fdt = load_blob_arg(argc, argv);
@@ -84,5 +85,15 @@ int main(int argc, char *argv[])
 	if (subsubnode2_offset != subsubnode2_offset2)
 		FAIL("Different offsets with and without unit address");
 
+	ss11_off = check_subnode(fdt, subnode1_offset, "ss1");
+	ss21_off = fdt_subnode_offset(fdt, subnode2_offset, "ss1");
+	if (ss21_off != -FDT_ERR_NOTFOUND)
+		FAIL("Incorrectly found ss1 in subnode2");
+
+	ss12_off = fdt_subnode_offset(fdt, subnode1_offset, "ss2");
+	if (ss12_off != -FDT_ERR_NOTFOUND)
+		FAIL("Incorrectly found ss2 in subnode1");
+	ss22_off = check_subnode(fdt, subnode2_offset, "ss2");
+
 	PASS();
 }
tests/trees.S +8 -0
Index: dtc/tests/trees.S
===================================================================
@@ -96,6 +96,10 @@ test_tree1_struct:
 	PROP_STR(test_tree1, compatible, "subsubnode1\0subsubnode")
 	PROP_INT(test_tree1, prop_int, TEST_VALUE_1)
 	END_NODE
+
+	BEGIN_NODE("ss1")
+	END_NODE
+
 	END_NODE
 
 	BEGIN_NODE("subnode@2")
@@ -107,6 +111,10 @@ test_tree1_struct:
 	PROP_STR(test_tree1, compatible, "subsubnode2\0subsubnode")
 	PROP_INT(test_tree1, prop_int, TEST_VALUE_2)
 	END_NODE
+
+	BEGIN_NODE("ss2")
+	END_NODE
+
 	END_NODE
 
 	END_NODE
tests/sw_tree1.c +5 -0
Index: dtc/tests/sw_tree1.c
===================================================================
@@ -66,6 +66,8 @@ int main(int argc, char *argv[])
 			   23));
 	CHECK(fdt_property_cell(fdt, "prop-int", TEST_VALUE_1));
 	CHECK(fdt_end_node(fdt));
+	CHECK(fdt_begin_node(fdt, "ss1"));
+	CHECK(fdt_end_node(fdt));
 	CHECK(fdt_end_node(fdt));
 
 	CHECK(fdt_begin_node(fdt, "subnode@2"));
@@ -77,6 +79,9 @@ int main(int argc, char *argv[])
 			   23));
 	CHECK(fdt_property_cell(fdt, "prop-int", TEST_VALUE_2));
 	CHECK(fdt_end_node(fdt));
+	CHECK(fdt_begin_node(fdt, "ss2"));
+	CHECK(fdt_end_node(fdt));
+
 	CHECK(fdt_end_node(fdt));
 
 	CHECK(fdt_end_node(fdt));
tests/rw_tree1.c +11 -7
Index: dtc/tests/rw_tree1.c
===================================================================
@@ -50,7 +50,7 @@ int main(int argc, char *argv[])
 {
 	void *fdt;
 	int err;
-	int offset;
+	int offset, s1, s2;
 
 	test_init(argc, argv);
 
@@ -77,21 +77,25 @@ int main(int argc, char *argv[])
 	CHECK(fdt_setprop_string(fdt, 0, "prop-str", TEST_STRING_1));
 
 	OFF_CHECK(offset, fdt_add_subnode(fdt, 0, "subnode@1"));
-	CHECK(fdt_setprop_string(fdt, offset, "compatible", "subnode1"));
-	CHECK(fdt_setprop_cell(fdt, offset, "prop-int", TEST_VALUE_1));
-	OFF_CHECK(offset, fdt_add_subnode(fdt, offset, "subsubnode"));
+	s1 = offset;
+	CHECK(fdt_setprop_string(fdt, s1, "compatible", "subnode1"));
+	CHECK(fdt_setprop_cell(fdt, s1, "prop-int", TEST_VALUE_1));
+	OFF_CHECK(offset, fdt_add_subnode(fdt, s1, "subsubnode"));
 	CHECK(fdt_setprop(fdt, offset, "compatible",
 			  "subsubnode1\0subsubnode", 23));
 	CHECK(fdt_setprop_cell(fdt, offset, "prop-int", TEST_VALUE_1));
+	OFF_CHECK(offset, fdt_add_subnode(fdt, s1, "ss1"));
 
 	OFF_CHECK(offset, fdt_add_subnode(fdt, 0, "subnode@2"));
-	CHECK(fdt_setprop_cell(fdt, offset, "linux,phandle", PHANDLE_1));
-	CHECK(fdt_setprop_cell(fdt, offset, "prop-int", TEST_VALUE_2));
-	OFF_CHECK(offset, fdt_add_subnode(fdt, offset, "subsubnode@0"));
+	s2 = offset;
+	CHECK(fdt_setprop_cell(fdt, s2, "linux,phandle", PHANDLE_1));
+	CHECK(fdt_setprop_cell(fdt, s2, "prop-int", TEST_VALUE_2));
+	OFF_CHECK(offset, fdt_add_subnode(fdt, s2, "subsubnode@0"));
 	CHECK(fdt_setprop_cell(fdt, offset, "linux,phandle", PHANDLE_2));
 	CHECK(fdt_setprop(fdt, offset, "compatible",
 			  "subsubnode2\0subsubnode", 23));
 	CHECK(fdt_setprop_cell(fdt, offset, "prop-int", TEST_VALUE_2));
+	OFF_CHECK(offset, fdt_add_subnode(fdt, s2, "ss2"));
 
 	CHECK(fdt_pack(fdt));
 
libfdt/fdt_ro.c +7 -4
Index: dtc/libfdt/fdt_ro.c
===================================================================
@@ -108,12 +108,12 @@ int fdt_num_mem_rsv(const void *fdt)
 int fdt_subnode_offset_namelen(const void *fdt, int offset,
 			       const char *name, int namelen)
 {
-	int depth;
+	int depth = 0;
 
 	FDT_CHECK_HEADER(fdt);
 
-	for (depth = 0;
-	     offset >= 0;
+	for (depth = 0, offset = fdt_next_node(fdt, offset, &depth);
+	     (offset >= 0) && (depth > 0);
 	     offset = fdt_next_node(fdt, offset, &depth)) {
 		if (depth < 0)
 			return -FDT_ERR_NOTFOUND;
@@ -122,7 +122,10 @@ int fdt_subnode_offset_namelen(const voi
 			return offset;
 	}
 
-	return offset; /* error */
+	if (offset < 0)
+		return offset; /* error */
+	else
+		return -FDT_ERR_NOTFOUND;
 }
 
 int fdt_subnode_offset(const void *fdt, int parentoffset,
tests/test_tree1_dts0.dts +6 -0
Index: dtc/tests/test_tree1_dts0.dts
===================================================================
@@ -16,6 +16,9 @@
 			compatible = "subsubnode1", "subsubnode";
 			prop-int = < 0xdeadbeef>;
 		};
+
+		ss1 {
+		};
 	};
 
 	subnode@2 {
@@ -27,5 +30,8 @@
 			compatible = "subsubnode2", "subsubnode";
 			prop-int = < 0726746425>;
 		};
+
+		ss2 {
+		};
 	};
 };
tests/include1.dts +3 -0
Index: dtc/tests/include1.dts
===================================================================
@@ -19,5 +19,8 @@
 			compatible = "subsubnode2", "subsubnode";
 			prop-int = <0726746425>;
 		};
+
+		ss2 {
+		};
 	};
 };
tests/include7.dts +3 -0
Index: dtc/tests/include7.dts
===================================================================
@@ -6,4 +6,7 @@
 			compatible = "subsubnode1", "subsubnode";
 			prop-int = <0xdeadbeef>;
 		};
+
+		ss1 {
+		};
 	};
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
Applied
#1 lore raw
Quoted 10 lines

There's currently an off-by-one bug in fdt_subnode_offset_namelen() which causes it to keep searching after it's finished the subnodes of the given parent, and into the subnodes of siblings of the original node which come after it in the tree.

This patch fixes the bug. It also extends the subnode_offset testcase (updating all of the 'test_tree1' example trees in the process) to catch it.

Applied.

Thanks,
jdl