Skip to contentSkip to conversation

gpio: sim: replace memmove() + strstrip() with skip_spaces() + strim()

PATCH In review Bartosz Golaszewski 2 participants 3 messages Aug 12, 2023 – Aug 15, 2023 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio: sim: replace memmove() + strstrip() with skip_spaces() + strim()

From: Bartosz Golaszewski <[email protected]>

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.

---
 drivers/gpio/gpio-sim.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/gpio/gpio-sim.c +5 -6
index bb8fcf2a794c..106a73263f49 100644
@@ -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,
Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

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.

...

drivers/gpio/gpio-sim.c static bool gpio_sim_device_is_live_unlocked(struct gpio_sim_device *dev) · line 644

Quoted 4 lines
drivers/gpio/gpio-sim.c +2 -1
+	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().

Quoted 1 line

}

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 16 lines · Andy Shevchenko
Quoted 4 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

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.

...

drivers/gpio/gpio-sim.c static bool gpio_sim_device_is_live_unlocked(struct gpio_sim_device *dev) · line 644

Quoted 4 lines
drivers/gpio/gpio-sim.c +2 -1
+     strim(trimmed);
 
-     return dup;
+     return trimmed;
Quoted 7 lines

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

Quoted 10 lines

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

Quoted 1 line

}

--
With Best Regards,
Andy Shevchenko