git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Make xstrndup common
@ 2007-05-03  2:49 Daniel Barkalow
  2007-05-03  4:08 ` Junio C Hamano
  2007-05-03  9:06 ` Alex Riesen
  0 siblings, 2 replies; 6+ messages in thread
From: Daniel Barkalow @ 2007-05-03  2:49 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano

This also improves the implementation to match how strndup is
specified (by GNU): if the length given is longer than the string,
only the string's length is allocated and copied, but the string need
not be null-terminated if it is at least as long as the given length.

Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
---
Haven't got the rest of my series updated for comments, but I'd like to 
get this change, which is logically unrelated aside from being a 
dependancy, in now.

 commit.c          |    8 --------
 git-compat-util.h |   12 ++++++++++++
 2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/commit.c b/commit.c
index f1ba972..aa7059c 100644
--- a/commit.c
+++ b/commit.c
@@ -718,14 +718,6 @@ static char *logmsg_reencode(const struct commit *commit,
 	return out;
 }
 
-static char *xstrndup(const char *text, int len)
-{
-	char *result = xmalloc(len + 1);
-	memcpy(result, text, len);
-	result[len] = '\0';
-	return result;
-}
-
 static void fill_person(struct interp *table, const char *msg, int len)
 {
 	int start, end, tz = 0;
diff --git a/git-compat-util.h b/git-compat-util.h
index 2c84016..0dcd4e2 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -197,6 +197,18 @@ static inline void *xmalloc(size_t size)
 	return ret;
 }
 
+static inline char *xstrndup(const char *str, int len)
+{
+	char *ret;
+	int i;
+	for (i = 0; i < len && str[i]; i++)
+		;
+	ret = xmalloc(i + 1);
+	strncpy(ret, str, i);
+	ret[i] = '\0';
+	return ret;
+}
+
 static inline void *xrealloc(void *ptr, size_t size)
 {
 	void *ret = realloc(ptr, size);
-- 
1.5.1.2.255.g6ead4-dirty

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] Make xstrndup common
  2007-05-03  2:49 [PATCH] Make xstrndup common Daniel Barkalow
@ 2007-05-03  4:08 ` Junio C Hamano
  2007-05-03  5:02   ` Daniel Barkalow
  2007-05-03  9:06 ` Alex Riesen
  1 sibling, 1 reply; 6+ messages in thread
From: Junio C Hamano @ 2007-05-03  4:08 UTC (permalink / raw)
  To: Daniel Barkalow; +Cc: git

Daniel Barkalow <barkalow@iabervon.org> writes:

> This also improves the implementation to match how strndup is
> specified (by GNU): if the length given is longer than the string,
> only the string's length is allocated and copied, but the string need
> not be null-terminated if it is at least as long as the given length.
>
> Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
> ---
> Haven't got the rest of my series updated for comments, but I'd like to 
> get this change, which is logically unrelated aside from being a 
> dependancy, in now.
>
>  commit.c          |    8 --------
>  git-compat-util.h |   12 ++++++++++++
>  2 files changed, 12 insertions(+), 8 deletions(-)
>
> diff --git a/commit.c b/commit.c
> index f1ba972..aa7059c 100644
> --- a/commit.c
> +++ b/commit.c
> @@ -718,14 +718,6 @@ static char *logmsg_reencode(const struct commit *commit,
>  	return out;
>  }
>  
> -static char *xstrndup(const char *text, int len)
> -{
> -	char *result = xmalloc(len + 1);
> -	memcpy(result, text, len);
> -	result[len] = '\0';
> -	return result;
> -}
> -
>  static void fill_person(struct interp *table, const char *msg, int len)
>  {
>  	int start, end, tz = 0;
> diff --git a/git-compat-util.h b/git-compat-util.h
> index 2c84016..0dcd4e2 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -197,6 +197,18 @@ static inline void *xmalloc(size_t size)
>  	return ret;
>  }
>  
> +static inline char *xstrndup(const char *str, int len)
> +{
> +	char *ret;
> +	int i;
> +	for (i = 0; i < len && str[i]; i++)
> +		;
> +	ret = xmalloc(i + 1);
> +	strncpy(ret, str, i);

Why strncpy() not memcpy()?

> +	ret[i] = '\0';
> +	return ret;
> +}
> +
>  static inline void *xrealloc(void *ptr, size_t size)
>  {
>  	void *ret = realloc(ptr, size);
> -- 
> 1.5.1.2.255.g6ead4-dirty

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Make xstrndup common
  2007-05-03  4:08 ` Junio C Hamano
@ 2007-05-03  5:02   ` Daniel Barkalow
  2007-05-03  5:07     ` Junio C Hamano
  0 siblings, 1 reply; 6+ messages in thread
From: Daniel Barkalow @ 2007-05-03  5:02 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Wed, 2 May 2007, Junio C Hamano wrote:

> Daniel Barkalow <barkalow@iabervon.org> writes:
> 
> > This also improves the implementation to match how strndup is
> > specified (by GNU): if the length given is longer than the string,
> > only the string's length is allocated and copied, but the string need
> > not be null-terminated if it is at least as long as the given length.
> >
> > Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
> > ---
> > Haven't got the rest of my series updated for comments, but I'd like to 
> > get this change, which is logically unrelated aside from being a 
> > dependancy, in now.
> >
> >  commit.c          |    8 --------
> >  git-compat-util.h |   12 ++++++++++++
> >  2 files changed, 12 insertions(+), 8 deletions(-)
> >
> > diff --git a/commit.c b/commit.c
> > index f1ba972..aa7059c 100644
> > --- a/commit.c
> > +++ b/commit.c
> > @@ -718,14 +718,6 @@ static char *logmsg_reencode(const struct commit *commit,
> >  	return out;
> >  }
> >  
> > -static char *xstrndup(const char *text, int len)
> > -{
> > -	char *result = xmalloc(len + 1);
> > -	memcpy(result, text, len);
> > -	result[len] = '\0';
> > -	return result;
> > -}
> > -
> >  static void fill_person(struct interp *table, const char *msg, int len)
> >  {
> >  	int start, end, tz = 0;
> > diff --git a/git-compat-util.h b/git-compat-util.h
> > index 2c84016..0dcd4e2 100644
> > --- a/git-compat-util.h
> > +++ b/git-compat-util.h
> > @@ -197,6 +197,18 @@ static inline void *xmalloc(size_t size)
> >  	return ret;
> >  }
> >  
> > +static inline char *xstrndup(const char *str, int len)
> > +{
> > +	char *ret;
> > +	int i;
> > +	for (i = 0; i < len && str[i]; i++)
> > +		;
> > +	ret = xmalloc(i + 1);
> > +	strncpy(ret, str, i);
> 
> Why strncpy() not memcpy()?

Left over from my previous attempt. memcpy() is better with the length 
check.

	-Daniel
*This .sig left intentionally blank*

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Make xstrndup common
  2007-05-03  5:02   ` Daniel Barkalow
@ 2007-05-03  5:07     ` Junio C Hamano
  0 siblings, 0 replies; 6+ messages in thread
From: Junio C Hamano @ 2007-05-03  5:07 UTC (permalink / raw)
  To: Daniel Barkalow; +Cc: git

Daniel Barkalow <barkalow@iabervon.org> writes:

>> Why strncpy() not memcpy()?
>
> Left over from my previous attempt. memcpy() is better with the length 
> check.

Ok, I was just sanity checking myself.  Will apply with the
change.

Thanks.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Make xstrndup common
  2007-05-03  2:49 [PATCH] Make xstrndup common Daniel Barkalow
  2007-05-03  4:08 ` Junio C Hamano
@ 2007-05-03  9:06 ` Alex Riesen
  2007-05-04  5:20   ` Junio C Hamano
  1 sibling, 1 reply; 6+ messages in thread
From: Alex Riesen @ 2007-05-03  9:06 UTC (permalink / raw)
  To: Daniel Barkalow; +Cc: git, Junio C Hamano

On 5/3/07, Daniel Barkalow <barkalow@iabervon.org> wrote:
> +static inline char *xstrndup(const char *str, int len)
> +{
> +       char *ret;
> +       int i;
> +       for (i = 0; i < len && str[i]; i++)
> +               ;
> +       ret = xmalloc(i + 1);
> +       strncpy(ret, str, i);
> +       ret[i] = '\0';
> +       return ret;
> +}

I'd suggest using platform-optimized memchr:

static inline char *xstrndup(const char *s, int len)
{
    char *p = memchr(s, 0, len);
    int n = p ? p - s: len;
    p = xmalloc(n + 1);
    memcpy(p, s, n);
    p[n] = '\0';
    return p;
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Make xstrndup common
  2007-05-03  9:06 ` Alex Riesen
@ 2007-05-04  5:20   ` Junio C Hamano
  0 siblings, 0 replies; 6+ messages in thread
From: Junio C Hamano @ 2007-05-04  5:20 UTC (permalink / raw)
  To: Alex Riesen; +Cc: Daniel Barkalow, git, Junio C Hamano

"Alex Riesen" <raa.lkml@gmail.com> writes:

> I'd suggest using platform-optimized memchr:

True.  On some architectures, strnlen() is a single instruction.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2007-05-04  5:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-03  2:49 [PATCH] Make xstrndup common Daniel Barkalow
2007-05-03  4:08 ` Junio C Hamano
2007-05-03  5:02   ` Daniel Barkalow
2007-05-03  5:07     ` Junio C Hamano
2007-05-03  9:06 ` Alex Riesen
2007-05-04  5:20   ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).