public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] gpio: sim: replace memmove() + strstrip() with skip_spaces() + strim()
@ 2023-08-12 18:57 Bartosz Golaszewski
  2023-08-15 10:34 ` Andy Shevchenko
  0 siblings, 1 reply; 3+ messages in thread
From: Bartosz Golaszewski @ 2023-08-12 18:57 UTC (permalink / raw)
  To: Linus Walleij, Andy Shevchenko, Kent Gibson
  Cc: linux-gpio, linux-kernel, Bartosz Golaszewski

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Turns out we can avoid the memmove() by using skip_spaces() and strim().
We did that in gpio-consumer, let's do it in gpio-sim.

Suggested-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 drivers/gpio/gpio-sim.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c
index bb8fcf2a794c..106a73263f49 100644
--- a/drivers/gpio/gpio-sim.c
+++ b/drivers/gpio/gpio-sim.c
@@ -633,16 +633,15 @@ static bool gpio_sim_device_is_live_unlocked(struct gpio_sim_device *dev)
 
 static char *gpio_sim_strdup_trimmed(const char *str, size_t count)
 {
-	char *dup, *trimmed;
+	char *trimmed;
 
-	dup = kstrndup(str, count, GFP_KERNEL);
-	if (!dup)
+	trimmed = kstrndup(skip_spaces(str), count, GFP_KERNEL);
+	if (!trimmed)
 		return NULL;
 
-	trimmed = strstrip(dup);
-	memmove(dup, trimmed, strlen(trimmed) + 1);
+	strim(trimmed);
 
-	return dup;
+	return trimmed;
 }
 
 static ssize_t gpio_sim_device_config_dev_name_show(struct config_item *item,
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] gpio: sim: replace memmove() + strstrip() with skip_spaces() + strim()
  2023-08-12 18:57 [PATCH] gpio: sim: replace memmove() + strstrip() with skip_spaces() + strim() Bartosz Golaszewski
@ 2023-08-15 10:34 ` Andy Shevchenko
  2023-08-15 18:45   ` Bartosz Golaszewski
  0 siblings, 1 reply; 3+ messages in thread
From: Andy Shevchenko @ 2023-08-15 10:34 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Linus Walleij, Kent Gibson, linux-gpio, linux-kernel,
	Bartosz Golaszewski

On Sat, Aug 12, 2023 at 08:57:48PM +0200, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> 
> Turns out we can avoid the memmove() by using skip_spaces() and strim().
> We did that in gpio-consumer, let's do it in gpio-sim.

Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

...

> +	strim(trimmed);
>  
> -	return dup;
> +	return trimmed;

Can be also

	return strim(trimmed);

If it's only about \n replacement, then

	return strreplace(trimmed, '\n', '\0');

would work and in the next release be changed to kstrdup_and_replace().

>  }

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] gpio: sim: replace memmove() + strstrip() with skip_spaces() + strim()
  2023-08-15 10:34 ` Andy Shevchenko
@ 2023-08-15 18:45   ` Bartosz Golaszewski
  0 siblings, 0 replies; 3+ messages in thread
From: Bartosz Golaszewski @ 2023-08-15 18:45 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Linus Walleij, Kent Gibson, linux-gpio, linux-kernel,
	Bartosz Golaszewski

On Tue, Aug 15, 2023 at 12:34 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> On Sat, Aug 12, 2023 at 08:57:48PM +0200, Bartosz Golaszewski wrote:
> > From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> >
> > Turns out we can avoid the memmove() by using skip_spaces() and strim().
> > We did that in gpio-consumer, let's do it in gpio-sim.
>
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
>
> ...
>
> > +     strim(trimmed);
> >
> > -     return dup;
> > +     return trimmed;
>
> Can be also
>
>         return strim(trimmed);
>
> If it's only about \n replacement, then
>
>         return strreplace(trimmed, '\n', '\0');

No, the user is free to pass all kinds of whitespaces after the
string. I'll queue it with the above change. Thanks.

Bart

>
> would work and in the next release be changed to kstrdup_and_replace().
>
> >  }
>
> --
> With Best Regards,
> Andy Shevchenko
>
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-08-15 18:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-12 18:57 [PATCH] gpio: sim: replace memmove() + strstrip() with skip_spaces() + strim() Bartosz Golaszewski
2023-08-15 10:34 ` Andy Shevchenko
2023-08-15 18:45   ` Bartosz Golaszewski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox