Skip to content

Commit 7059dcc

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e230c56 commit 7059dcc

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

commit.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,13 @@ int parse_commit(struct commit *item)
341341
return ret;
342342
}
343343

344+
void parse_commit_or_die(struct commit *item)
345+
{
346+
if (parse_commit(item))
347+
die("unable to parse commit %s",
348+
item ? sha1_to_hex(item->object.sha1) : "(null)");
349+
}
350+
344351
int find_commit_subject(const char *commit_buffer, const char **subject)
345352
{
346353
const char *eol;

commit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_n
4949

5050
int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size);
5151
int parse_commit(struct commit *item);
52+
void parse_commit_or_die(struct commit *item);
5253

5354
/* Find beginning and length of commit subject. */
5455
int find_commit_subject(const char *commit_buffer, const char **subject);

log-tree.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
734734
if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
735735
return 0;
736736

737-
parse_commit(commit);
737+
parse_commit_or_die(commit);
738738
sha1 = commit->tree->object.sha1;
739739

740740
/* Root commit? */
@@ -759,7 +759,7 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
759759
* parent, showing summary diff of the others
760760
* we merged _in_.
761761
*/
762-
parse_commit(parents->item);
762+
parse_commit_or_die(parents->item);
763763
diff_tree_sha1(parents->item->tree->object.sha1,
764764
sha1, "", &opt->diffopt);
765765
log_tree_diff_flush(opt);
@@ -774,7 +774,7 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
774774
for (;;) {
775775
struct commit *parent = parents->item;
776776

777-
parse_commit(parent);
777+
parse_commit_or_die(parent);
778778
diff_tree_sha1(parent->tree->object.sha1,
779779
sha1, "", &opt->diffopt);
780780
log_tree_diff_flush(opt);

0 commit comments

Comments
 (0)