Re: [PATCH 1/5] Make xstrndup common

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Josh Triplett <josh@xxxxxxxxxxxxxxx> writes:

> 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.

Very well caught indeed, thanks.  In the original context in
commit.c, this function is always given length computed by
inspecting the source text by the caller, so the code was
correct, but if we are making it available as a general utility
routine, we probably cannot depend on that assumption anymore.


-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]