This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] Faster strn?cat
On Sun, Jun 17, 2012 at 10:53:51AM +0300, Kalle Olavi Niemitalo wrote:
> Ondrej Bilka <neleai@seznam.cz> writes:
>
> > char *
> > -STRNCAT (char *s1, const char *s2, size_t n)
> > +STRNCAT (char *d, const char *s, size_t n)
> > {
>
> [...]
>
> > + return strncpy(d+strlen(d),s,n);
> > }
>
> Returns the wrong value; should return d always.
> Does not append a null character if strlen(s) >= n.
> Appends too many null characters if strlen(s) + 1 < n.
>
> It seems test_strncat in string/tester.c should have detected
> these errors. Did you run the test suite?
Only for strcat. Idea to include strncat came later.
Perhaps following could suffice.
static char * strncpy2(char *d,const char *s,size_t n){
size_t ss=strnlen(s,n);
memcpy(d,s,ss);
d[ss]=0;
return d;
}
STRNCAT (char *d, const char *s, size_t n){
strncpy2(d+strlen(d),s,n);
return d;
}