summaryrefslogtreecommitdiff
path: root/builtin/checkout.c
diff options
context:
space:
mode:
authorGlen Choo <chooglen@google.com>2022-03-02 00:36:13 (GMT)
committerJunio C Hamano <gitster@pobox.com>2022-03-02 07:27:09 (GMT)
commit8d2eaf649abd28baa083a723d1e041b26d2be83e (patch)
treeae72cb98b4b15b4b19187a5d96aab76d3bcf9258 /builtin/checkout.c
parent4c53a8c20f8984adb226293a3ffd7b88c3f4ac1a (diff)
downloadgit-8d2eaf649abd28baa083a723d1e041b26d2be83e.zip
git-8d2eaf649abd28baa083a723d1e041b26d2be83e.tar.gz
git-8d2eaf649abd28baa083a723d1e041b26d2be83e.tar.bz2
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>
Diffstat (limited to 'builtin/checkout.c')
-rw-r--r--builtin/checkout.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/builtin/checkout.c b/builtin/checkout.c
index cc804ba..a2c59df 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -733,6 +733,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
struct tree_desc trees[2];
struct tree *tree;
struct unpack_trees_options topts;
+ const struct object_id *old_commit_oid;
memset(&topts, 0, sizeof(topts));
topts.head_idx = -1;
@@ -760,9 +761,15 @@ static int merge_working_tree(const struct checkout_opts *opts,
&new_branch_info->commit->object.oid :
&new_branch_info->oid, NULL);
topts.preserve_ignored = !opts->overwrite_ignore;
- tree = parse_tree_indirect(old_branch_info->commit ?
- &old_branch_info->commit->object.oid :
- the_hash_algo->empty_tree);
+
+ old_commit_oid = old_branch_info->commit ?
+ &old_branch_info->commit->object.oid :
+ the_hash_algo->empty_tree;
+ tree = parse_tree_indirect(old_commit_oid);
+ if (!tree)
+ die(_("unable to parse commit %s"),
+ oid_to_hex(old_commit_oid));
+
init_tree_desc(&trees[0], tree->buffer, tree->size);
parse_tree(new_tree);
tree = new_tree;