Browse Source

libfdt: Fix bug in fdt_subnode_offset_namelen()

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.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
main
David Gibson 16 years ago committed by Jon Loeliger
parent
commit
f99cd158a9
  1. 11
      libfdt/fdt_ro.c
  2. 3
      tests/include1.dts
  3. 3
      tests/include7.dts
  4. 18
      tests/rw_tree1.c
  5. 11
      tests/subnode_offset.c
  6. 5
      tests/sw_tree1.c
  7. 6
      tests/test_tree1.dts
  8. 6
      tests/test_tree1_dts0.dts
  9. 8
      tests/trees.S

11
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, int fdt_subnode_offset_namelen(const void *fdt, int offset,
const char *name, int namelen) const char *name, int namelen)
{ {
int depth; int depth = 0;


FDT_CHECK_HEADER(fdt); FDT_CHECK_HEADER(fdt);


for (depth = 0; for (depth = 0, offset = fdt_next_node(fdt, offset, &depth);
offset >= 0; (offset >= 0) && (depth > 0);
offset = fdt_next_node(fdt, offset, &depth)) { offset = fdt_next_node(fdt, offset, &depth)) {
if (depth < 0) if (depth < 0)
return -FDT_ERR_NOTFOUND; return -FDT_ERR_NOTFOUND;
@ -122,7 +122,10 @@ int fdt_subnode_offset_namelen(const void *fdt, int offset,
return offset; 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, int fdt_subnode_offset(const void *fdt, int parentoffset,

3
tests/include1.dts

@ -19,5 +19,8 @@
compatible = "subsubnode2", "subsubnode"; compatible = "subsubnode2", "subsubnode";
prop-int = <0726746425>; prop-int = <0726746425>;
}; };

ss2 {
};
}; };
}; };

3
tests/include7.dts

@ -6,4 +6,7 @@
compatible = "subsubnode1", "subsubnode"; compatible = "subsubnode1", "subsubnode";
prop-int = <0xdeadbeef>; prop-int = <0xdeadbeef>;
}; };

ss1 {
};
}; };

18
tests/rw_tree1.c

@ -50,7 +50,7 @@ int main(int argc, char *argv[])
{ {
void *fdt; void *fdt;
int err; int err;
int offset; int offset, s1, s2;


test_init(argc, argv); 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)); CHECK(fdt_setprop_string(fdt, 0, "prop-str", TEST_STRING_1));


OFF_CHECK(offset, fdt_add_subnode(fdt, 0, "subnode@1")); OFF_CHECK(offset, fdt_add_subnode(fdt, 0, "subnode@1"));
CHECK(fdt_setprop_string(fdt, offset, "compatible", "subnode1")); s1 = offset;
CHECK(fdt_setprop_cell(fdt, offset, "prop-int", TEST_VALUE_1)); CHECK(fdt_setprop_string(fdt, s1, "compatible", "subnode1"));
OFF_CHECK(offset, fdt_add_subnode(fdt, offset, "subsubnode")); 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", CHECK(fdt_setprop(fdt, offset, "compatible",
"subsubnode1\0subsubnode", 23)); "subsubnode1\0subsubnode", 23));
CHECK(fdt_setprop_cell(fdt, offset, "prop-int", TEST_VALUE_1)); 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")); OFF_CHECK(offset, fdt_add_subnode(fdt, 0, "subnode@2"));
CHECK(fdt_setprop_cell(fdt, offset, "linux,phandle", PHANDLE_1)); s2 = offset;
CHECK(fdt_setprop_cell(fdt, offset, "prop-int", TEST_VALUE_2)); CHECK(fdt_setprop_cell(fdt, s2, "linux,phandle", PHANDLE_1));
OFF_CHECK(offset, fdt_add_subnode(fdt, offset, "subsubnode@0")); 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_cell(fdt, offset, "linux,phandle", PHANDLE_2));
CHECK(fdt_setprop(fdt, offset, "compatible", CHECK(fdt_setprop(fdt, offset, "compatible",
"subsubnode2\0subsubnode", 23)); "subsubnode2\0subsubnode", 23));
CHECK(fdt_setprop_cell(fdt, offset, "prop-int", TEST_VALUE_2)); CHECK(fdt_setprop_cell(fdt, offset, "prop-int", TEST_VALUE_2));
OFF_CHECK(offset, fdt_add_subnode(fdt, s2, "ss2"));


CHECK(fdt_pack(fdt)); CHECK(fdt_pack(fdt));



11
tests/subnode_offset.c

@ -60,6 +60,7 @@ int main(int argc, char *argv[])
void *fdt; void *fdt;
int subnode1_offset, subnode2_offset; int subnode1_offset, subnode2_offset;
int subsubnode1_offset, subsubnode2_offset, subsubnode2_offset2; int subsubnode1_offset, subsubnode2_offset, subsubnode2_offset2;
int ss11_off, ss12_off, ss21_off, ss22_off;


test_init(argc, argv); test_init(argc, argv);
fdt = load_blob_arg(argc, argv); fdt = load_blob_arg(argc, argv);
@ -84,5 +85,15 @@ int main(int argc, char *argv[])
if (subsubnode2_offset != subsubnode2_offset2) if (subsubnode2_offset != subsubnode2_offset2)
FAIL("Different offsets with and without unit address"); 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(); PASS();
} }

5
tests/sw_tree1.c

@ -66,6 +66,8 @@ int main(int argc, char *argv[])
23)); 23));
CHECK(fdt_property_cell(fdt, "prop-int", TEST_VALUE_1)); CHECK(fdt_property_cell(fdt, "prop-int", TEST_VALUE_1));
CHECK(fdt_end_node(fdt)); CHECK(fdt_end_node(fdt));
CHECK(fdt_begin_node(fdt, "ss1"));
CHECK(fdt_end_node(fdt));
CHECK(fdt_end_node(fdt)); CHECK(fdt_end_node(fdt));


CHECK(fdt_begin_node(fdt, "subnode@2")); CHECK(fdt_begin_node(fdt, "subnode@2"));
@ -77,6 +79,9 @@ int main(int argc, char *argv[])
23)); 23));
CHECK(fdt_property_cell(fdt, "prop-int", TEST_VALUE_2)); CHECK(fdt_property_cell(fdt, "prop-int", TEST_VALUE_2));
CHECK(fdt_end_node(fdt)); 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));


CHECK(fdt_end_node(fdt)); CHECK(fdt_end_node(fdt));

6
tests/test_tree1.dts

@ -16,6 +16,9 @@
compatible = "subsubnode1", "subsubnode"; compatible = "subsubnode1", "subsubnode";
prop-int = <0xdeadbeef>; prop-int = <0xdeadbeef>;
}; };

ss1 {
};
}; };


subnode@2 { subnode@2 {
@ -27,5 +30,8 @@
compatible = "subsubnode2", "subsubnode"; compatible = "subsubnode2", "subsubnode";
prop-int = <0726746425>; prop-int = <0726746425>;
}; };

ss2 {
};
}; };
}; };

6
tests/test_tree1_dts0.dts

@ -16,6 +16,9 @@
compatible = "subsubnode1", "subsubnode"; compatible = "subsubnode1", "subsubnode";
prop-int = < 0xdeadbeef>; prop-int = < 0xdeadbeef>;
}; };

ss1 {
};
}; };


subnode@2 { subnode@2 {
@ -27,5 +30,8 @@
compatible = "subsubnode2", "subsubnode"; compatible = "subsubnode2", "subsubnode";
prop-int = < 0726746425>; prop-int = < 0726746425>;
}; };

ss2 {
};
}; };
}; };

8
tests/trees.S

@ -96,6 +96,10 @@ test_tree1_struct:
PROP_STR(test_tree1, compatible, "subsubnode1\0subsubnode") PROP_STR(test_tree1, compatible, "subsubnode1\0subsubnode")
PROP_INT(test_tree1, prop_int, TEST_VALUE_1) PROP_INT(test_tree1, prop_int, TEST_VALUE_1)
END_NODE END_NODE

BEGIN_NODE("ss1")
END_NODE

END_NODE END_NODE


BEGIN_NODE("subnode@2") BEGIN_NODE("subnode@2")
@ -107,6 +111,10 @@ test_tree1_struct:
PROP_STR(test_tree1, compatible, "subsubnode2\0subsubnode") PROP_STR(test_tree1, compatible, "subsubnode2\0subsubnode")
PROP_INT(test_tree1, prop_int, TEST_VALUE_2) PROP_INT(test_tree1, prop_int, TEST_VALUE_2)
END_NODE END_NODE

BEGIN_NODE("ss2")
END_NODE

END_NODE END_NODE


END_NODE END_NODE

Loading…
Cancel
Save