[PATCH] gpio: sim: Remove intermediate pointer variable and harden function

Maxwell Doose posted 1 patch 1 month, 3 weeks ago
drivers/gpio/gpio-sim.c | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
[PATCH] gpio: sim: Remove intermediate pointer variable and harden function
Posted by Maxwell Doose 1 month, 3 weeks ago
Remove the *pdev intermediate variable and directly dereference the
pointer. While at it, replace sprintf() calls with sysfs_emit() to
harden the driver.

Signed-off-by: Maxwell Doose <m32285159@gmail.com>
---
 drivers/gpio/gpio-sim.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c
index 13b87c8e6d0c..3c230f94eea2 100644
--- a/drivers/gpio/gpio-sim.c
+++ b/drivers/gpio/gpio-sim.c
@@ -691,15 +691,13 @@ static ssize_t gpio_sim_device_config_dev_name_show(struct config_item *item,
 						    char *page)
 {
 	struct gpio_sim_device *dev = to_gpio_sim_device(item);
-	struct platform_device *pdev;
 
 	guard(mutex)(&dev->lock);
 
-	pdev = dev->probe_data.pdev;
-	if (pdev)
-		return sprintf(page, "%s\n", dev_name(&pdev->dev));
+	if (dev->probe_data.pdev)
+		return sysfs_emit(page, "%s\n", dev_name(&dev->probe_data.pdev->dev));
 
-	return sprintf(page, "gpio-sim.%d\n", dev->id);
+	return sysfs_emit(page, "gpio-sim.%d\n", dev->id);
 }
 
 CONFIGFS_ATTR_RO(gpio_sim_device_config_, dev_name);
-- 
2.53.0
Re: [PATCH] gpio: sim: Remove intermediate pointer variable and harden function
Posted by Bartosz Golaszewski 1 month, 3 weeks ago
On Fri, Apr 24, 2026 at 12:32 AM Maxwell Doose <m32285159@gmail.com> wrote:
>
> Remove the *pdev intermediate variable and directly dereference the
> pointer. While at it, replace sprintf() calls with sysfs_emit() to
> harden the driver.
>
> Signed-off-by: Maxwell Doose <m32285159@gmail.com>
> ---
>  drivers/gpio/gpio-sim.c | 8 +++-----
>  1 file changed, 3 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c
> index 13b87c8e6d0c..3c230f94eea2 100644
> --- a/drivers/gpio/gpio-sim.c
> +++ b/drivers/gpio/gpio-sim.c
> @@ -691,15 +691,13 @@ static ssize_t gpio_sim_device_config_dev_name_show(struct config_item *item,
>                                                     char *page)
>  {
>         struct gpio_sim_device *dev = to_gpio_sim_device(item);
> -       struct platform_device *pdev;
>
>         guard(mutex)(&dev->lock);
>
> -       pdev = dev->probe_data.pdev;
> -       if (pdev)
> -               return sprintf(page, "%s\n", dev_name(&pdev->dev));
> +       if (dev->probe_data.pdev)
> +               return sysfs_emit(page, "%s\n", dev_name(&dev->probe_data.pdev->dev));

Do you believe the code looks better with more layered dereferences?

>
> -       return sprintf(page, "gpio-sim.%d\n", dev->id);
> +       return sysfs_emit(page, "gpio-sim.%d\n", dev->id);

sprintf() is safe here as we cannot possibly exceed PAGE_SIZE with
this format but if you really want to do this, than please send a
separate patch converting all configfs show callbacks in the driver.

Bart

>  }
>
>  CONFIGFS_ATTR_RO(gpio_sim_device_config_, dev_name);
> --
> 2.53.0
>
Re: [PATCH] gpio: sim: Remove intermediate pointer variable and harden function
Posted by Maxwell Doose 1 month, 3 weeks ago
On Fri, Apr 24, 2026 at 2:54 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
>
> Do you believe the code looks better with more layered dereferences?
>

I think some people might value the more explicit dereferencing, and with this
we also won't need to handle the overhead of assigning another pointer, but if
you want this removed thats fine by me.

> sprintf() is safe here as we cannot possibly exceed PAGE_SIZE with
> this format but if you really want to do this, than please send a
> separate patch converting all configfs show callbacks in the driver.

I can do that, but I'd prefer to separate the functional changes with
this one, so
expect another patch that fixes these. I'll remove those sysfs_emit changes for
now so that the commit history will be clean for when I come back with this
second patch.

best regards,
maxwell
Re: [PATCH] gpio: sim: Remove intermediate pointer variable and harden function
Posted by Bartosz Golaszewski 1 month, 3 weeks ago
On Fri, Apr 24, 2026 at 3:05 PM Maxwell Doose <m32285159@gmail.com> wrote:
>
> On Fri, Apr 24, 2026 at 2:54 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
> >
> > Do you believe the code looks better with more layered dereferences?
> >
>
> I think some people might value the more explicit dereferencing, and with this
> we also won't need to handle the overhead of assigning another pointer, but if
> you want this removed thats fine by me.
>

I prefer to keep it as is.

> > sprintf() is safe here as we cannot possibly exceed PAGE_SIZE with
> > this format but if you really want to do this, than please send a
> > separate patch converting all configfs show callbacks in the driver.
>
> I can do that, but I'd prefer to separate the functional changes with
> this one, so
> expect another patch that fixes these. I'll remove those sysfs_emit changes for
> now so that the commit history will be clean for when I come back with this
> second patch.
>

Sure, let's drop the first part and do the conversion of the configfs
show callbacks

Bart
Re: [PATCH] gpio: sim: Remove intermediate pointer variable and harden function
Posted by Maxwell Doose 1 month, 3 weeks ago
On Fri, Apr 24, 2026 at 8:13 AM Bartosz Golaszewski <brgl@kernel.org> wrote:
>
> On Fri, Apr 24, 2026 at 3:05 PM Maxwell Doose <m32285159@gmail.com> wrote:
> > I think some people might value the more explicit dereferencing, and with this
> > we also won't need to handle the overhead of assigning another pointer, but if
> > you want this removed thats fine by me.
> >
>
> I prefer to keep it as is.
>
> >
> > I can do that, but I'd prefer to separate the functional changes with
> > this one, so
> > expect another patch that fixes these. I'll remove those sysfs_emit changes for
> > now so that the commit history will be clean for when I come back with this
> > second patch.
> >
>
> Sure, let's drop the first part and do the conversion of the configfs
> show callbacks
>
> Bart

Sounds good. I'll get that patch out this afternoon, otherwise I'll be
away for most
of the weekend and I'll get it out Sunday evening.

best regards,
maxwell