From e4fdcadd8a6eedb1edaabbc85c782b43d4e80fe2 Mon Sep 17 00:00:00 2001 From: Lucian Cojocar Date: Sun, 28 Apr 2013 11:31:57 +0000 Subject: env: throw an error when an empty key is used If the environment contains an entry like "=value" "\0" we should throw an error when parsing the environment. Otherwise, U-Boot will enter in an infinite loop. Signed-off-by: Lucian Cojocar --- lib/hashtable.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib') diff --git a/lib/hashtable.c b/lib/hashtable.c index 6050dd0829..4cdbc95329 100644 --- a/lib/hashtable.c +++ b/lib/hashtable.c @@ -901,6 +901,12 @@ int himport_r(struct hsearch_data *htab, *sp++ = '\0'; /* terminate value */ ++dp; + if (*name == 0) { + debug("INSERT: unable to use an empty key\n"); + __set_errno(EINVAL); + return 0; + } + /* Skip variables which are not supposed to be processed */ if (!drop_var_from_set(name, nvars, localvars)) continue; -- cgit From e853b32424e1fbbbcf1a78e6ddd3e732abe72dc0 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Mon, 21 Jan 2013 12:59:18 -0800 Subject: Export fdt_stringlist_contains() This function is useful outside libfdt, so export it. Ref: DTC commit b7aa300e Signed-off-by: Simon Glass Acked-by: David Gibson --- lib/libfdt/fdt_ro.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/libfdt/fdt_ro.c b/lib/libfdt/fdt_ro.c index 1a461c3e97..b65f4e23ad 100644 --- a/lib/libfdt/fdt_ro.c +++ b/lib/libfdt/fdt_ro.c @@ -519,8 +519,7 @@ int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle) return offset; /* error from fdt_next_node() */ } -static int _fdt_stringlist_contains(const char *strlist, int listlen, - const char *str) +int fdt_stringlist_contains(const char *strlist, int listlen, const char *str) { int len = strlen(str); const char *p; @@ -546,7 +545,7 @@ int fdt_node_check_compatible(const void *fdt, int nodeoffset, prop = fdt_getprop(fdt, nodeoffset, "compatible", &len); if (!prop) return len; - if (_fdt_stringlist_contains(prop, len, compatible)) + if (fdt_stringlist_contains(prop, len, compatible)) return 0; else return 1; -- cgit From 88f95bbadda89bfaf6a8e817bb66fd114afc1caf Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 7 May 2013 06:11:50 +0000 Subject: libfdt: Add fdt_next_subnode() to permit easy subnode iteration Iterating through subnodes with libfdt is a little painful to write as we need something like this: for (depth = 0, count = 0, offset = fdt_next_node(fdt, parent_offset, &depth); (offset >= 0) && (depth > 0); offset = fdt_next_node(fdt, offset, &depth)) { if (depth == 1) { /* code body */ } } Using fdt_next_subnode() we can instead write this, which is shorter and easier to get right: for (offset = fdt_first_subnode(fdt, parent_offset); offset >= 0; offset = fdt_next_subnode(fdt, offset)) { /* code body */ } Also, it doesn't require two levels of indentation for the loop body. Signed-off-by: Simon Glass (Cherry-picked from dtc commit 4e76ec79) Acked-by: Gerald Van Baren --- lib/libfdt/fdt.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'lib') diff --git a/lib/libfdt/fdt.c b/lib/libfdt/fdt.c index 387e3544b7..154e9a4461 100644 --- a/lib/libfdt/fdt.c +++ b/lib/libfdt/fdt.c @@ -202,6 +202,34 @@ int fdt_next_node(const void *fdt, int offset, int *depth) return offset; } +int fdt_first_subnode(const void *fdt, int offset) +{ + int depth = 0; + + offset = fdt_next_node(fdt, offset, &depth); + if (offset < 0 || depth != 1) + return -FDT_ERR_NOTFOUND; + + return offset; +} + +int fdt_next_subnode(const void *fdt, int offset) +{ + int depth = 1; + + /* + * With respect to the parent, the depth of the next subnode will be + * the same as the last. + */ + do { + offset = fdt_next_node(fdt, offset, &depth); + if (offset < 0 || depth < 1) + return -FDT_ERR_NOTFOUND; + } while (depth > 1); + + return offset; +} + const char *_fdt_find_string(const char *strtab, int tabsize, const char *s) { int len = strlen(s) + 1; -- cgit