On Sun, Nov 12, 2023 at 10:10 PM Alejandro Colomar via Gcc
<gcc@gcc.gnu.org> wrote:
>
> Hi,
>
> The following code can be optimized to use memcpy(3), since the length
> of the copy is known (we've just called strnlen(3), and discarded the
> possibility of truncated lengths).
>
> $ cat strxcpy.c
> #include <stddef.h>
> #include <string.h>
> #include <sys/types.h>
>
> ssize_t
> strxcpy(char *restrict dst, const char *restrict src, size_t dsize)
> {
>         if (strnlen(src, dsize) == dsize)
>                 return -1;
>
>         return stpcpy(dst, src) - dst;
> }
>
>
> This compiles to stpcpy(3).  Similar code written with strcpy(3) also
> keeps strcpy(3).  But they could be optimized with mempcpy(3) or
> memcpy(3).

Can you file a bugreport please?  It looks like a transform for the
strlen pass.

>
> Thanks,
> Alex
>
> --
> <https://www.alejandro-colomar.es/>

Reply via email to