git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
Oct 18, 2008, 12:35 UTC
Message-ID
<48F9D823.4000908@lsrfire.ath.cx>
In-Reply-To
<36ca99e90810180507q2dedf4ck7262239ae91d892f@mail.gmail.com>
Bert Wesarg schrieb:
Show 11 quoted lines
> On Sat, Oct 18, 2008 at 11:39, René Scharfe <rene.scharfe@lsrfire.ath.cx> 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;
 }
 
Previous: Bert WesargNext: Andreas Ericsson
Message 7 of 8 in “add alloc_ref_with_prefix()”
  1. 1/3 add alloc_ref_with_prefix()René Scharfe, Oct 18, 2008
  2. 2/3 use alloc_ref_from_str() everywhereRené Scharfe, Oct 18, 2008
  3. 3/3 make alloc_ref_from_str() the new alloc_ref()René Scharfe, Oct 18, 2008
  4. Bert WesargOct 18, 2008
  5. René ScharfeOct 18, 2008
  6. Bert WesargOct 18, 2008
  7. René ScharfeOct 18, 2008
  8. Andreas EricssonOct 20, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.