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

[PATCH 2/3] fix off-by-one allocation error

From
Jeff King <peff@peff.net>
Date
Jan 29, 2010, 10:31 UTC
Message-ID
<20100129103130.GB6025@coredump.intra.peff.net>
In-Reply-To
<20100129102518.GA5875@coredump.intra.peff.net>

Caught by valgrind in t5516. Reading the code shows we malloc enough for our string, but not trailing NUL.

Signed-off-by: Jeff King <peff@peff.net>
---
This bug was introduced in f517f1f (builtin-push: add --delete as
syntactic sugar for :foo, 2009-12-30), not released yet but part of
1.7.0-rc0. So no need for a 'maint' fix.

An obvious alternative would be to convert it to strbuf (which could also be used to clean up other non-buggy string generation earlier in the function).

 builtin-push.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/builtin-push.c b/builtin-push.c
index 5df6608..5633f0a 100644
--- a/builtin-push.c
+++ b/builtin-push.c
@@ -52,7 +52,7 @@ static void set_refspecs(const char **refs, int nr)
 		} else if (deleterefs && !strchr(ref, ':')) {
 			char *delref;
 			int len = strlen(ref)+1;
-			delref = xmalloc(len);
+			delref = xmalloc(len+1);
 			strcpy(delref, ":");
 			strcat(delref, ref);
 			ref = delref;
-- 
1.7.0.rc0.41.g538720
Previous: Jeff KingNext: Jeff King
Message 4 of 13 in “rerere: fix too-short initialization”
  1. rerere: fix too-short initializationJeff King, Jan 28, 2010
  2. 0/3 valgrind bug roundupJeff King, Jan 29, 2010
  3. 1/3 fix memcpy of overlapping areaJeff King, Jan 29, 2010
  4. 2/3 fix off-by-one allocation errorJeff King, Jan 29, 2010
  5. 3/3 add shebang line to git-mergetool--lib.shJeff King, Jan 29, 2010
  6. Do not install shell libraries executableJonathan Nieder, Jan 29, 2010
  7. Junio C HamanoJan 31, 2010
  8. Do not install shell libraries executableJonathan Nieder, Jan 31, 2010
  9. Junio C HamanoJan 31, 2010
  10. Jonathan NiederJan 31, 2010
  11. Junio C HamanoJan 31, 2010
  12. Jonathan NiederJan 31, 2010
  13. Junio C HamanoJan 31, 2010

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.