Skip to content

Commit 8d2eaf6

Browse files
chooglengitster
authored andcommitted
checkout, clone: die if tree cannot be parsed
When a tree oid is invalid, parse_tree_indirect() can return NULL. Check for NULL instead of proceeding as though it were a valid pointer and segfaulting. Signed-off-by: Glen Choo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4c53a8c commit 8d2eaf6

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

builtin/checkout.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
733733
struct tree_desc trees[2];
734734
struct tree *tree;
735735
struct unpack_trees_options topts;
736+
const struct object_id *old_commit_oid;
736737

737738
memset(&topts, 0, sizeof(topts));
738739
topts.head_idx = -1;
@@ -760,9 +761,15 @@ static int merge_working_tree(const struct checkout_opts *opts,
760761
&new_branch_info->commit->object.oid :
761762
&new_branch_info->oid, NULL);
762763
topts.preserve_ignored = !opts->overwrite_ignore;
763-
tree = parse_tree_indirect(old_branch_info->commit ?
764-
&old_branch_info->commit->object.oid :
765-
the_hash_algo->empty_tree);
764+
765+
old_commit_oid = old_branch_info->commit ?
766+
&old_branch_info->commit->object.oid :
767+
the_hash_algo->empty_tree;
768+
tree = parse_tree_indirect(old_commit_oid);
769+
if (!tree)
770+
die(_("unable to parse commit %s"),
771+
oid_to_hex(old_commit_oid));
772+
766773
init_tree_desc(&trees[0], tree->buffer, tree->size);
767774
parse_tree(new_tree);
768775
tree = new_tree;

builtin/clone.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,8 @@ static int checkout(int submodule_progress)
695695
init_checkout_metadata(&opts.meta, head, &oid, NULL);
696696

697697
tree = parse_tree_indirect(&oid);
698+
if (!tree)
699+
die(_("unable to parse commit %s"), oid_to_hex(&oid));
698700
parse_tree(tree);
699701
init_tree_desc(&t, tree->buffer, tree->size);
700702
if (unpack_trees(1, &t, &opts) < 0)

0 commit comments

Comments
 (0)