summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2017-10-29 22:54:09 +0100
committerDavid Gibson <david@gibson.dropbear.id.au>2017-11-11 19:36:42 +1100
commit7975f6422260af4ac7ae2fcdff0ef2a6e391ab71 (patch)
treeb8ea452fcf138b59d6ccb897e582e44e2cd371a0
parentfca296445eabf3cfe986e89dd8711c0be583036d (diff)
downloaddevice-tree-compiler-7975f6422260af4ac7ae2fcdff0ef2a6e391ab71.tar.gz
Fix widespread incorrect use of strneq(), replace with new strprefixeq()
Every remaining usage of strneq() is, in fact, incorrect. They're trying to check that the first n characters of one string exactly match another string. But, they fall into the classic trap of strncmp() on which strneq() is based. If n is less than the length of the second string, they only check that the first string matches the start of the second, not the whole of it. To fix this, remove strneq() and replace it with a strprefixeq() function which does what we want here. Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
-rw-r--r--checks.c6
-rw-r--r--dtc.h2
-rw-r--r--livetree.c2
3 files changed, 5 insertions, 5 deletions
diff --git a/checks.c b/checks.c
index 770af32..f5bf5f9 100644
--- a/checks.c
+++ b/checks.c
@@ -696,8 +696,8 @@ static void check_pci_bridge(struct check *c, struct dt_info *dti, struct node *
node->bus = &pci_bus;
- if (!strneq(node->name, "pci", node->basenamelen) &&
- !strneq(node->name, "pcie", node->basenamelen))
+ if (!strprefixeq(node->name, node->basenamelen, "pci") &&
+ !strprefixeq(node->name, node->basenamelen, "pcie"))
FAIL(c, dti, "Node %s node name is not \"pci\" or \"pcie\"",
node->fullpath);
@@ -828,7 +828,7 @@ static bool node_is_compatible(struct node *node, const char *compat)
for (str = prop->val.val, end = str + prop->val.len; str < end;
str += strnlen(str, end - str) + 1) {
- if (strneq(str, compat, end - str))
+ if (strprefixeq(str, end - str, compat))
return true;
}
return false;
diff --git a/dtc.h b/dtc.h
index bb0b91b..758eb7c 100644
--- a/dtc.h
+++ b/dtc.h
@@ -67,8 +67,8 @@ typedef uint32_t cell_t;
#define streq(a, b) (strcmp((a), (b)) == 0)
-#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
#define strstarts(s, prefix) (strncmp((s), (prefix), strlen(prefix)) == 0)
+#define strprefixeq(a, n, b) (strlen(b) == (n) && (memcmp(a, b, n) == 0))
#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
diff --git a/livetree.c b/livetree.c
index 184703a..457d01e 100644
--- a/livetree.c
+++ b/livetree.c
@@ -507,7 +507,7 @@ struct node *get_node_by_path(struct node *tree, const char *path)
for_each_child(tree, child) {
if (p && (strlen(child->name) == p-path) &&
- strneq(path, child->name, p-path))
+ strprefixeq(path, p - path, child->name))
return get_node_by_path(child, p+1);
else if (!p && streq(path, child->name))
return child;