summaryrefslogtreecommitdiff
path: root/libfdt
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2008-08-29 14:19:13 +1000
committerJon Loeliger <jdl@jdl.com>2008-09-25 09:34:27 -0500
commit8daae14b742a48078c77fca3ccb24cd376232bc1 (patch)
tree42dc06c0d720049a7f54bf22645052245c8302c2 /libfdt
parent02cc83540bd7aed689560908da2e4e7f6a39e9b4 (diff)
downloaddtc-8daae14b742a48078c77fca3ccb24cd376232bc1.tar.gz
libfdt: Fix bugs in fdt_get_path()
The current implementation of fdt_get_path() has a couple of bugs, fixed by this patch. First, contrary to its documentation, on success it returns the length of the node's path, rather than 0. The testcase is correspondingly wrong, and the patch fixes this as well. Second, in some circumstances, it will return -FDT_ERR_BADOFFSET instead of -FDT_ERR_NOSPACE when given insufficient buffer space. Specifically this happens when there is insufficient space even to hold the path's second last component. This behaviour is corrected, and the testcase updated to check it. Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Diffstat (limited to 'libfdt')
-rw-r--r--libfdt/fdt_ro.c23
1 files changed, 11 insertions, 12 deletions
diff --git a/libfdt/fdt_ro.c b/libfdt/fdt_ro.c
index 2f3ff48..1d1e35e 100644
--- a/libfdt/fdt_ro.c
+++ b/libfdt/fdt_ro.c
@@ -316,9 +316,6 @@ int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
for (offset = 0, depth = 0;
(offset >= 0) && (offset <= nodeoffset);
offset = fdt_next_node(fdt, offset, &depth)) {
- if (pdepth < depth)
- continue; /* overflowed buffer */
-
while (pdepth > depth) {
do {
p--;
@@ -326,14 +323,16 @@ int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
pdepth--;
}
- name = fdt_get_name(fdt, offset, &namelen);
- if (!name)
- return namelen;
- if ((p + namelen + 1) <= buflen) {
- memcpy(buf + p, name, namelen);
- p += namelen;
- buf[p++] = '/';
- pdepth++;
+ if (pdepth >= depth) {
+ name = fdt_get_name(fdt, offset, &namelen);
+ if (!name)
+ return namelen;
+ if ((p + namelen + 1) <= buflen) {
+ memcpy(buf + p, name, namelen);
+ p += namelen;
+ buf[p++] = '/';
+ pdepth++;
+ }
}
if (offset == nodeoffset) {
@@ -343,7 +342,7 @@ int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
if (p > 1) /* special case so that root path is "/", not "" */
p--;
buf[p] = '\0';
- return p;
+ return 0;
}
}