]> git.ipfire.org Git - thirdparty/git.git/commitdiff
checkout, clone: die if tree cannot be parsed
authorGlen Choo <chooglen@google.com>
Wed, 2 Mar 2022 00:36:13 +0000 (16:36 -0800)
committerJunio C Hamano <gitster@pobox.com>
Wed, 2 Mar 2022 07:27:09 +0000 (23:27 -0800)
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>
builtin/checkout.c
builtin/clone.c

index cc804ba8e1e6cdb8748eabc7e91575ac2ee53a4a..a2c59dfcae2a7b1a7c2c8126a656002a30e14df9 100644 (file)
@@ -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;
index 727e16e0aea435a1fe244c381d8bb7670e8e7a5a..2d04c54d0c0536ffd6107a9ee3ff269ae58ee6ee 100644 (file)
@@ -695,6 +695,8 @@ static int checkout(int submodule_progress)
        init_checkout_metadata(&opts.meta, head, &oid, NULL);
 
        tree = parse_tree_indirect(&oid);
+       if (!tree)
+               die(_("unable to parse commit %s"), oid_to_hex(&oid));
        parse_tree(tree);
        init_tree_desc(&t, tree->buffer, tree->size);
        if (unpack_trees(1, &t, &opts) < 0)