Skip to content

Commit 80693e3

Browse files
committed
Merge branch 'tb/commit-graph-harden'
The code to parse and use the commit-graph file has been made more robust against corrupted input. * 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 ae203ba + 806278d commit 80693e3

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
@@ -846,12 +846,19 @@ static void write_graph_chunk_data(struct hashfile *f, int hash_len,
846846

847847
while (list < last) {
848848
struct commit_list *parent;
849+
struct object_id *tree;
849850
int edge_value;
850851
uint32_t packedDate[2];
851852
display_progress(ctx->progress, ++ctx->progress_cnt);
852853

853-
parse_commit_no_graph(*list);
854-
hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
854+
if (parse_commit_no_graph(*list))
855+
die(_("unable to parse commit %s"),
856+
oid_to_hex(&(*list)->object.oid));
857+
tree = get_commit_tree_oid(*list);
858+
if (!tree)
859+
die(_("unable to get tree for %s"),
860+
oid_to_hex(&(*list)->object.oid));
861+
hashwrite(f, tree->hash, hash_len);
855862

856863
parent = (*list)->parents;
857864

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
@@ -621,4 +621,47 @@ test_expect_success 'get_commit_tree_in_graph works for non-the_repository' '
621621
test_cmp expect actual
622622
'
623623

624+
test_expect_success 'corrupt commit-graph write (broken parent)' '
625+
rm -rf repo &&
626+
git init repo &&
627+
(
628+
cd repo &&
629+
empty="$(git mktree </dev/null)" &&
630+
cat >broken <<-EOF &&
631+
tree $empty
632+
parent 0000000000000000000000000000000000000000
633+
author whatever <[email protected]> 1234 -0000
634+
committer whatever <[email protected]> 1234 -0000
635+
636+
broken commit
637+
EOF
638+
broken="$(git hash-object -w -t commit --literally broken)" &&
639+
git commit-tree -p "$broken" -m "good commit" "$empty" >good &&
640+
test_must_fail git commit-graph write --stdin-commits \
641+
<good 2>test_err &&
642+
test_i18ngrep "unable to parse commit" test_err
643+
)
644+
'
645+
646+
test_expect_success 'corrupt commit-graph write (missing tree)' '
647+
rm -rf repo &&
648+
git init repo &&
649+
(
650+
cd repo &&
651+
tree="$(git mktree </dev/null)" &&
652+
cat >broken <<-EOF &&
653+
parent 0000000000000000000000000000000000000000
654+
author whatever <[email protected]> 1234 -0000
655+
committer whatever <[email protected]> 1234 -0000
656+
657+
broken commit
658+
EOF
659+
broken="$(git hash-object -w -t commit --literally broken)" &&
660+
git commit-tree -p "$broken" -m "good" "$tree" >good &&
661+
test_must_fail git commit-graph write --stdin-commits \
662+
<good 2>test_err &&
663+
test_i18ngrep "unable to get tree for" test_err
664+
)
665+
'
666+
624667
test_done

0 commit comments

Comments
 (0)