2022-11-30 08:36:05

by Yang Yang

[permalink] [raw]
Subject: [PATCH linux-next] printk: use strscpy() to instead of strlcpy()

From: Xu Panda <[email protected]>

The implementation of strscpy() is more robust and safer.
That's now the recommended way to copy NUL terminated strings.

Signed-off-by: Xu Panda <[email protected]>
Signed-off-by: Yang Yang <[email protected]>
---
kernel/printk/printk.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 9ec101766471..7decf1e9c486 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -2480,7 +2480,7 @@ static int __add_preferred_console(char *name, int idx, char *options,
return -E2BIG;
if (!brl_options)
preferred_console = i;
- strlcpy(c->name, name, sizeof(c->name));
+ strscpy(c->name, name, sizeof(c->name));
c->options = options;
set_user_specified(c, user_specified);
braille_set_options(c, brl_options);
--
2.15.2


2022-11-30 09:30:50

by John Ogness

[permalink] [raw]
Subject: Re: [PATCH linux-next] printk: use strscpy() to instead of strlcpy()

On 2022-11-30, <[email protected]> wrote:
> From: Xu Panda <[email protected]>
>
> The implementation of strscpy() is more robust and safer.
> That's now the recommended way to copy NUL terminated strings.
>
> Signed-off-by: Xu Panda <[email protected]>
> Signed-off-by: Yang Yang <[email protected]>

This looks fine, but I am wondering if we should also replace all
strncpy() usage as well. AFAICT they can all be replaced. It would also
avoid the manual termination in console_setup().

John Ogness

2022-11-30 10:48:38

by Petr Mladek

[permalink] [raw]
Subject: Re: [PATCH linux-next] printk: use strscpy() to instead of strlcpy()

On Wed 2022-11-30 16:01:41, [email protected] wrote:
> From: Xu Panda <[email protected]>
>
> The implementation of strscpy() is more robust and safer.
> That's now the recommended way to copy NUL terminated strings.
>
> Signed-off-by: Xu Panda <[email protected]>
> Signed-off-by: Yang Yang <[email protected]>

Reviewed-by: Petr Mladek <[email protected]>

Best Regards,
Petr

2022-11-30 11:29:40

by Petr Mladek

[permalink] [raw]
Subject: Re: [PATCH linux-next] printk: use strscpy() to instead of strlcpy()

On Wed 2022-11-30 10:17:07, John Ogness wrote:
> On 2022-11-30, <[email protected]> wrote:
> > From: Xu Panda <[email protected]>
> >
> > The implementation of strscpy() is more robust and safer.
> > That's now the recommended way to copy NUL terminated strings.
> >
> > Signed-off-by: Xu Panda <[email protected]>
> > Signed-off-by: Yang Yang <[email protected]>
>
> This looks fine, but I am wondering if we should also replace all
> strncpy() usage as well. AFAICT they can all be replaced. It would also
> avoid the manual termination in console_setup().

I agree that it would be nice to replace strncpy() as well.
But it might be done in a separate patch.

Yang, Xu, would you like to do it, please?

Best Regards,
Petr