* [PATCH v1 1/1] leds: trigger: gpio: Convert to use kstrtox()
@ 2022-06-09 16:18 Andy Shevchenko
2022-06-10 11:39 ` Andy Shevchenko
0 siblings, 1 reply; 2+ messages in thread
From: Andy Shevchenko @ 2022-06-09 16:18 UTC (permalink / raw)
To: Andy Shevchenko, linux-leds, linux-kernel; +Cc: Pavel Machek
sscanf() is a heavy one and moreover requires additional boundary checks.
Convert driver to use kstrtox() and replace kstrtoul() by kstrtobool()
in gpio_trig_inverted_store().
While here, check the desired brightness against maximum defined for
a certain LED.
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
drivers/leds/trigger/ledtrig-gpio.c | 21 +++++++++------------
1 file changed, 9 insertions(+), 12 deletions(-)
diff --git a/drivers/leds/trigger/ledtrig-gpio.c b/drivers/leds/trigger/ledtrig-gpio.c
index 0120faa3dafa..19e43333457a 100644
--- a/drivers/leds/trigger/ledtrig-gpio.c
+++ b/drivers/leds/trigger/ledtrig-gpio.c
@@ -60,10 +60,10 @@ static ssize_t gpio_trig_brightness_store(struct device *dev,
unsigned desired_brightness;
int ret;
- ret = sscanf(buf, "%u", &desired_brightness);
- if (ret < 1 || desired_brightness > 255) {
+ ret = kstrtouint(buf, 10, &desired_brightness);
+ if (ret || desired_brightness > gpio_data->led->max_brightness) {
dev_err(dev, "invalid value\n");
- return -EINVAL;
+ return ret ?: -EINVAL;
}
gpio_data->desired_brightness = desired_brightness;
@@ -86,16 +86,13 @@ static ssize_t gpio_trig_inverted_store(struct device *dev,
{
struct led_classdev *led = led_trigger_get_led(dev);
struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev);
- unsigned long inverted;
+ bool inverted;
int ret;
- ret = kstrtoul(buf, 10, &inverted);
- if (ret < 0)
+ ret = kstrtobool(buf, &inverted);
+ if (ret)
return ret;
- if (inverted > 1)
- return -EINVAL;
-
gpio_data->inverted = inverted;
/* After inverting, we need to update the LED. */
@@ -123,10 +120,10 @@ static ssize_t gpio_trig_gpio_store(struct device *dev,
unsigned gpio;
int ret;
- ret = sscanf(buf, "%u", &gpio);
- if (ret < 1) {
+ ret = kstrtouint(buf, 10, &gpio);
+ if (ret) {
dev_err(dev, "couldn't read gpio number\n");
- return -EINVAL;
+ return ret;
}
if (gpio_data->gpio == gpio)
--
2.35.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v1 1/1] leds: trigger: gpio: Convert to use kstrtox()
2022-06-09 16:18 [PATCH v1 1/1] leds: trigger: gpio: Convert to use kstrtox() Andy Shevchenko
@ 2022-06-10 11:39 ` Andy Shevchenko
0 siblings, 0 replies; 2+ messages in thread
From: Andy Shevchenko @ 2022-06-10 11:39 UTC (permalink / raw)
To: linux-leds, linux-kernel; +Cc: Pavel Machek
On Thu, Jun 09, 2022 at 07:18:29PM +0300, Andy Shevchenko wrote:
> sscanf() is a heavy one and moreover requires additional boundary checks.
> Convert driver to use kstrtox() and replace kstrtoul() by kstrtobool()
> in gpio_trig_inverted_store().
> While here, check the desired brightness against maximum defined for
> a certain LED.
I realize that this part may be wrong. I have to drop it in the next version.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-06-10 11:39 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-09 16:18 [PATCH v1 1/1] leds: trigger: gpio: Convert to use kstrtox() Andy Shevchenko
2022-06-10 11:39 ` Andy Shevchenko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox