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

[PATCH] for-each-ref: fix segfault in copy_email

From
Jeff King <peff@peff.net>
Date
May 18, 2009, 17:58 UTC
Message-ID
<20090518175811.GB14509@sigill.intra.peff.net>
In-Reply-To
<20090518174547.GA14509@sigill.intra.peff.net>
You can trigger a segfault in git.git by doing:
  git for-each-ref --format='%(taggeremail)' refs/tags/v0.99

The v0.99 tag is special in that it contains no "tagger" header.

The bug is obvious in copy_email, which carefully checks to make sure the result of a strchr is non-NULL, but only after already having used it to perform other work. The fix is to move the check up.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin-for-each-ref.c |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/builtin-for-each-ref.c b/builtin-for-each-ref.c
index 91e8f95..d091e04 100644
--- a/builtin-for-each-ref.c
+++ b/builtin-for-each-ref.c
@@ -339,8 +339,11 @@ static const char *copy_name(const char *buf)
 static const char *copy_email(const char *buf)
 {
 	const char *email = strchr(buf, '<');
-	const char *eoemail = strchr(email, '>');
-	if (!email || !eoemail)
+	const char *eoemail;
+	if (!email)
+		return "";
+	eoemail = strchr(email, '>');
+	if (!eoemail)
 		return "";
 	return xmemdupz(email, eoemail + 1 - email);
 }
-- 
1.6.3.1.137.g9019.dirty
Previous: Jeff KingNext: Thomas Rast
Message 5 of 7 in “Pretty date option for git tag?”
  1. dloewenherz@gmail.comMay 18, 2009
  2. Michael J GruberMay 18, 2009
  3. Linus TorvaldsMay 18, 2009
  4. Jeff KingMay 18, 2009
  5. for-each-ref: fix segfault in copy_emailJeff King, May 18, 2009
  6. Thomas RastMay 18, 2009
  7. Jeff KingMay 18, 2009

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.