Skip to contentSkip to conversation

tools: gpio: replace strncpy with strscpy

PATCH In review Lucas Poupeau 3 participants 7 messages May 4 – May 5 2 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: gpio: replace strncpy with strscpy

Replace strncpy with strscpy in gpio-utils.c. strscpy is the preferred way to copy strings in the kernel as it guarantees NUL-termination and is more robust.

Reported by checkpatch.pl.

---
 tools/gpio/gpio-utils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/gpio/gpio-utils.c +1 -1
index 4096bcd511d1..d5d194be7cc2 100644
@@ -82,7 +82,7 @@ int gpiotools_request_line(const char *device_name, unsigned int *lines,
 		req.offsets[i] = lines[i];
 
 	req.config = *config;
-	strcpy(req.consumer, consumer);
+	strscpy(req.consumer, consumer);
 	req.num_lines = num_lines;
 
 	ret = ioctl(fd, GPIO_V2_GET_LINE_IOCTL, &req);
Signature · 1 line
2.54.0
#1 lore raw

Hi Lucas,

Quoted 5 lines · Lucas Poupeau

Replace strncpy with strscpy in gpio-utils.c. strscpy is the preferred way to copy strings in the kernel as it guarantees NUL-termination and is more robust.

Quoted 7 lines

Reported by checkpatch.pl.

---
 tools/gpio/gpio-utils.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)

[snip]

There's already someone working on this:

https://lore.kernel.org/linux-gpio/20260504134532.0438e390@pumpkin/

but to be honest this one is much better than that one. Just some quick recommendations, please change:

Quoted 1 line

+ strscpy(req.consumer, consumer);

to be something like:

	strscpy(req.consumer, consumer, sizeof(req.consumer));

Also, please double check the commit message and subject (says strncpy, should be strcpy). Once you fix those issues feel free to add my reviewed-by (Reviewed-by: Maxwell Doose <[email protected]>).

best regards,
max

A message that is not in the archive (sent off-list or not yet received): CAKYXYMRX-oV6iO+Kh+dV7_2BfsY9gjdXMw6cueOgYw2hjLWeaQ@mail.gmail.com

#3 lore raw

Hi Lucas,

Quoted 5 lines · Lucas

Hi Max,

Thank you for your review and your kind words!

Quoted 9 lines

I will follow your recommendations and send a v2 shortly. I'll make sure to update the function call to include the destination buffer size, and I'll also correct the commit message and subject as you pointed out.

I'll include your "Reviewed-by" tag in the new version.

Best regards,
Lucas

Actually, timeout, I'm not sure if strscpy() is allowed in tools/. I'm going to double-check and I'll let you know if it is.

best regards,
max

#4 lore raw
Quoted 19 lines · Maxwell Doose

Hi Lucas,

Quoted 5 lines · Lucas

Hi Max,

Thank you for your review and your kind words!

Quoted 9 lines

I will follow your recommendations and send a v2 shortly. I'll make sure to update the function call to include the destination buffer size, and I'll also correct the commit message and subject as you pointed out.

I'll include your "Reviewed-by" tag in the new version.

Best regards,
Lucas

Quoted 5 lines

Actually, timeout, I'm not sure if strscpy() is allowed in tools/. I'm going to double-check and I'll let you know if it is.

best regards,
max

Alright, just checked tools/include/linux/string.h, this can't be done unfortunately. I like the idea though, perhaps you should submit a patch adding strscpy there.

best regards,
max

A message that is not in the archive (sent off-list or not yet received): CAKYXYMQQ2UxLJw=2O2y=fLL6Kpp3eUiaH7Rx4MF5H7wcoYzcLQ@mail.gmail.com

#6 lore raw
Quoted 9 lines · Lucas

That's a great suggestion! I would love to try adding strscpy to tools/include/linux/string.h.

It might take me a little more time than a simple cleanup as I need to make sure I don't break the tools build, but I'll work on it and send a patch as soon as it's ready.

Thanks for the guidance!

Best regards, Lucas

Of course, but again, don't send html email or top-post, read the mailing list etiquette at https://subspace.kernel.org/etiquette.html.

best regards,
max

#7 lore raw
Quoted 9 lines · Maxwell Doose
Quoted 7 lines · Lucas

That's a great suggestion! I would love to try adding strscpy to tools/include/linux/string.h.

It might take me a little more time than a simple cleanup as I need to make sure I don't break the tools build, but I'll work on it and send a patch as soon as it's ready.

Thanks for the guidance!

Quoted 8 lines
Quoted 2 lines

Best regards, Lucas

Of course, but again, don't send html email or top-post, read the mailing list etiquette at https://subspace.kernel.org/etiquette.html.

best regards,
max

Are you guys two AI bots just talking nonsense to each other? This is a user-space tool, there's no strscpy() in libc...

Reading this was a waste of two minutes I'm not getting back.

Bart

#8 lore raw
Quoted 6 lines · Bartosz Golaszewski

Are you guys two AI bots just talking nonsense to each other? This is a user-space tool, there's no strscpy() in libc...

Reading this was a waste of two minutes I'm not getting back.

??
No, some confusion arised as I'm typically working in iio and gpio. I'm more used to having strscpy() readily available and thought that not having strscpy() in the tools-specific libraries was strange. But the stupid thing is that it was aliased to strcpy in tools/include/linux/string.h (#define strscpy strcpy). I'm sorry, but we really need to at least get that macro removed, it's pretty precarious. Besides, we're not trying to change glibc, we're just trying to move in the latest and much safer function...