Re: [PATCH 1/3] add alloc_ref_with_prefix()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Bert Wesarg schrieb:
> On Sat, Oct 18, 2008 at 11:39, René Scharfe <rene.scharfe@xxxxxxxxxxxxxx> 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;
 }
 
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux