tools: gpio: replace strncpy with strscpy
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
@@ -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
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.
Signed-off-by Lucas Poupeau <[email protected]>---
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
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
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 intools/. 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
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
Bartosz Golaszewski
maintainerQuoted 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
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...