linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] gpio: gpiolib: replace deprecated strcpy() with strscpy()
@ 2025-11-01  7:41 Dmitry Antipov
  2025-11-01  7:41 ` [PATCH] gpio: virtuser: avoid strlen() in gpio_virtuser_direction_do_read() Dmitry Antipov
  2025-11-10 22:06 ` [PATCH] gpio: gpiolib: replace deprecated strcpy() with strscpy() Linus Walleij
  0 siblings, 2 replies; 5+ messages in thread
From: Dmitry Antipov @ 2025-11-01  7:41 UTC (permalink / raw)
  To: Bartosz Golaszewski, Linus Walleij; +Cc: linux-gpio, Dmitry Antipov

Prefer 'strscpy()' over deprecated 'strcpy()' in 'desc_set_label()'.

Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
 drivers/gpio/gpiolib.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 9952e412da50..5c0cd096642b 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -146,12 +146,13 @@ static int desc_set_label(struct gpio_desc *desc, const char *label)
 	struct gpio_desc_label *new = NULL, *old;
 
 	if (label) {
-		new = kzalloc(struct_size(new, str, strlen(label) + 1),
-			      GFP_KERNEL);
+		int size = strlen(label) + 1;
+
+		new = kzalloc(struct_size(new, str, size), GFP_KERNEL);
 		if (!new)
 			return -ENOMEM;
 
-		strcpy(new->str, label);
+		strscpy(new->str, label, size);
 	}
 
 	old = rcu_replace_pointer(desc->label, new, 1);
-- 
2.51.0


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

* [PATCH] gpio: virtuser: avoid strlen() in gpio_virtuser_direction_do_read()
  2025-11-01  7:41 [PATCH] gpio: gpiolib: replace deprecated strcpy() with strscpy() Dmitry Antipov
@ 2025-11-01  7:41 ` Dmitry Antipov
  2025-11-10 22:07   ` Linus Walleij
  2025-11-10 22:06 ` [PATCH] gpio: gpiolib: replace deprecated strcpy() with strscpy() Linus Walleij
  1 sibling, 1 reply; 5+ messages in thread
From: Dmitry Antipov @ 2025-11-01  7:41 UTC (permalink / raw)
  To: Bartosz Golaszewski, Linus Walleij; +Cc: linux-gpio, Dmitry Antipov

Since 'snprintf()' returns the number of characters emitted and buffer
size guarantees that there will be no overflow, an extra call to
'strlen()' in 'gpio_virtuser_direction_do_read()' may be dropped.

Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
 drivers/gpio/gpio-virtuser.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/gpio/gpio-virtuser.c b/drivers/gpio/gpio-virtuser.c
index a10eab7d2617..0f959254fda5 100644
--- a/drivers/gpio/gpio-virtuser.c
+++ b/drivers/gpio/gpio-virtuser.c
@@ -342,7 +342,7 @@ static ssize_t gpio_virtuser_direction_do_read(struct file *file,
 	struct gpio_virtuser_line_data *data = file->private_data;
 	struct gpio_desc *desc = data->ad.desc;
 	char buf[32];
-	int dir;
+	int dir, len;
 
 	if (!atomic)
 		dir = gpiod_get_direction(desc);
@@ -351,9 +351,9 @@ static ssize_t gpio_virtuser_direction_do_read(struct file *file,
 	if (dir < 0)
 		return dir;
 
-	snprintf(buf, sizeof(buf), "%s\n", dir ? "input" : "output");
+	len = snprintf(buf, sizeof(buf), "%s\n", dir ? "input" : "output");
 
-	return simple_read_from_buffer(user_buf, size, ppos, buf, strlen(buf));
+	return simple_read_from_buffer(user_buf, size, ppos, buf, len);
 }
 
 static int gpio_virtuser_set_direction(struct gpio_desc *desc, int dir, int val)
-- 
2.51.0


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

* Re: [PATCH] gpio: gpiolib: replace deprecated strcpy() with strscpy()
  2025-11-01  7:41 [PATCH] gpio: gpiolib: replace deprecated strcpy() with strscpy() Dmitry Antipov
  2025-11-01  7:41 ` [PATCH] gpio: virtuser: avoid strlen() in gpio_virtuser_direction_do_read() Dmitry Antipov
@ 2025-11-10 22:06 ` Linus Walleij
  1 sibling, 0 replies; 5+ messages in thread
From: Linus Walleij @ 2025-11-10 22:06 UTC (permalink / raw)
  To: Dmitry Antipov; +Cc: Bartosz Golaszewski, linux-gpio

On Sat, Nov 1, 2025 at 8:41 AM Dmitry Antipov <dmantipov@yandex.ru> wrote:

> Prefer 'strscpy()' over deprecated 'strcpy()' in 'desc_set_label()'.
>
> Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>

Looks correct and makes sense:
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij

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

* Re: [PATCH] gpio: virtuser: avoid strlen() in gpio_virtuser_direction_do_read()
  2025-11-01  7:41 ` [PATCH] gpio: virtuser: avoid strlen() in gpio_virtuser_direction_do_read() Dmitry Antipov
@ 2025-11-10 22:07   ` Linus Walleij
  2025-11-12  9:43     ` Andy Shevchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Linus Walleij @ 2025-11-10 22:07 UTC (permalink / raw)
  To: Dmitry Antipov; +Cc: Bartosz Golaszewski, linux-gpio

On Sat, Nov 1, 2025 at 8:41 AM Dmitry Antipov <dmantipov@yandex.ru> wrote:

> Since 'snprintf()' returns the number of characters emitted and buffer
> size guarantees that there will be no overflow, an extra call to
> 'strlen()' in 'gpio_virtuser_direction_do_read()' may be dropped.
>
> Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>

Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij

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

* Re: [PATCH] gpio: virtuser: avoid strlen() in gpio_virtuser_direction_do_read()
  2025-11-10 22:07   ` Linus Walleij
@ 2025-11-12  9:43     ` Andy Shevchenko
  0 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2025-11-12  9:43 UTC (permalink / raw)
  To: Linus Walleij; +Cc: Dmitry Antipov, Bartosz Golaszewski, linux-gpio

On Mon, Nov 10, 2025 at 11:07:22PM +0100, Linus Walleij wrote:
> On Sat, Nov 1, 2025 at 8:41 AM Dmitry Antipov <dmantipov@yandex.ru> wrote:
> 
> > Since 'snprintf()' returns the number of characters emitted and buffer
> > size guarantees that there will be no overflow, an extra call to
> > 'strlen()' in 'gpio_virtuser_direction_do_read()' may be dropped.
> >
> > Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
> 
> Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Strictly speaking this is incorrect change due to use of snprintf().
One should have converted that one to scnprintf().

While at it, we also have str_input_output() helper, but this is not
exactly related to the change.

-- 
With Best Regards,
Andy Shevchenko



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

end of thread, other threads:[~2025-11-12  9:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-01  7:41 [PATCH] gpio: gpiolib: replace deprecated strcpy() with strscpy() Dmitry Antipov
2025-11-01  7:41 ` [PATCH] gpio: virtuser: avoid strlen() in gpio_virtuser_direction_do_read() Dmitry Antipov
2025-11-10 22:07   ` Linus Walleij
2025-11-12  9:43     ` Andy Shevchenko
2025-11-10 22:06 ` [PATCH] gpio: gpiolib: replace deprecated strcpy() with strscpy() Linus Walleij

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).