commit: fix a segfault when displaying a commit with unreachable parents
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> Signed-off-by: Junio C Hamano <junkio@cox.net>
Eric Wong committed
Oct 11, 2006 at 16:16 UTC
14763e7bda06a502455ef4420067205797b9a907
1 file changed
+7
-4
commit.c
+7
-4
@@ -548,10 +548,13 @@ static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *com
548
549
while (parent) {
550
struct commit *p = parent->item;
551
- const char *hex = abbrev
552
- ? find_unique_abbrev(p->object.sha1, abbrev)
553
- : sha1_to_hex(p->object.sha1);
554
- const char *dots = (abbrev && strlen(hex) != 40) ? "..." : "";
551
+ const char *hex = NULL;
552
+ const char *dots;
553
+ if (abbrev)
554
+ hex = find_unique_abbrev(p->object.sha1, abbrev);
555
+ if (!hex)
556
+ hex = sha1_to_hex(p->object.sha1);
557
+ dots = (abbrev && strlen(hex) != 40) ? "..." : "";
558
parent = parent->next;
559
560
offset += sprintf(buf + offset, " %s%s", hex, dots);