2008-08-01 20:30:41

by Sven Wegener

[permalink] [raw]
Subject: [PATCH] leds: Avoid needless strlen() for attributes

There's no need for the additional call to strlen(), we can directly return the
value returned by sprintf(). We now return a length value that doesn't include
the final '\0', but user space shouldn't bother about it anyway.

Signed-off-by: Sven Wegener <[email protected]>
---
drivers/leds/led-class.c | 5 +----
drivers/leds/ledtrig-timer.c | 8 ++------
2 files changed, 3 insertions(+), 10 deletions(-)

Patch is based on your current (024e8ac) leds git tree head.

diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
index 559a408..1aa24d3 100644
--- a/drivers/leds/led-class.c
+++ b/drivers/leds/led-class.c
@@ -34,14 +34,11 @@ static ssize_t led_brightness_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *led_cdev = dev_get_drvdata(dev);
- ssize_t ret = 0;

/* no lock needed for this */
led_update_brightness(led_cdev);
- sprintf(buf, "%u\n", led_cdev->brightness);
- ret = strlen(buf) + 1;

- return ret;
+ return sprintf(buf, "%u\n", led_cdev->brightness);
}

static ssize_t led_brightness_store(struct device *dev,
diff --git a/drivers/leds/ledtrig-timer.c b/drivers/leds/ledtrig-timer.c
index 5c99f4f..db68196 100644
--- a/drivers/leds/ledtrig-timer.c
+++ b/drivers/leds/ledtrig-timer.c
@@ -70,9 +70,7 @@ static ssize_t led_delay_on_show(struct device *dev,
struct led_classdev *led_cdev = dev_get_drvdata(dev);
struct timer_trig_data *timer_data = led_cdev->trigger_data;

- sprintf(buf, "%lu\n", timer_data->delay_on);
-
- return strlen(buf) + 1;
+ return sprintf(buf, "%lu\n", timer_data->delay_on);
}

static ssize_t led_delay_on_store(struct device *dev,
@@ -116,9 +114,7 @@ static ssize_t led_delay_off_show(struct device *dev,
struct led_classdev *led_cdev = dev_get_drvdata(dev);
struct timer_trig_data *timer_data = led_cdev->trigger_data;

- sprintf(buf, "%lu\n", timer_data->delay_off);
-
- return strlen(buf) + 1;
+ return sprintf(buf, "%lu\n", timer_data->delay_off);
}

static ssize_t led_delay_off_store(struct device *dev,


2008-08-01 20:36:56

by Ray Lee

[permalink] [raw]
Subject: Re: [PATCH] leds: Avoid needless strlen() for attributes

On Fri, Aug 1, 2008 at 1:30 PM, Sven Wegener <[email protected]> wrote:
> There's no need for the additional call to strlen(), we can directly return the
> value returned by sprintf(). We now return a length value that doesn't include
> the final '\0', but user space shouldn't bother about it anyway.

Why not just add 1 to the return value of sprintf, and guarantee that
userspace sees the same thing before and after? It's simple enough to
do, and avoids guessing about what userspace may or may not be doing.

> Signed-off-by: Sven Wegener <[email protected]>
> ---
> drivers/leds/led-class.c | 5 +----
> drivers/leds/ledtrig-timer.c | 8 ++------
> 2 files changed, 3 insertions(+), 10 deletions(-)
>
> Patch is based on your current (024e8ac) leds git tree head.
>
> diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
> index 559a408..1aa24d3 100644
> --- a/drivers/leds/led-class.c
> +++ b/drivers/leds/led-class.c
> @@ -34,14 +34,11 @@ static ssize_t led_brightness_show(struct device *dev,
> struct device_attribute *attr, char *buf)
> {
> struct led_classdev *led_cdev = dev_get_drvdata(dev);
> - ssize_t ret = 0;
>
> /* no lock needed for this */
> led_update_brightness(led_cdev);
> - sprintf(buf, "%u\n", led_cdev->brightness);
> - ret = strlen(buf) + 1;
>
> - return ret;
> + return sprintf(buf, "%u\n", led_cdev->brightness);
> }
>
> static ssize_t led_brightness_store(struct device *dev,
> diff --git a/drivers/leds/ledtrig-timer.c b/drivers/leds/ledtrig-timer.c
> index 5c99f4f..db68196 100644
> --- a/drivers/leds/ledtrig-timer.c
> +++ b/drivers/leds/ledtrig-timer.c
> @@ -70,9 +70,7 @@ static ssize_t led_delay_on_show(struct device *dev,
> struct led_classdev *led_cdev = dev_get_drvdata(dev);
> struct timer_trig_data *timer_data = led_cdev->trigger_data;
>
> - sprintf(buf, "%lu\n", timer_data->delay_on);
> -
> - return strlen(buf) + 1;
> + return sprintf(buf, "%lu\n", timer_data->delay_on);
> }
>
> static ssize_t led_delay_on_store(struct device *dev,
> @@ -116,9 +114,7 @@ static ssize_t led_delay_off_show(struct device *dev,
> struct led_classdev *led_cdev = dev_get_drvdata(dev);
> struct timer_trig_data *timer_data = led_cdev->trigger_data;
>
> - sprintf(buf, "%lu\n", timer_data->delay_off);
> -
> - return strlen(buf) + 1;
> + return sprintf(buf, "%lu\n", timer_data->delay_off);
> }
>
> static ssize_t led_delay_off_store(struct device *dev,
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
>

2008-08-01 20:55:40

by Sven Wegener

[permalink] [raw]
Subject: Re: [PATCH] leds: Avoid needless strlen() for attributes

On Fri, 1 Aug 2008, Ray Lee wrote:

> On Fri, Aug 1, 2008 at 1:30 PM, Sven Wegener <[email protected]> wrote:
> > There's no need for the additional call to strlen(), we can directly return the
> > value returned by sprintf(). We now return a length value that doesn't include
> > the final '\0', but user space shouldn't bother about it anyway.
>
> Why not just add 1 to the return value of sprintf, and guarantee that
> userspace sees the same thing before and after? It's simple enough to
> do, and avoids guessing about what userspace may or may not be doing.

I thought about it, but all other places I looked at just do the return
sprintf(...); thing, so we are consistent with what the other drivers do.
And user space should be really ignoring the final '\0' currently. I know
that this can be considered some user space API change, but I think it is
in the range of what the API allows us. If noone has strong objections
against leaving the -1 out, I would keep it that way. But it's OK for me,
if someone really wants it there, but I don't think it's necessary.

Sven