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

[PATCH] commit: fix a segfault when displaying a commit with unreachable parents

From
EWEric Wong <normalperson@yhbt.net>
Date
Oct 11, 2006, 23:16 UTC
Message-ID
<11606085621137-git-send-email-normalperson@yhbt.net>

I was running git show on various commits found by fsck-objects when I found this bug. Since find_unique_abbrev() cannot find an abbreviation for an object not in the database, it will return NULL, which is bad to run strlen() on. So instead, we'll just display the unabbreviated sha1 that we referenced in the commit.

I'm not sure that this is the best 'fix' for it because the commit I was trying to show was broken, but I don't think a program should segfault even if the user tries to do something stupid.

Signed-off-by: Eric Wong <normalperson@yhbt.net>
---
 commit.c |   11 +++++++----
 1 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/commit.c b/commit.c
index 5b6e082..a6d543e 100644
--- a/commit.c
+++ b/commit.c
@@ -548,10 +548,13 @@ static int add_merge_info(enum cmit_fmt 
 
 	while (parent) {
 		struct commit *p = parent->item;
-		const char *hex = abbrev
-			? find_unique_abbrev(p->object.sha1, abbrev)
-			: sha1_to_hex(p->object.sha1);
-		const char *dots = (abbrev && strlen(hex) != 40) ? "..." : "";
+		const char *hex = NULL;
+		const char *dots;
+		if (abbrev)
+			hex = find_unique_abbrev(p->object.sha1, abbrev);
+		if (!hex)
+			hex = sha1_to_hex(p->object.sha1);
+		dots = (abbrev && strlen(hex) != 40) ?  "..." : "";
 		parent = parent->next;
 
 		offset += sprintf(buf + offset, " %s%s", hex, dots);
-- 
1.4.3.rc2.g823d6-dirty
Message 1 of 1 in “commit: fix a segfault when displaying a commit with unreachable parents”
  1. commit: fix a segfault when displaying a commit with unreachable parentsEric Wong, Oct 11, 2006

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.