RE: [PATCH] Fix get_node_by_path string equality check

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



When determining if to recurse into a node, get_node_by_path does not
check if the length of each node name is equal.  If searching for
/foo/baz, this can result in recursing into /foobar because
strneq("foo", "foobar", 3) is true.

This can result in a reference to /foo/baz to be incorrectly set to
/foobar/baz.  A test for this was added.

Signed-off-by: Tim Montague <tmontague@xxxxxxx>
---
 livetree.c                |  3 ++-
 tests/path-references.c   | 12 +++++++++++-
 tests/path-references.dts | 13 +++++++++++++
 3 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/livetree.c b/livetree.c
index 3673de0..aecd278 100644
--- a/livetree.c
+++ b/livetree.c
@@ -478,7 +478,8 @@ struct node *get_node_by_path(struct node *tree, const char *path)
 	p = strchr(path, '/');
 
 	for_each_child(tree, child) {
-		if (p && strneq(path, child->name, p-path))
+		if (p && (strlen(child->name) == p-path) &&
+				strneq(path, child->name, p-path))
 			return get_node_by_path(child, p+1);
 		else if (!p && streq(path, child->name))
 			return child;
diff --git a/tests/path-references.c b/tests/path-references.c
index c8d25fb..5e332e8 100644
--- a/tests/path-references.c
+++ b/tests/path-references.c
@@ -66,7 +66,7 @@ int main(int argc, char *argv[])
 	void *fdt;
 	const char *p;
 	int len, multilen;
-	int n1, n2;
+	int n1, n2, n3, n4;
 
 	test_init(argc, argv);
 	fdt = load_blob_arg(argc, argv);
@@ -92,6 +92,16 @@ int main(int argc, char *argv[])
 	if ((!streq(p, "/node1") || !streq(p + strlen("/node1") + 1, "/node2")))
 		FAIL("multiref has wrong value");
 
+	/* Check reference to nested nodes with common prefix */
+	n3 = fdt_path_offset(fdt, "/foo/baz");
+	if (n3 < 0)
+		FAIL("fdt_path_offset(/foo/baz): %s", fdt_strerror(n3));
+	n4 = fdt_path_offset(fdt, "/foobar/baz");
+	if (n4 < 0)
+		FAIL("fdt_path_offset(/foobar/baz): %s", fdt_strerror(n4));
+	check_ref(fdt, n3, "/foobar/baz");
+	check_ref(fdt, n4, "/foo/baz");
+
 	check_rref(fdt);
 
 	PASS();
diff --git a/tests/path-references.dts b/tests/path-references.dts
index b00fd79..3808aa6 100644
--- a/tests/path-references.dts
+++ b/tests/path-references.dts
@@ -12,4 +12,17 @@
 		ref = &{/node1}; /* reference after target */
 		lref = &n1;
 	};
+	/* Check references to nested nodes with common prefix */
+	foobar {
+		n3: baz {
+		    ref = &{/foo/baz};
+		    lref = &n4;
+		};
+	};
+	foo {
+		n4: baz {
+		    ref = &{/foobar/baz};
+		    lref = &n3;
+		};
+	};
 };
-- 
2.7.4
��.n��������+%������w��{.n����z�{��z��^n�r������&��z�ޗ�zf���h���~����������_��+v���)ߣ�

[Index of Archives]     [Device Tree]     [Device Tree Spec]     [Linux Driver Backports]     [Video for Linux]     [Linux USB Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]

  Powered by Linux