Browse Source

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>
main
David Gibson 7 years ago
parent
commit
7975f64222
  1. 6
      checks.c
  2. 2
      dtc.h
  3. 2
      livetree.c

6
checks.c

@ -696,8 +696,8 @@ static void check_pci_bridge(struct check *c, struct dt_info *dti, struct node *


node->bus = &pci_bus; node->bus = &pci_bus;


if (!strneq(node->name, "pci", node->basenamelen) && if (!strprefixeq(node->name, node->basenamelen, "pci") &&
!strneq(node->name, "pcie", node->basenamelen)) !strprefixeq(node->name, node->basenamelen, "pcie"))
FAIL(c, dti, "Node %s node name is not \"pci\" or \"pcie\"", FAIL(c, dti, "Node %s node name is not \"pci\" or \"pcie\"",
node->fullpath); 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; for (str = prop->val.val, end = str + prop->val.len; str < end;
str += strnlen(str, end - str) + 1) { str += strnlen(str, end - str) + 1) {
if (strneq(str, compat, end - str)) if (strprefixeq(str, end - str, compat))
return true; return true;
} }
return false; return false;

2
dtc.h

@ -67,8 +67,8 @@ typedef uint32_t cell_t;




#define streq(a, b) (strcmp((a), (b)) == 0) #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 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)) #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))



2
livetree.c

@ -507,7 +507,7 @@ struct node *get_node_by_path(struct node *tree, const char *path)


for_each_child(tree, child) { for_each_child(tree, child) {
if (p && (strlen(child->name) == p-path) && 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); return get_node_by_path(child, p+1);
else if (!p && streq(path, child->name)) else if (!p && streq(path, child->name))
return child; return child;

Loading…
Cancel
Save