2023-11-03 20:03:47

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 2/4] leds: trigger: gpio: Convert to use kstrtox()

sscanf() is a heavy one and moreover requires additional boundary checks.
Convert driver to use kstrtou8() in gpio_trig_inverted_store().

Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/leds/trigger/ledtrig-gpio.c | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/drivers/leds/trigger/ledtrig-gpio.c b/drivers/leds/trigger/ledtrig-gpio.c
index d91ae7fde3cf..8a30f9228186 100644
--- a/drivers/leds/trigger/ledtrig-gpio.c
+++ b/drivers/leds/trigger/ledtrig-gpio.c
@@ -53,14 +53,12 @@ static ssize_t gpio_trig_brightness_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t n)
{
struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
- unsigned desired_brightness;
+ u8 desired_brightness;
int ret;

- ret = sscanf(buf, "%u", &desired_brightness);
- if (ret < 1 || desired_brightness > 255) {
- dev_err(dev, "invalid value\n");
- return -EINVAL;
- }
+ ret = kstrtou8(buf, 10, &desired_brightness);
+ if (ret)
+ return ret;

gpio_data->desired_brightness = desired_brightness;

--
2.40.0.1.gaa8946217a0b


2023-11-04 22:27:26

by Linus Walleij

[permalink] [raw]
Subject: Re: [PATCH v1 2/4] leds: trigger: gpio: Convert to use kstrtox()

On Fri, Nov 3, 2023 at 9:03 PM Andy Shevchenko
<[email protected]> wrote:

> sscanf() is a heavy one and moreover requires additional boundary checks.
> Convert driver to use kstrtou8() in gpio_trig_inverted_store().
>
> Signed-off-by: Andy Shevchenko <[email protected]>

Reviewed-by: Linus Walleij <[email protected]>

Yours,
Linus Walleij

2023-11-23 11:06:20

by Lee Jones

[permalink] [raw]
Subject: Re: [PATCH v1 2/4] leds: trigger: gpio: Convert to use kstrtox()

On Fri, 03 Nov 2023, Andy Shevchenko wrote:

> sscanf() is a heavy one and moreover requires additional boundary checks.
> Convert driver to use kstrtou8() in gpio_trig_inverted_store().
>
> Signed-off-by: Andy Shevchenko <[email protected]>
> ---
> drivers/leds/trigger/ledtrig-gpio.c | 10 ++++------
> 1 file changed, 4 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/leds/trigger/ledtrig-gpio.c b/drivers/leds/trigger/ledtrig-gpio.c
> index d91ae7fde3cf..8a30f9228186 100644
> --- a/drivers/leds/trigger/ledtrig-gpio.c
> +++ b/drivers/leds/trigger/ledtrig-gpio.c
> @@ -53,14 +53,12 @@ static ssize_t gpio_trig_brightness_store(struct device *dev,
> struct device_attribute *attr, const char *buf, size_t n)
> {
> struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
> - unsigned desired_brightness;
> + u8 desired_brightness;
> int ret;
>
> - ret = sscanf(buf, "%u", &desired_brightness);
> - if (ret < 1 || desired_brightness > 255) {
> - dev_err(dev, "invalid value\n");
> - return -EINVAL;
> - }
> + ret = kstrtou8(buf, 10, &desired_brightness);

Where does 10 come from?

> + if (ret)
> + return ret;
>
> gpio_data->desired_brightness = desired_brightness;
>
> --
> 2.40.0.1.gaa8946217a0b
>

--
Lee Jones [李琼斯]

2023-11-23 14:12:15

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v1 2/4] leds: trigger: gpio: Convert to use kstrtox()

On Thu, Nov 23, 2023 at 11:05:38AM +0000, Lee Jones wrote:
> On Fri, 03 Nov 2023, Andy Shevchenko wrote:

...

> > - ret = sscanf(buf, "%u", &desired_brightness);

"%u" (see man sscanf() for the details)

...

> > + ret = kstrtou8(buf, 10, &desired_brightness);
>
> Where does 10 come from?

See above.

--
With Best Regards,
Andy Shevchenko


2023-11-23 14:47:54

by Lee Jones

[permalink] [raw]
Subject: Re: [PATCH v1 2/4] leds: trigger: gpio: Convert to use kstrtox()

On Thu, 23 Nov 2023, Andy Shevchenko wrote:

> On Thu, Nov 23, 2023 at 11:05:38AM +0000, Lee Jones wrote:
> > On Fri, 03 Nov 2023, Andy Shevchenko wrote:
>
> ...
>
> > > - ret = sscanf(buf, "%u", &desired_brightness);
>
> "%u" (see man sscanf() for the details)
>
> ...
>
> > > + ret = kstrtou8(buf, 10, &desired_brightness);
> >
> > Where does 10 come from?
>
> See above.

Hmmm ...

I see that this is generally accepted. Although is looks like a recipe
for bugs to me. It's a shame we don't have something that can take a
variable, derives its type, then calculates the maximum length if
converted to a string.

Anyway, I'm probably babbling now ...

--
Lee Jones [李琼斯]