Re: [PATCH 1/5] Make xstrndup common
- From
Daniel Barkalow <barkalow@iabervon.org>
- Date
- Apr 29, 2007, 18:19 UTC
- Message-ID
- <Pine.LNX.4.64.0704291416370.28708@iabervon.org>
- In-Reply-To
- <46339819.8030007@freedesktop.org>
On Sat, 28 Apr 2007, Josh Triplett wrote:
Show 17 quoted lines
> Daniel Barkalow wrote:
> > It was implemented in commit.c; move it with the other x memory functions.
> [...]
> > +static inline char *xstrndup(const char *str, int len)
> > +{
> > + char *ret = xmalloc(len + 1);
> > + memcpy(ret, str, len);
> > + ret[len] = '\0';
> > + return ret;
> > +}
> > +
>
> I don't know if it matters, but this definition of xstrndup, like the version
> in commit.c, doesn't match the definition of strndup. strndup duplicates a
> string, copying up to n characters or the length of the string. This xstrndup
> always copies n characters, reading past the end of the string if it doesn't
> have at least n characters.Good catch. Replacing the memcpy with strncpy solves this, right? (Potentially allocating a bit of extra memory if someone is actually using it on too short a string for some reason, of course).
-Daniel *This .sig left intentionally blank*