Skip to content

Commit a9c7a8a

Browse files
peffgitster
authored andcommitted
avoid segfault when reading header of malformed commits
If a commit object has a header line at the end of the buffer that is missing its newline (or if it appears so because the content on the header line contains a stray NUL), then git will segfault. Interestingly, this case is explicitly handled and we do correctly scan the final line for the header we are looking for. But if we don't find it, we will dereference NULL while trying to look at the next line. Git will never generate such a commit, but it's good to be defensive. We could die() in such a case, but since it's easy enough to handle it gracefully, let's just issue a warning and continue (so you could still view such a commit with "git show", though you might be missing headers after the NUL). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0f1ea6 commit a9c7a8a

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

pretty.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,12 +439,14 @@ static char *get_header(const struct commit *commit, const char *key)
439439
int key_len = strlen(key);
440440
const char *line = commit->buffer;
441441

442-
for (;;) {
442+
while (line) {
443443
const char *eol = strchr(line, '\n'), *next;
444444

445445
if (line == eol)
446446
return NULL;
447447
if (!eol) {
448+
warning("malformed commit (header is missing newline): %s",
449+
sha1_to_hex(commit->object.sha1));
448450
eol = line + strlen(line);
449451
next = NULL;
450452
} else
@@ -456,6 +458,7 @@ static char *get_header(const struct commit *commit, const char *key)
456458
}
457459
line = next;
458460
}
461+
return NULL;
459462
}
460463

461464
static char *replace_encoding_header(char *buf, const char *encoding)

0 commit comments

Comments
 (0)