Hi Bartosz, > Date: 2026-11-04 15:03:43+0100 > From: Bartosz Golaszewski > > str_has_prefix() returns the prefix length on match and 0 on mismatch, This is indeed a problem. str_has_prefix() is a bad design. > so the mistmatch case can't be told from "empty prefix matched an empty > string". Indeed. The better solution would be to fix str_has_prefix(). In other projects, I've been using a similar function, but it returns a pointer to after the prefix, which is more useful *and* doesn't have this bug. char * strprefix(const char *s, const char *prefix) { if (strncmp(s, prefix, strlen(prefix)) != 0) return NULL; return (char *) s + strlen(prefix); } > For instance: the OF root node's name as returned by > fwnode_get_name() is "" and will return 0 for any name matched against > it and since len is 0 too, fwnode_name_eq() will return true. > > Do what OF code already does in of_node_name_eq(): check if the length > of the expected name matches the length of the actual node name before > comparing it. > > Fixes: a9c8c738066b ("device property: Add fwnode_name_eq()") > Signed-off-by: Bartosz Golaszewski > --- > drivers/base/property.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/base/property.c b/drivers/base/property.c > index 51109794d7275ddaf4571584d0e77d8573ebb475..a97575ff9199004b4f401f432aed6c95e40d0c25 100644 > --- a/drivers/base/property.c > +++ b/drivers/base/property.c > @@ -700,7 +700,7 @@ bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name) > > len = strchrnul(node_name, '@') - node_name; > > - return str_has_prefix(node_name, name) == len; > + return len == strlen(name) && !strncmp(node_name, name, len); This would be going back to the stone age, and introducing code that is prone to bugs. Please don't. I'd suggest adding strprefix(), and then simplifying all this into the following: diff --git a/drivers/base/property.c b/drivers/base/property.c index 51109794d727..7728116e2968 100644 --- a/drivers/base/property.c +++ b/drivers/base/property.c @@ -692,15 +692,12 @@ const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode) bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name) { const char *node_name; - ptrdiff_t len; node_name = fwnode_get_name(fwnode); if (!node_name) return false; - len = strchrnul(node_name, '@') - node_name; - - return str_has_prefix(node_name, name) == len; + return strprefix(node_name, name) == strchrnul(node_name, '@'); } EXPORT_SYMBOL_GPL(fwnode_name_eq); I'll send a pair of patches in a moment. Have a lovely day! Alex > } > EXPORT_SYMBOL_GPL(fwnode_name_eq); > > > -- > 2.47.3 > > --