Skip to content

Commit 475d62c

Browse files
committed
Merge branch 'tb/commit-graph-harden' into pu
* tb/commit-graph-harden: commit-graph.c: handle corrupt/missing trees commit-graph.c: handle commit parsing errors t/t5318: introduce failing 'git commit-graph write' tests
2 parents 54b8038 + 806278d commit 475d62c

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

commit-graph.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,12 +839,19 @@ static void write_graph_chunk_data(struct hashfile *f, int hash_len,
839839

840840
while (list < last) {
841841
struct commit_list *parent;
842+
struct object_id *tree;
842843
int edge_value;
843844
uint32_t packedDate[2];
844845
display_progress(ctx->progress, ++ctx->progress_cnt);
845846

846-
parse_commit_no_graph(*list);
847-
hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
847+
if (parse_commit_no_graph(*list))
848+
die(_("unable to parse commit %s"),
849+
oid_to_hex(&(*list)->object.oid));
850+
tree = get_commit_tree_oid(*list);
851+
if (!tree)
852+
die(_("unable to get tree for %s"),
853+
oid_to_hex(&(*list)->object.oid));
854+
hashwrite(f, tree->hash, hash_len);
848855

849856
parent = (*list)->parents;
850857

commit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,8 @@ struct tree *repo_get_commit_tree(struct repository *r,
358358

359359
struct object_id *get_commit_tree_oid(const struct commit *commit)
360360
{
361-
return &get_commit_tree(commit)->object.oid;
361+
struct tree *tree = get_commit_tree(commit);
362+
return tree ? &tree->object.oid : NULL;
362363
}
363364

364365
void release_commit_memory(struct parsed_object_pool *pool, struct commit *c)

t/t5318-commit-graph.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,4 +585,47 @@ test_expect_success 'get_commit_tree_in_graph works for non-the_repository' '
585585
test_cmp expect actual
586586
'
587587

588+
test_expect_success 'corrupt commit-graph write (broken parent)' '
589+
rm -rf repo &&
590+
git init repo &&
591+
(
592+
cd repo &&
593+
empty="$(git mktree </dev/null)" &&
594+
cat >broken <<-EOF &&
595+
tree $empty
596+
parent 0000000000000000000000000000000000000000
597+
author whatever <[email protected]> 1234 -0000
598+
committer whatever <[email protected]> 1234 -0000
599+
600+
broken commit
601+
EOF
602+
broken="$(git hash-object -w -t commit --literally broken)" &&
603+
git commit-tree -p "$broken" -m "good commit" "$empty" >good &&
604+
test_must_fail git commit-graph write --stdin-commits \
605+
<good 2>test_err &&
606+
test_i18ngrep "unable to parse commit" test_err
607+
)
608+
'
609+
610+
test_expect_success 'corrupt commit-graph write (missing tree)' '
611+
rm -rf repo &&
612+
git init repo &&
613+
(
614+
cd repo &&
615+
tree="$(git mktree </dev/null)" &&
616+
cat >broken <<-EOF &&
617+
parent 0000000000000000000000000000000000000000
618+
author whatever <[email protected]> 1234 -0000
619+
committer whatever <[email protected]> 1234 -0000
620+
621+
broken commit
622+
EOF
623+
broken="$(git hash-object -w -t commit --literally broken)" &&
624+
git commit-tree -p "$broken" -m "good" "$tree" >good &&
625+
test_must_fail git commit-graph write --stdin-commits \
626+
<good 2>test_err &&
627+
test_i18ngrep "unable to get tree for" test_err
628+
)
629+
'
630+
588631
test_done

0 commit comments

Comments
 (0)