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 <chooglen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Glen Choo committed
Mar 1, 2022 at 16:36 UTC
8d2eaf649abd28baa083a723d1e041b26d2be83e
2 files changed
+12
-3
builtin/checkout.c
+10
-3
@@ -733,6 +733,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
733
struct tree_desc trees[2];
734
struct tree *tree;
735
struct unpack_trees_options topts;
736
+ const struct object_id *old_commit_oid;
737
738
memset(&topts, 0, sizeof(topts));
739
topts.head_idx = -1;
@@ -760,9 +761,15 @@ static int merge_working_tree(const struct checkout_opts *opts,
761
&new_branch_info->commit->object.oid :
762
&new_branch_info->oid, NULL);
763
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
+
773
init_tree_desc(&trees[0], tree->buffer, tree->size);
774
parse_tree(new_tree);
775
tree = new_tree;
builtin/clone.c
+2
@@ -695,6 +695,8 @@ static int checkout(int submodule_progress)
695
init_checkout_metadata(&opts.meta, head, &oid, NULL);
696
697
tree = parse_tree_indirect(&oid);
698
+ if (!tree)
699
+ die(_("unable to parse commit %s"), oid_to_hex(&oid));
700
parse_tree(tree);
701
init_tree_desc(&t, tree->buffer, tree->size);
702
if (unpack_trees(1, &t, &opts) < 0)