Re: [PATCH] lib/string.c: implement stpcpy
From: Arvind Sankar
Date: Sat Aug 15 2020 - 17:55:28 EST
On Fri, Aug 14, 2020 at 09:33:10PM -0400, Arvind Sankar wrote:
> On Fri, Aug 14, 2020 at 05:24:15PM -0700, Nick Desaulniers wrote:
> > +#ifndef __HAVE_ARCH_STPCPY
> > +/**
> > + * stpcpy - copy a string from src to dest returning a pointer to the new end
> > + * of dest, including src's NULL terminator. May overrun dest.
> > + * @dest: pointer to end of string being copied into. Must be large enough
> > + * to receive copy.
> > + * @src: pointer to the beginning of string being copied from. Must not overlap
> > + * dest.
> > + *
> > + * stpcpy differs from strcpy in two key ways:
> > + * 1. inputs must not overlap.
> > + * 2. return value is the new NULL terminated character. (for strcpy, the
> > + * return value is a pointer to src.
> > + */
> > +#undef stpcpy
> > +char *stpcpy(char *__restrict__ dest, const char *__restrict__ src)
> > +{
> > + while ((*dest++ = *src++) != '\0')
> > + /* nothing */;
> > + return dest;
> > +}
> > +#endif
> > +
>
> Won't this return a pointer that's one _past_ the terminating NUL? I
> think you need the increments to be inside the loop body, rather than as
> part of the condition.
>
> Nit: NUL is more correct than NULL to refer to the string terminator.
Also, strcpy (at least the one in the C standard) is undefined if the
strings overlap, so that's not really a difference.