Re: [PATCH 1/5] Make xstrndup common
- From
Josh Triplett <josh@freedesktop.org>
- Date
- Apr 28, 2007, 18:53 UTC
- Message-ID
- <46339819.8030007@freedesktop.org>
Daniel Barkalow wrote:
> It was implemented in commit.c; move it with the other x memory functions.
[...]
Show 8 quoted lines
> +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.
- Josh Triplett