{"thread":{"id":"7950","subject":"[PATCH] Make xstrndup common","startedAt":"2007-05-03T02:49:41Z","lastAt":"2007-05-04T05:20:54Z","messageCount":6,"participants":["Daniel Barkalow","Junio C Hamano","Alex Riesen"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"40932","messageId":"Pine.LNX.4.64.0705022248040.28708@iabervon.org","threadId":"7950","inReplyTo":null,"subject":"[PATCH] Make xstrndup common","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2007-05-03T02:49:41Z","receivedAt":"2007-05-03T02:49:41Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"This also improves the implementation to match how strndup is\nspecified (by GNU): if the length given is longer than the string,\nonly the string's length is allocated and copied, but the string need\nnot be null-terminated if it is at least as long as the given length.\n\nSigned-off-by: Daniel Barkalow <barkalow@iabervon.org>\n---\nHaven't got the rest of my series updated for comments, but I'd like to \nget this change, which is logically unrelated aside from being a \ndependancy, in now.\n\n commit.c          |    8 --------\n git-compat-util.h |   12 ++++++++++++\n 2 files changed, 12 insertions(+), 8 deletions(-)\n\ndiff --git a/commit.c b/commit.c\nindex f1ba972..aa7059c 100644\n--- a/commit.c\n+++ b/commit.c\n@@ -718,14 +718,6 @@ static char *logmsg_reencode(const struct commit *commit,\n \treturn out;\n }\n \n-static char *xstrndup(const char *text, int len)\n-{\n-\tchar *result = xmalloc(len + 1);\n-\tmemcpy(result, text, len);\n-\tresult[len] = '\\0';\n-\treturn result;\n-}\n-\n static void fill_person(struct interp *table, const char *msg, int len)\n {\n \tint start, end, tz = 0;\ndiff --git a/git-compat-util.h b/git-compat-util.h\nindex 2c84016..0dcd4e2 100644\n--- a/git-compat-util.h\n+++ b/git-compat-util.h\n@@ -197,6 +197,18 @@ static inline void *xmalloc(size_t size)\n \treturn ret;\n }\n \n+static inline char *xstrndup(const char *str, int len)\n+{\n+\tchar *ret;\n+\tint i;\n+\tfor (i = 0; i < len && str[i]; i++)\n+\t\t;\n+\tret = xmalloc(i + 1);\n+\tstrncpy(ret, str, i);\n+\tret[i] = '\\0';\n+\treturn ret;\n+}\n+\n static inline void *xrealloc(void *ptr, size_t size)\n {\n \tvoid *ret = realloc(ptr, size);\n-- \n1.5.1.2.255.g6ead4-dirty\n"},{"id":"40935","messageId":"7vzm4m35xm.fsf@assigned-by-dhcp.cox.net","threadId":"7950","inReplyTo":"Pine.LNX.4.64.0705022248040.28708@iabervon.org","subject":"Re: [PATCH] Make xstrndup common","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-05-03T04:08:21Z","receivedAt":"2007-05-03T04:08:21Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Daniel Barkalow <barkalow@iabervon.org> writes:\n\n> This also improves the implementation to match how strndup is\n> specified (by GNU): if the length given is longer than the string,\n> only the string's length is allocated and copied, but the string need\n> not be null-terminated if it is at least as long as the given length.\n>\n> Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>\n> ---\n> Haven't got the rest of my series updated for comments, but I'd like to \n> get this change, which is logically unrelated aside from being a \n> dependancy, in now.\n>\n>  commit.c          |    8 --------\n>  git-compat-util.h |   12 ++++++++++++\n>  2 files changed, 12 insertions(+), 8 deletions(-)\n>\n> diff --git a/commit.c b/commit.c\n> index f1ba972..aa7059c 100644\n> --- a/commit.c\n> +++ b/commit.c\n> @@ -718,14 +718,6 @@ static char *logmsg_reencode(const struct commit *commit,\n>  \treturn out;\n>  }\n>  \n> -static char *xstrndup(const char *text, int len)\n> -{\n> -\tchar *result = xmalloc(len + 1);\n> -\tmemcpy(result, text, len);\n> -\tresult[len] = '\\0';\n> -\treturn result;\n> -}\n> -\n>  static void fill_person(struct interp *table, const char *msg, int len)\n>  {\n>  \tint start, end, tz = 0;\n> diff --git a/git-compat-util.h b/git-compat-util.h\n> index 2c84016..0dcd4e2 100644\n> --- a/git-compat-util.h\n> +++ b/git-compat-util.h\n> @@ -197,6 +197,18 @@ static inline void *xmalloc(size_t size)\n>  \treturn ret;\n>  }\n>  \n> +static inline char *xstrndup(const char *str, int len)\n> +{\n> +\tchar *ret;\n> +\tint i;\n> +\tfor (i = 0; i < len && str[i]; i++)\n> +\t\t;\n> +\tret = xmalloc(i + 1);\n> +\tstrncpy(ret, str, i);\n\nWhy strncpy() not memcpy()?\n\n> +\tret[i] = '\\0';\n> +\treturn ret;\n> +}\n> +\n>  static inline void *xrealloc(void *ptr, size_t size)\n>  {\n>  \tvoid *ret = realloc(ptr, size);\n> -- \n> 1.5.1.2.255.g6ead4-dirty\n"},{"id":"40937","messageId":"Pine.LNX.4.64.0705030100050.28708@iabervon.org","threadId":"7950","inReplyTo":"7vzm4m35xm.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH] Make xstrndup common","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2007-05-03T05:02:22Z","receivedAt":"2007-05-03T05:02:22Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"On Wed, 2 May 2007, Junio C Hamano wrote:\n\n> Daniel Barkalow <barkalow@iabervon.org> writes:\n> \n> > This also improves the implementation to match how strndup is\n> > specified (by GNU): if the length given is longer than the string,\n> > only the string's length is allocated and copied, but the string need\n> > not be null-terminated if it is at least as long as the given length.\n> >\n> > Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>\n> > ---\n> > Haven't got the rest of my series updated for comments, but I'd like to \n> > get this change, which is logically unrelated aside from being a \n> > dependancy, in now.\n> >\n> >  commit.c          |    8 --------\n> >  git-compat-util.h |   12 ++++++++++++\n> >  2 files changed, 12 insertions(+), 8 deletions(-)\n> >\n> > diff --git a/commit.c b/commit.c\n> > index f1ba972..aa7059c 100644\n> > --- a/commit.c\n> > +++ b/commit.c\n> > @@ -718,14 +718,6 @@ static char *logmsg_reencode(const struct commit *commit,\n> >  \treturn out;\n> >  }\n> >  \n> > -static char *xstrndup(const char *text, int len)\n> > -{\n> > -\tchar *result = xmalloc(len + 1);\n> > -\tmemcpy(result, text, len);\n> > -\tresult[len] = '\\0';\n> > -\treturn result;\n> > -}\n> > -\n> >  static void fill_person(struct interp *table, const char *msg, int len)\n> >  {\n> >  \tint start, end, tz = 0;\n> > diff --git a/git-compat-util.h b/git-compat-util.h\n> > index 2c84016..0dcd4e2 100644\n> > --- a/git-compat-util.h\n> > +++ b/git-compat-util.h\n> > @@ -197,6 +197,18 @@ static inline void *xmalloc(size_t size)\n> >  \treturn ret;\n> >  }\n> >  \n> > +static inline char *xstrndup(const char *str, int len)\n> > +{\n> > +\tchar *ret;\n> > +\tint i;\n> > +\tfor (i = 0; i < len && str[i]; i++)\n> > +\t\t;\n> > +\tret = xmalloc(i + 1);\n> > +\tstrncpy(ret, str, i);\n> \n> Why strncpy() not memcpy()?\n\nLeft over from my previous attempt. memcpy() is better with the length \ncheck.\n\n\t-Daniel\n*This .sig left intentionally blank*\n"},{"id":"40938","messageId":"7vvefa336s.fsf@assigned-by-dhcp.cox.net","threadId":"7950","inReplyTo":"Pine.LNX.4.64.0705030100050.28708@iabervon.org","subject":"Re: [PATCH] Make xstrndup common","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-05-03T05:07:39Z","receivedAt":"2007-05-03T05:07:39Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Daniel Barkalow <barkalow@iabervon.org> writes:\n\n>> Why strncpy() not memcpy()?\n>\n> Left over from my previous attempt. memcpy() is better with the length \n> check.\n\nOk, I was just sanity checking myself.  Will apply with the\nchange.\n\nThanks.\n"},{"id":"40951","messageId":"81b0412b0705030206u3a6b8a46qfd98ccf597d3c96e@mail.gmail.com","threadId":"7950","inReplyTo":"Pine.LNX.4.64.0705022248040.28708@iabervon.org","subject":"Re: [PATCH] Make xstrndup common","fromName":"Alex Riesen","fromEmail":"raa.lkml@gmail.com","sentAt":"2007-05-03T09:06:05Z","receivedAt":"2007-05-03T09:06:05Z","isPatch":true,"sender":{"key":"raa.lkml@gmail.com","avatar":"https://avatars.githubusercontent.com/u/324101?v=4"},"body":"On 5/3/07, Daniel Barkalow <barkalow@iabervon.org> wrote:\n> +static inline char *xstrndup(const char *str, int len)\n> +{\n> +       char *ret;\n> +       int i;\n> +       for (i = 0; i < len && str[i]; i++)\n> +               ;\n> +       ret = xmalloc(i + 1);\n> +       strncpy(ret, str, i);\n> +       ret[i] = '\\0';\n> +       return ret;\n> +}\n\nI'd suggest using platform-optimized memchr:\n\nstatic inline char *xstrndup(const char *s, int len)\n{\n    char *p = memchr(s, 0, len);\n    int n = p ? p - s: len;\n    p = xmalloc(n + 1);\n    memcpy(p, s, n);\n    p[n] = '\\0';\n    return p;\n}\n"},{"id":"41005","messageId":"7vtzutxiyx.fsf@assigned-by-dhcp.cox.net","threadId":"7950","inReplyTo":"81b0412b0705030206u3a6b8a46qfd98ccf597d3c96e@mail.gmail.com","subject":"Re: [PATCH] Make xstrndup common","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-05-04T05:20:54Z","receivedAt":"2007-05-04T05:20:54Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Alex Riesen\" <raa.lkml@gmail.com> writes:\n\n> I'd suggest using platform-optimized memchr:\n\nTrue.  On some architectures, strnlen() is a single instruction.\n"}]}