Re: [PATCH linux-next v2] x86/platform/uv: use strscpy to instead of strncpy()

From: Andy Shevchenko
Date: Mon Dec 05 2022 - 08:14:09 EST


On Mon, Dec 5, 2022 at 1:42 PM <yang.yang29@xxxxxxxxxx> wrote:
>
> From: Xu Panda <xu.panda@xxxxxxxxxx>

Thank you for an update, my comments below.

> The implementation of strscpy() is more robust and safer.
> That's now the recommended way to copy NUL terminated strings.
> ---
> change for v2
> - fix the mistake and make the code better and simpler,
> thanks to Andy Shevchenko.
> ---

This is (the changelog) in the wrong place.

> Signed-off-by: Xu Panda <xu.panda@xxxxxxxxxx>
> Signed-off-by: Yang Yang <yang.yang29@xxxxxxx>

> Signed-off-by: Andy Shevchenko <andy.shevchenko@xxxxxxxxx>

No, I haven't given you this tag, sorry. The changelog is enough to give credit.

...

> char arg[ACTION_LEN], *p;
>
> /* (remove possible '\n') */
> - strncpy(arg, val, ACTION_LEN - 1);
> - arg[ACTION_LEN - 1] = '\0';
> - p = strchr(arg, '\n');
> - if (p)
> - *p = '\0';
> + strscpy(arg, val, strnchrnul(val, ACTION_LEN, '\n') - val);

And even better is to replace ACTION_LEN by sizeof(arg).

--
With Best Regards,
Andy Shevchenko