# Re: [PATCH 1/5] Make xstrndup common

9 messages from 2007-04-28 to 2007-04-30. Participants: Josh Triplett, Junio C Hamano, Daniel Barkalow, Adam Roben, Johannes Schindelin, Jeff King.
Thread: https://gitlist.dev/t/7889

## Josh Triplett, 2007-04-28 18:53

Subject: Re: [PATCH 1/5] Make xstrndup common
Message-ID: <46339819.8030007@freedesktop.org>
URL: https://gitlist.dev/e/46339819.8030007%40freedesktop.org

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

- Josh Triplett


```

## Junio C Hamano, 2007-04-28 22:47

Subject: Re: [PATCH 1/5] Make xstrndup common
Message-ID: <7vslaki0au.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vslaki0au.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <46339819.8030007@freedesktop.org>

```
Josh Triplett <josh@freedesktop.org> 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.

```

## Daniel Barkalow, 2007-04-29 18:19

Subject: Re: [PATCH 1/5] Make xstrndup common
Message-ID: <Pine.LNX.4.64.0704291416370.28708@iabervon.org>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0704291416370.28708%40iabervon.org
In-Reply-To: <46339819.8030007@freedesktop.org>

```
On Sat, 28 Apr 2007, Josh Triplett wrote:

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

```

## Josh Triplett, 2007-04-29 20:29

Subject: Re: [PATCH 1/5] Make xstrndup common
Message-ID: <4635003F.7080408@freedesktop.org>
URL: https://gitlist.dev/e/4635003F.7080408%40freedesktop.org
In-Reply-To: <Pine.LNX.4.64.0704291416370.28708@iabervon.org>

```
Daniel Barkalow wrote:
> On Sat, 28 Apr 2007, Josh Triplett wrote:
>> 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).

That would work, but it seems bad to allocate excess memory.  How about just
using strlen and setting len to that if shorter, before doing the xmalloc and
memcpy?  Yes, that makes two passes over the string, but I don't see any way
around that.

I just checked the glibc source for strndup, and it does exactly the same
thing, except that it uses the glibc-specific function strnlen rather than
using strlen and figuring out the smaller of the two lengths.  That probably
increases efficiency if we have a string longer than, but we can't portably
use strnlen, so we'd have to check for it; doesn't seem worth the trouble.

- Josh Triplett


```

## Junio C Hamano, 2007-04-29 20:39

Subject: Re: [PATCH 1/5] Make xstrndup common
Message-ID: <7vy7kadifj.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vy7kadifj.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <4635003F.7080408@freedesktop.org>

```
Josh Triplett <josh@freedesktop.org> writes:

> Daniel Barkalow wrote:
> ...
>> 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).
>
> That would work, but it seems bad to allocate excess memory.  How about just
> using strlen and setting len to that if shorter, before doing the xmalloc and
> memcpy?  Yes, that makes two passes over the string, but I don't see any way
> around that.

Hand-rolling strnlen() would be needed anyway, because there is
no guarantee that the incoming string is NUL terminated.  In the
worst case the string may point at a region of memory filled
with non-NUL to the end, which coincides with a page boundary,
and the next page may be an unmapped one; your strlen() would
sigbus.

```

## Adam Roben, 2007-04-29 20:40

Subject: Re: [PATCH 1/5] Make xstrndup common
Message-ID: <84F8DDF3-BCF5-4C2A-9C90-38AE139D7178@apple.com>
URL: https://gitlist.dev/e/84F8DDF3-BCF5-4C2A-9C90-38AE139D7178%40apple.com
In-Reply-To: <4635003F.7080408@freedesktop.org>

```
On Apr 29, 2007, at 1:29 PM, Josh Triplett wrote:

> Daniel Barkalow wrote:
>> On Sat, 28 Apr 2007, Josh Triplett wrote:
>>> 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).
>
> That would work, but it seems bad to allocate excess memory.  How  
> about just
> using strlen and setting len to that if shorter, before doing the  
> xmalloc and
> memcpy?  Yes, that makes two passes over the string, but I don't  
> see any way
> around that.

    An easy way around that is to do the string copy yourself,  
walking the string until you either find '\0' or reach len copied  
characters.

-Adam

```

## Johannes Schindelin, 2007-04-30 13:12

Subject: Re: [PATCH 1/5] Make xstrndup common
Message-ID: <Pine.LNX.4.64.0704301508550.29859@racer.site>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0704301508550.29859%40racer.site
In-Reply-To: <4635003F.7080408@freedesktop.org>

```
Hi,

On Sun, 29 Apr 2007, Josh Triplett wrote:

> Daniel Barkalow wrote:
> > On Sat, 28 Apr 2007, Josh Triplett wrote:
> >> 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).
> 
> That would work, but it seems bad to allocate excess memory.  How about 
> just using strlen and setting len to that if shorter, before doing the 
> xmalloc and memcpy?  Yes, that makes two passes over the string, but I 
> don't see any way around that.

Unless I am missing something, I think this should work:

static inline char *xstrndup(const char *str, int len)
{
	char *result = strndup(str, len);
	if (result == NULL)
		die ("xstrndup(): out of memory");
	return result;
}

Hmm?

Ciao,
Dscho

P.S.: If you feel real paranoid about it, you might insert

	if (result == NULL) {
		release_pack_memory(len, -1);
		result = strndup(str, len);
	}

before the if (...), but I think that's overkill.

```

## Jeff King, 2007-04-30 20:50

Subject: Re: [PATCH 1/5] Make xstrndup common
Message-ID: <20070430205050.GC4385@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20070430205050.GC4385%40coredump.intra.peff.net
In-Reply-To: <Pine.LNX.4.64.0704301508550.29859@racer.site>

```
On Mon, Apr 30, 2007 at 03:12:50PM +0200, Johannes Schindelin wrote:

> Unless I am missing something, I think this should work:
> 
> static inline char *xstrndup(const char *str, int len)
> {
> 	char *result = strndup(str, len);
> 	if (result == NULL)
> 		die ("xstrndup(): out of memory");
> 	return result;
> }
> 
> Hmm?

I can't speak for the original authors, but I imagine part of the
impetus was that strndup is a GNU-ism.

-Peff

```

## Johannes Schindelin, 2007-04-30 22:21

Subject: Re: [PATCH 1/5] Make xstrndup common
Message-ID: <Pine.LNX.4.64.0705010020070.29859@racer.site>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0705010020070.29859%40racer.site
In-Reply-To: <20070430205050.GC4385@coredump.intra.peff.net>

```
Hi,

On Mon, 30 Apr 2007, Jeff King wrote:

> On Mon, Apr 30, 2007 at 03:12:50PM +0200, Johannes Schindelin wrote:
> 
> > Unless I am missing something, I think this should work:
> > 
> > static inline char *xstrndup(const char *str, int len)
> > {
> > 	char *result = strndup(str, len);
> > 	if (result == NULL)
> > 		die ("xstrndup(): out of memory");
> > 	return result;
> > }
> > 
> > Hmm?
> 
> I can't speak for the original authors, but I imagine part of the
> impetus was that strndup is a GNU-ism.

D'oh! I never thought this was a GNU-ism.

Ciao,
Dscho

```
