From: René Scharfe Date: Sat, 18 Oct 2008 12:35:47 GMT Subject: Re: [PATCH 1/3] add alloc_ref_with_prefix() Message-ID: <48F9D823.4000908@lsrfire.ath.cx> In-Reply-To: <36ca99e90810180507q2dedf4ck7262239ae91d892f@mail.gmail.com> Bert Wesarg schrieb: > On Sat, Oct 18, 2008 at 11:39, René Scharfe wrote: >>>> +static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen, >>>> + const char *name) >>>> +{ >>>> + size_t len = strlen(name); >>>> + struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1); >>>> + memcpy(ref->name, prefix, prefixlen); >>>> + memcpy(ref->name + prefixlen, name, len); >>> Where does you \0-terminate the string? >> xcalloc() calls calloc(), which zeroes the memory. > So, you write the memory range twice, just for the last \0? Well, one could exclude the name part from zeroing, that's true. It's usually safer to zero a whole block of memory right at allocation time, lest one forgets, though. I simply kept it they way it was done before. That said, here's a patch (4/3): diff --git a/remote.c b/remote.c index e530a21..184115d 100644 --- a/remote.c +++ b/remote.c @@ -753,9 +753,10 @@ static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen, const char *name) { size_t len = strlen(name); - struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1); + struct ref *ref = xmalloc(sizeof(struct ref) + prefixlen + len + 1); + memset(ref, sizeof(struct ref)); memcpy(ref->name, prefix, prefixlen); - memcpy(ref->name + prefixlen, name, len); + memcpy(ref->name + prefixlen, name, len + 1); return ref; }