{"thread":{"id":"15948","subject":"[PATCH 1/3] add alloc_ref_with_prefix()","startedAt":"2008-10-18T08:37:40Z","lastAt":"2008-10-20T10:34:22Z","messageCount":8,"participants":["René Scharfe","Bert Wesarg","Andreas Ericsson"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"93345","messageId":"48F9A054.4010703@lsrfire.ath.cx","threadId":"15948","inReplyTo":null,"subject":"[PATCH 1/3] add alloc_ref_with_prefix()","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2008-10-18T08:37:40Z","receivedAt":"2008-10-18T08:37:40Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"In three cases in remote.c, a \"raw\" ref is allocated using alloc_ref()\nand then its is constructed using sprintf().  Clean it up by adding a\nhelper function, alloc_ref_with_prefix(), which creates a composite\nname.  Use it in alloc_ref_from_str(), too, as it simplifies the code.\n\nOpen code alloc_ref() in alloc_ref_with_prefix(), as the former is\ngoing to be removed in the patch after the next.\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\n remote.c |   32 ++++++++++++++++----------------\n 1 files changed, 16 insertions(+), 16 deletions(-)\n\ndiff --git a/remote.c b/remote.c\nindex 8a04066..98cbcf9 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -749,6 +749,16 @@ int remote_find_tracking(struct remote *remote, struct refspec *refspec)\n \treturn -1;\n }\n \n+static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\n+\t\tconst char *name)\n+{\n+\tsize_t len = strlen(name);\n+\tstruct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);\n+\tmemcpy(ref->name, prefix, prefixlen);\n+\tmemcpy(ref->name + prefixlen, name, len);\n+\treturn ref;\n+}\n+\n struct ref *alloc_ref(unsigned namelen)\n {\n \tstruct ref *ret = xcalloc(1, sizeof(struct ref) + namelen);\n@@ -757,9 +767,7 @@ struct ref *alloc_ref(unsigned namelen)\n \n struct ref *alloc_ref_from_str(const char* str)\n {\n-\tstruct ref *ret = alloc_ref(strlen(str) + 1);\n-\tstrcpy(ret->name, str);\n-\treturn ret;\n+\treturn alloc_ref_with_prefix(\"\", 0, str);\n }\n \n static struct ref *copy_ref(const struct ref *ref)\n@@ -1152,10 +1160,8 @@ static struct ref *get_expanded_map(const struct ref *remote_refs,\n \t\t\tstruct ref *cpy = copy_ref(ref);\n \t\t\tmatch = ref->name + remote_prefix_len;\n \n-\t\t\tcpy->peer_ref = alloc_ref(local_prefix_len +\n-\t\t\t\t\t\t  strlen(match) + 1);\n-\t\t\tsprintf(cpy->peer_ref->name, \"%s%s\",\n-\t\t\t\trefspec->dst, match);\n+\t\t\tcpy->peer_ref = alloc_ref_with_prefix(refspec->dst,\n+\t\t\t\t\tlocal_prefix_len, match);\n \t\t\tif (refspec->force)\n \t\t\t\tcpy->peer_ref->force = 1;\n \t\t\t*tail = cpy;\n@@ -1188,7 +1194,6 @@ struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)\n \n static struct ref *get_local_ref(const char *name)\n {\n-\tstruct ref *ret;\n \tif (!name)\n \t\treturn NULL;\n \n@@ -1198,15 +1203,10 @@ static struct ref *get_local_ref(const char *name)\n \n \tif (!prefixcmp(name, \"heads/\") ||\n \t    !prefixcmp(name, \"tags/\") ||\n-\t    !prefixcmp(name, \"remotes/\")) {\n-\t\tret = alloc_ref(strlen(name) + 6);\n-\t\tsprintf(ret->name, \"refs/%s\", name);\n-\t\treturn ret;\n-\t}\n+\t    !prefixcmp(name, \"remotes/\"))\n+\t\treturn alloc_ref_with_prefix(\"refs/\", 5, name);\n \n-\tret = alloc_ref(strlen(name) + 12);\n-\tsprintf(ret->name, \"refs/heads/%s\", name);\n-\treturn ret;\n+\treturn alloc_ref_with_prefix(\"refs/heads/\", 11, name);\n }\n \n int get_fetch_map(const struct ref *remote_refs,\n-- \n1.6.0.2.542.gde55\n"},{"id":"93346","messageId":"48F9A13D.3090401@lsrfire.ath.cx","threadId":"15948","inReplyTo":"48F9A054.4010703@lsrfire.ath.cx","subject":"[PATCH 2/3] use alloc_ref_from_str() everywhere","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2008-10-18T08:41:33Z","receivedAt":"2008-10-18T08:41:33Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Replace pairs of alloc_ref() and strcpy() with alloc_ref_from_str(),\nsimplifying the code.\n\nIn connect.c, also a pair of alloc_ref() and memcpy() is replaced --\nthe additional cost of a strlen() call should not have too much of an\nimpact.  Consistency and simplicity are more important.\n\nIn remote.c, the code was allocating 11 bytes more than needed for\nthe name part, but I couldn't see them being used for anything.\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\n connect.c   |    3 +--\n remote.c    |    3 +--\n transport.c |    6 ++----\n 3 files changed, 4 insertions(+), 8 deletions(-)\n\ndiff --git a/connect.c b/connect.c\nindex 67d2cd8..b69060b 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -90,9 +90,8 @@ struct ref **get_remote_heads(int in, struct ref **list,\n \t\t\tcontinue;\n \t\tif (nr_match && !path_match(name, nr_match, match))\n \t\t\tcontinue;\n-\t\tref = alloc_ref(name_len + 1);\n+\t\tref = alloc_ref_from_str(buffer + 41);\n \t\thashcpy(ref->old_sha1, old_sha1);\n-\t\tmemcpy(ref->name, buffer + 41, name_len + 1);\n \t\t*list = ref;\n \t\tlist = &ref->next;\n \t}\ndiff --git a/remote.c b/remote.c\nindex 98cbcf9..44d681d 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -878,8 +878,7 @@ static struct ref *try_explicit_object_name(const char *name)\n \tstruct ref *ref;\n \n \tif (!*name) {\n-\t\tref = alloc_ref(20);\n-\t\tstrcpy(ref->name, \"(delete)\");\n+\t\tref = alloc_ref_from_str(\"(delete)\");\n \t\thashclr(ref->new_sha1);\n \t\treturn ref;\n \t}\ndiff --git a/transport.c b/transport.c\nindex 5110c56..3d03475 100644\n--- a/transport.c\n+++ b/transport.c\n@@ -75,15 +75,14 @@ static int read_loose_refs(struct strbuf *path, int name_offset,\n \n \t\t\tif (fd < 0)\n \t\t\t\tcontinue;\n-\t\t\tnext = alloc_ref(path->len - name_offset + 1);\n+\t\t\tnext = alloc_ref_from_str(path->buf + name_offset);\n \t\t\tif (read_in_full(fd, buffer, 40) != 40 ||\n \t\t\t\t\tget_sha1_hex(buffer, next->old_sha1)) {\n \t\t\t\tclose(fd);\n \t\t\t\tfree(next);\n \t\t\t\tcontinue;\n \t\t\t}\n \t\t\tclose(fd);\n-\t\t\tstrcpy(next->name, path->buf + name_offset);\n \t\t\t(*tail)->next = next;\n \t\t\t*tail = next;\n \t\t}\n@@ -127,14 +126,13 @@ static void insert_packed_refs(const char *packed_refs, struct ref **list)\n \t\t\t\t      (*list)->next->name)) > 0)\n \t\t\tlist = &(*list)->next;\n \t\tif (!(*list)->next || cmp < 0) {\n-\t\t\tstruct ref *next = alloc_ref(len - 40);\n+\t\t\tstruct ref *next = alloc_ref_from_str(buffer + 41);\n \t\t\tbuffer[40] = '\\0';\n \t\t\tif (get_sha1_hex(buffer, next->old_sha1)) {\n \t\t\t\twarning (\"invalid SHA-1: %s\", buffer);\n \t\t\t\tfree(next);\n \t\t\t\tcontinue;\n \t\t\t}\n-\t\t\tstrcpy(next->name, buffer + 41);\n \t\t\tnext->next = (*list)->next;\n \t\t\t(*list)->next = next;\n \t\t\tlist = &(*list)->next;\n-- \n1.6.0.2.542.gde55\n"},{"id":"93347","messageId":"48F9A1E2.50000@lsrfire.ath.cx","threadId":"15948","inReplyTo":"48F9A13D.3090401@lsrfire.ath.cx","subject":"[PATCH 3/3] make alloc_ref_from_str() the new alloc_ref()","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2008-10-18T08:44:18Z","receivedAt":"2008-10-18T08:44:18Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"With all calls to alloc_ref() gone, we can remove it and then we're free\nto give alloc_ref_from_str() the shorter name.  It's a much nicer\ninterface, as the callers always need to have a name string when they\nallocate a ref anyway and don't need to calculate and pass its length+1\nany more.\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\n builtin-fetch.c |    4 ++--\n connect.c       |    2 +-\n http-push.c     |    4 ++--\n remote.c        |   21 +++++++--------------\n remote.h        |    4 +---\n transport.c     |    8 ++++----\n walker.c        |    2 +-\n 7 files changed, 18 insertions(+), 27 deletions(-)\n\ndiff --git a/builtin-fetch.c b/builtin-fetch.c\nindex ee93d3a..e008ee9 100644\n--- a/builtin-fetch.c\n+++ b/builtin-fetch.c\n@@ -521,8 +521,8 @@ static void find_non_local_tags(struct transport *transport,\n \t\t     will_fetch(head, ref->old_sha1))) {\n \t\t\tstring_list_insert(ref_name, &new_refs);\n \n-\t\t\trm = alloc_ref_from_str(ref_name);\n-\t\t\trm->peer_ref = alloc_ref_from_str(ref_name);\n+\t\t\trm = alloc_ref(ref_name);\n+\t\t\trm->peer_ref = alloc_ref(ref_name);\n \t\t\thashcpy(rm->old_sha1, ref_sha1);\n \n \t\t\t**tail = rm;\ndiff --git a/connect.c b/connect.c\nindex b69060b..0c50d0a 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -90,7 +90,7 @@ struct ref **get_remote_heads(int in, struct ref **list,\n \t\t\tcontinue;\n \t\tif (nr_match && !path_match(name, nr_match, match))\n \t\t\tcontinue;\n-\t\tref = alloc_ref_from_str(buffer + 41);\n+\t\tref = alloc_ref(buffer + 41);\n \t\thashcpy(ref->old_sha1, old_sha1);\n \t\t*list = ref;\n \t\tlist = &ref->next;\ndiff --git a/http-push.c b/http-push.c\nindex 42f4d78..5cecef4 100644\n--- a/http-push.c\n+++ b/http-push.c\n@@ -1780,7 +1780,7 @@ static void one_remote_ref(char *refname)\n \tstruct ref *ref;\n \tstruct object *obj;\n \n-\tref = alloc_ref_from_str(refname);\n+\tref = alloc_ref(refname);\n \n \tif (http_fetch_ref(remote->url, ref) != 0) {\n \t\tfprintf(stderr,\n@@ -1887,7 +1887,7 @@ static void add_remote_info_ref(struct remote_ls_ctx *ls)\n \tchar *ref_info;\n \tstruct ref *ref;\n \n-\tref = alloc_ref_from_str(ls->dentry_name);\n+\tref = alloc_ref(ls->dentry_name);\n \n \tif (http_fetch_ref(remote->url, ref) != 0) {\n \t\tfprintf(stderr,\ndiff --git a/remote.c b/remote.c\nindex 44d681d..e530a21 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -759,15 +759,9 @@ static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\n \treturn ref;\n }\n \n-struct ref *alloc_ref(unsigned namelen)\n+struct ref *alloc_ref(const char *name)\n {\n-\tstruct ref *ret = xcalloc(1, sizeof(struct ref) + namelen);\n-\treturn ret;\n-}\n-\n-struct ref *alloc_ref_from_str(const char* str)\n-{\n-\treturn alloc_ref_with_prefix(\"\", 0, str);\n+\treturn alloc_ref_with_prefix(\"\", 0, name);\n }\n \n static struct ref *copy_ref(const struct ref *ref)\n@@ -878,20 +872,20 @@ static struct ref *try_explicit_object_name(const char *name)\n \tstruct ref *ref;\n \n \tif (!*name) {\n-\t\tref = alloc_ref_from_str(\"(delete)\");\n+\t\tref = alloc_ref(\"(delete)\");\n \t\thashclr(ref->new_sha1);\n \t\treturn ref;\n \t}\n \tif (get_sha1(name, sha1))\n \t\treturn NULL;\n-\tref = alloc_ref_from_str(name);\n+\tref = alloc_ref(name);\n \thashcpy(ref->new_sha1, sha1);\n \treturn ref;\n }\n \n static struct ref *make_linked_ref(const char *name, struct ref ***tail)\n {\n-\tstruct ref *ret = alloc_ref_from_str(name);\n+\tstruct ref *ret = alloc_ref(name);\n \ttail_link_ref(ret, tail);\n \treturn ret;\n }\n@@ -1196,9 +1190,8 @@ static struct ref *get_local_ref(const char *name)\n \tif (!name)\n \t\treturn NULL;\n \n-\tif (!prefixcmp(name, \"refs/\")) {\n-\t\treturn alloc_ref_from_str(name);\n-\t}\n+\tif (!prefixcmp(name, \"refs/\"))\n+\t\treturn alloc_ref(name);\n \n \tif (!prefixcmp(name, \"heads/\") ||\n \t    !prefixcmp(name, \"tags/\") ||\ndiff --git a/remote.h b/remote.h\nindex c6163ff..d2e170c 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -55,9 +55,7 @@ struct refspec {\n \n extern const struct refspec *tag_refspec;\n \n-struct ref *alloc_ref(unsigned namelen);\n-\n-struct ref *alloc_ref_from_str(const char* str);\n+struct ref *alloc_ref(const char *name);\n \n struct ref *copy_ref_list(const struct ref *ref);\n \ndiff --git a/transport.c b/transport.c\nindex 3d03475..cfb7350 100644\n--- a/transport.c\n+++ b/transport.c\n@@ -75,7 +75,7 @@ static int read_loose_refs(struct strbuf *path, int name_offset,\n \n \t\t\tif (fd < 0)\n \t\t\t\tcontinue;\n-\t\t\tnext = alloc_ref_from_str(path->buf + name_offset);\n+\t\t\tnext = alloc_ref(path->buf + name_offset);\n \t\t\tif (read_in_full(fd, buffer, 40) != 40 ||\n \t\t\t\t\tget_sha1_hex(buffer, next->old_sha1)) {\n \t\t\t\tclose(fd);\n@@ -126,7 +126,7 @@ static void insert_packed_refs(const char *packed_refs, struct ref **list)\n \t\t\t\t      (*list)->next->name)) > 0)\n \t\t\tlist = &(*list)->next;\n \t\tif (!(*list)->next || cmp < 0) {\n-\t\t\tstruct ref *next = alloc_ref_from_str(buffer + 41);\n+\t\t\tstruct ref *next = alloc_ref(buffer + 41);\n \t\t\tbuffer[40] = '\\0';\n \t\t\tif (get_sha1_hex(buffer, next->old_sha1)) {\n \t\t\t\twarning (\"invalid SHA-1: %s\", buffer);\n@@ -499,7 +499,7 @@ static struct ref *get_refs_via_curl(struct transport *transport)\n \n \tstrbuf_release(&buffer);\n \n-\tref = alloc_ref_from_str(\"HEAD\");\n+\tref = alloc_ref(\"HEAD\");\n \tif (!walker->fetch_ref(walker, ref) &&\n \t    !resolve_remote_symref(ref, refs)) {\n \t\tref->next = refs;\n@@ -540,7 +540,7 @@ static struct ref *get_refs_from_bundle(struct transport *transport)\n \t\tdie (\"Could not read bundle '%s'.\", transport->url);\n \tfor (i = 0; i < data->header.references.nr; i++) {\n \t\tstruct ref_list_entry *e = data->header.references.list + i;\n-\t\tstruct ref *ref = alloc_ref_from_str(e->name);\n+\t\tstruct ref *ref = alloc_ref(e->name);\n \t\thashcpy(ref->old_sha1, e->sha1);\n \t\tref->next = result;\n \t\tresult = ref;\ndiff --git a/walker.c b/walker.c\nindex 6b4cf70..679adab 100644\n--- a/walker.c\n+++ b/walker.c\n@@ -191,7 +191,7 @@ static int interpret_target(struct walker *walker, char *target, unsigned char *\n \tif (!get_sha1_hex(target, sha1))\n \t\treturn 0;\n \tif (!check_ref_format(target)) {\n-\t\tstruct ref *ref = alloc_ref_from_str(target);\n+\t\tstruct ref *ref = alloc_ref(target);\n \t\tif (!walker->fetch_ref(walker, ref)) {\n \t\t\thashcpy(sha1, ref->old_sha1);\n \t\t\tfree(ref);\n-- \n1.6.0.2.542.gde55\n"},{"id":"93351","messageId":"36ca99e90810180227u367783f6vc3b7af0176f6df06@mail.gmail.com","threadId":"15948","inReplyTo":"48F9A054.4010703@lsrfire.ath.cx","subject":"Re: [PATCH 1/3] add alloc_ref_with_prefix()","fromName":"Bert Wesarg","fromEmail":"bert.wesarg@googlemail.com","sentAt":"2008-10-18T09:27:56Z","receivedAt":"2008-10-18T09:27:56Z","isPatch":true,"sender":{"key":"bert.wesarg@googlemail.com","avatar":"https://avatars.githubusercontent.com/u/111934?v=4"},"body":"On Sat, Oct 18, 2008 at 10:37, René Scharfe <rene.scharfe@lsrfire.ath.cx> wrote:\n> In three cases in remote.c, a \"raw\" ref is allocated using alloc_ref()\n> and then its is constructed using sprintf().  Clean it up by adding a\n> helper function, alloc_ref_with_prefix(), which creates a composite\n> name.  Use it in alloc_ref_from_str(), too, as it simplifies the code.\n>\n> Open code alloc_ref() in alloc_ref_with_prefix(), as the former is\n> going to be removed in the patch after the next.\n>\n> Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n> ---\n>  remote.c |   32 ++++++++++++++++----------------\n>  1 files changed, 16 insertions(+), 16 deletions(-)\n>\n> diff --git a/remote.c b/remote.c\n> index 8a04066..98cbcf9 100644\n> --- a/remote.c\n> +++ b/remote.c\n> @@ -749,6 +749,16 @@ int remote_find_tracking(struct remote *remote, struct refspec *refspec)\n>        return -1;\n>  }\n>\n> +static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\n> +               const char *name)\n> +{\n> +       size_t len = strlen(name);\n> +       struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);\n> +       memcpy(ref->name, prefix, prefixlen);\n> +       memcpy(ref->name + prefixlen, name, len);\nWhere does you \\0-terminate the string?\n\nRegards\nBert\n"},{"id":"93353","messageId":"48F9AECA.3020606@lsrfire.ath.cx","threadId":"15948","inReplyTo":"36ca99e90810180227u367783f6vc3b7af0176f6df06@mail.gmail.com","subject":"Re: [PATCH 1/3] add alloc_ref_with_prefix()","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2008-10-18T09:39:22Z","receivedAt":"2008-10-18T09:39:22Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":">> +static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\n>> +               const char *name)\n>> +{\n>> +       size_t len = strlen(name);\n>> +       struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);\n>> +       memcpy(ref->name, prefix, prefixlen);\n>> +       memcpy(ref->name + prefixlen, name, len);\n> Where does you \\0-terminate the string?\n\nxcalloc() calls calloc(), which zeroes the memory.\n\nRené\n"},{"id":"93357","messageId":"36ca99e90810180507q2dedf4ck7262239ae91d892f@mail.gmail.com","threadId":"15948","inReplyTo":"48F9AECA.3020606@lsrfire.ath.cx","subject":"Re: [PATCH 1/3] add alloc_ref_with_prefix()","fromName":"Bert Wesarg","fromEmail":"bert.wesarg@googlemail.com","sentAt":"2008-10-18T12:07:48Z","receivedAt":"2008-10-18T12:07:48Z","isPatch":true,"sender":{"key":"bert.wesarg@googlemail.com","avatar":"https://avatars.githubusercontent.com/u/111934?v=4"},"body":"On Sat, Oct 18, 2008 at 11:39, René Scharfe <rene.scharfe@lsrfire.ath.cx> wrote:\n>>> +static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\n>>> +               const char *name)\n>>> +{\n>>> +       size_t len = strlen(name);\n>>> +       struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);\n>>> +       memcpy(ref->name, prefix, prefixlen);\n>>> +       memcpy(ref->name + prefixlen, name, len);\n>> Where does you \\0-terminate the string?\n>\n> xcalloc() calls calloc(), which zeroes the memory.\nSo, you write the memory range twice, just for the last \\0?\n\nBert\n>\n> René\n>\n"},{"id":"93361","messageId":"48F9D823.4000908@lsrfire.ath.cx","threadId":"15948","inReplyTo":"36ca99e90810180507q2dedf4ck7262239ae91d892f@mail.gmail.com","subject":"Re: [PATCH 1/3] add alloc_ref_with_prefix()","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2008-10-18T12:35:47Z","receivedAt":"2008-10-18T12:35:47Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Bert Wesarg schrieb:\n> On Sat, Oct 18, 2008 at 11:39, René Scharfe <rene.scharfe@lsrfire.ath.cx> wrote:\n>>>> +static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\n>>>> +               const char *name)\n>>>> +{\n>>>> +       size_t len = strlen(name);\n>>>> +       struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);\n>>>> +       memcpy(ref->name, prefix, prefixlen);\n>>>> +       memcpy(ref->name + prefixlen, name, len);\n>>> Where does you \\0-terminate the string?\n>> xcalloc() calls calloc(), which zeroes the memory.\n> So, you write the memory range twice, just for the last \\0?\n\nWell, one could exclude the name part from zeroing, that's true.  It's usually\nsafer to zero a whole block of memory right at allocation time, lest one\nforgets, though.  I simply kept it they way it was done before.\n\nThat said, here's a patch (4/3):\n\ndiff --git a/remote.c b/remote.c\nindex e530a21..184115d 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -753,9 +753,10 @@ static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\n \t\tconst char *name)\n {\n \tsize_t len = strlen(name);\n-\tstruct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);\n+\tstruct ref *ref = xmalloc(sizeof(struct ref) + prefixlen + len + 1);\n+\tmemset(ref, sizeof(struct ref));\n \tmemcpy(ref->name, prefix, prefixlen);\n-\tmemcpy(ref->name + prefixlen, name, len);\n+\tmemcpy(ref->name + prefixlen, name, len + 1);\n \treturn ref;\n }\n \n"},{"id":"93489","messageId":"48FC5EAE.9000108@op5.se","threadId":"15948","inReplyTo":"36ca99e90810180507q2dedf4ck7262239ae91d892f@mail.gmail.com","subject":"Re: [PATCH 1/3] add alloc_ref_with_prefix()","fromName":"Andreas Ericsson","fromEmail":"ae@op5.se","sentAt":"2008-10-20T10:34:22Z","receivedAt":"2008-10-20T10:34:22Z","isPatch":true,"sender":{"key":"ae@op5.se","avatar":"https://gravatar.com/avatar/426e89595c75a8f5252dd0c989e5fabe5bcac616e68557427ad9aef6b0ca342a?d=mp&s=160"},"body":"Bert Wesarg wrote:\n> On Sat, Oct 18, 2008 at 11:39, René Scharfe <rene.scharfe@lsrfire.ath.cx> wrote:\n>>>> +static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,\n>>>> +               const char *name)\n>>>> +{\n>>>> +       size_t len = strlen(name);\n>>>> +       struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1);\n>>>> +       memcpy(ref->name, prefix, prefixlen);\n>>>> +       memcpy(ref->name + prefixlen, name, len);\n>>> Where does you \\0-terminate the string?\n>> xcalloc() calls calloc(), which zeroes the memory.\n> So, you write the memory range twice, just for the last \\0?\n> \n\ncalloc() doesn't do the zeroing pass if it gets memory from the kernel,\nwhich guarantees that the memory is already zeroed out. In reality,\ncalloc() usually saves on both readability and performance over\nexplicitly nul-terminating strings.\n\n-- \nAndreas Ericsson                   andreas.ericsson@op5.se\nOP5 AB                             www.op5.se\nTel: +46 8-230225                  Fax: +46 8-230231\n"}]}