Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx+linux-hardening@kernel.org>
To: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	 Daniel Scally <djrscally@gmail.com>,
	Heikki Krogerus <heikki.krogerus@linux.intel.com>,
	 Sakari Ailus <sakari.ailus@linux.intel.com>,
	Bartosz Golaszewski <brgl@kernel.org>,
	 Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	 Danilo Krummrich <dakr@kernel.org>,
	Johan Hovold <johan@kernel.org>,
	 Hans Verkuil <hverkuil@kernel.org>,
	Laurent Pinchart <laurent.pinchart@ideasonboard.com>,
	 Kees Cook <kees@kernel.org>, Andy Shevchenko <andy@kernel.org>,
	Len Brown <lenb@kernel.org>,  Rob Herring <robh@kernel.org>,
	Saravana Kannan <saravanak@kernel.org>,
	 Linus Walleij <linusw@kernel.org>,
	linux-acpi@vger.kernel.org, driver-core@lists.linux.dev,
	 linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org,
	devicetree@vger.kernel.org,  linux-gpio@vger.kernel.org
Subject: Re: [PATCH 1/9] device property: fix fwnode_name_eq() for empty node names
Date: Thu, 5 Nov 2026 12:12:14 +0100	[thread overview]
Message-ID: <auxXdlSWo2zG7iFP@devuan> (raw)
In-Reply-To: <20261104-fwnode-for-each-prop-v1-1-6e28fe1c72c4@oss.qualcomm.com>

[-- Attachment #1: Type: text/plain, Size: 2971 bytes --]

Hi Bartosz,

> Date: 2026-11-04 15:03:43+0100
> From: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
>
> 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 <bartosz.golaszewski@oss.qualcomm.com>
> ---
>  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
> 
> 

-- 
<https://www.alejandro-colomar.es>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  parent reply	other threads:[~2026-11-05 11:12 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-04 14:03 [PATCH 0/9] gpio: make gpiolib-shared more fwnode-centric Bartosz Golaszewski
2026-11-04 14:03 ` [PATCH 1/9] device property: fix fwnode_name_eq() for empty node names Bartosz Golaszewski
2026-11-05 10:00   ` Sakari Ailus
2026-11-05 11:12   ` Alejandro Colomar [this message]
2026-11-04 14:03 ` [PATCH 2/9] lib: string_helpers: implement a CLASS() for string arrays Bartosz Golaszewski
2026-11-04 21:07   ` Andy Shevchenko
2026-11-04 14:03 ` [PATCH 3/9] fwnode: define the property_get_names() operation Bartosz Golaszewski
2026-11-04 14:03 ` [PATCH 4/9] device property: provide fwnode_property_get_names() Bartosz Golaszewski
2026-11-04 14:12   ` sashiko-bot
2026-11-04 21:17   ` Andy Shevchenko
2026-11-04 14:03 ` [PATCH 5/9] of: property: implement the .property_get_names() fwnode operation Bartosz Golaszewski
2026-11-04 14:18   ` sashiko-bot
2026-11-04 21:21   ` Andy Shevchenko
2026-11-04 14:03 ` [PATCH 6/9] fwnode: define the count_references() " Bartosz Golaszewski
2026-11-04 14:03 ` [PATCH 7/9] device property: provide fwnode_property_count_references() Bartosz Golaszewski
2026-11-04 14:03 ` [PATCH 8/9] of: property: implement the count_references() fwnode operation Bartosz Golaszewski
2026-11-04 15:33   ` Rob Herring
2026-11-04 14:03 ` [PATCH 9/9] gpio: shared: generalize most code to firmware nodes Bartosz Golaszewski
2026-11-04 14:15   ` sashiko-bot
2026-11-04 21:33   ` Andy Shevchenko
2026-11-05  6:28   ` kernel test robot
2026-11-05  7:34   ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=auxXdlSWo2zG7iFP@devuan \
    --to=alx+linux-hardening@kernel.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=andy@kernel.org \
    --cc=bartosz.golaszewski@oss.qualcomm.com \
    --cc=brgl@kernel.org \
    --cc=dakr@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=djrscally@gmail.com \
    --cc=driver-core@lists.linux.dev \
    --cc=gregkh@linuxfoundation.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=hverkuil@kernel.org \
    --cc=johan@kernel.org \
    --cc=kees@kernel.org \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=lenb@kernel.org \
    --cc=linusw@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=robh@kernel.org \
    --cc=sakari.ailus@linux.intel.com \
    --cc=saravanak@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox