Re: [PATCH] x86/platform/uv: refactor deprecated strcpy and strncpy
From: Justin Stitt
Date: Wed Aug 23 2023 - 18:51:11 EST
On Wed, Aug 23, 2023 at 4:07 AM Andy Shevchenko
<andy.shevchenko@xxxxxxxxx> wrote:
>
> On Wed, Aug 23, 2023 at 1:32 AM Justin Stitt <justinstitt@xxxxxxxxxx> wrote:
> >
> > Both `strncpy` and `strcpy` are deprecated for use on NUL-terminated
> > destination strings [1].
> >
> > A suitable replacement is `strscpy` [2] due to the fact that it
> > guarantees NUL-termination on its destination buffer argument which is
> > _not_ the case for `strncpy` or `strcpy`!
> >
> > In this case, we can drop both the forced NUL-termination and the `... -1` from:
> > | strncpy(arg, val, ACTION_LEN - 1);
> > as `strscpy` implicitly has this behavior.
>
> ...
>
> > char arg[ACTION_LEN], *p;
> >
> > /* (remove possible '\n') */
> > - strncpy(arg, val, ACTION_LEN - 1);
> > - arg[ACTION_LEN - 1] = '\0';
> > + strscpy(arg, val, ACTION_LEN);
> > p = strchr(arg, '\n');
> > if (p)
> > *p = '\0';
>
> https://lore.kernel.org/all/202212091545310085328@xxxxxxxxxx/
>
> ...
>
> > + strscpy(uv_nmi_action, arg, strlen(uv_nmi_action));
>
> strlen() on the destination?!
>
> ...
>
> > - strncpy(uv_nmi_action, "dump", strlen(uv_nmi_action));
> > + strscpy(uv_nmi_action, "dump", strlen(uv_nmi_action));
>
> Again, this is weird.
This is a common pattern with `strxcpy` and `sizeof` if you `$ rg
"strncpy\(.*sizeof"`. Do you recommend I switch the strlen(dest) to
strlen(src)? I only kept as-is because that's what was there
originally and I assumed some greater purpose of it.
>
> --
> With Best Regards,
> Andy Shevchenko