From 7059dccc6c60a872a314b19ac17702065a71d6bd Mon Sep 17 00:00:00 2001 From: Jeff King Date: Thu, 24 Oct 2013 04:52:36 -0400 Subject: log_tree_diff: die when we fail to parse a commit We currently call parse_commit and then assume we can dereference the resulting "tree" struct field. If parsing failed, however, that field is NULL and we end up segfaulting. Instead of a segfault, let's print an error message and die a little more gracefully. Note that this should never happen in practice, but may happen in a corrupt repository. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- commit.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'commit.c') diff --git a/commit.c b/commit.c index a575564a15..26c1d5406d 100644 --- a/commit.c +++ b/commit.c @@ -341,6 +341,13 @@ int parse_commit(struct commit *item) return ret; } +void parse_commit_or_die(struct commit *item) +{ + if (parse_commit(item)) + die("unable to parse commit %s", + item ? sha1_to_hex(item->object.sha1) : "(null)"); +} + int find_commit_subject(const char *commit_buffer, const char **subject) { const char *eol; -- cgit v1.2.3 From 5e7d4d3e932432131d0f8f4195e0061ecf644865 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Thu, 24 Oct 2013 04:53:19 -0400 Subject: assume parse_commit checks for NULL commit The parse_commit function will check whether it was passed a NULL commit pointer, and if so, return an error. There is no need for callers to check this separately. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- commit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'commit.c') diff --git a/commit.c b/commit.c index 26c1d5406d..8535e5cf35 100644 --- a/commit.c +++ b/commit.c @@ -79,7 +79,7 @@ struct commit *lookup_commit_reference_by_name(const char *name) if (get_sha1_committish(name, sha1)) return NULL; commit = lookup_commit_reference(sha1); - if (!commit || parse_commit(commit)) + if (parse_commit(commit)) return NULL; return commit; } -- cgit v1.2.3