Skip to content

Commit aa9f618

Browse files
dschogitster
authored andcommitted
Always check parse_tree*()'s return value
Otherwise we may easily run into serious crashes: For example, if we run `init_tree_desc()` directly after a failed `parse_tree()`, we are accessing uninitialized data or trying to dereference `NULL`. Note that the `parse_tree()` function already takes care of showing an error message. The `parse_tree_indirectly()` and `repo_get_commit_tree()` functions do not, therefore those latter call sites need to show a useful error message while the former do not. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 98c6d16 commit aa9f618

File tree

12 files changed

+52
-10
lines changed

12 files changed

+52
-10
lines changed

builtin/checkout.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,8 @@ static int reset_tree(struct tree *tree, const struct checkout_opts *o,
707707
init_checkout_metadata(&opts.meta, info->refname,
708708
info->commit ? &info->commit->object.oid : null_oid(),
709709
NULL);
710-
parse_tree(tree);
710+
if (parse_tree(tree) < 0)
711+
return 128;
711712
init_tree_desc(&tree_desc, tree->buffer, tree->size);
712713
switch (unpack_trees(1, &tree_desc, &opts)) {
713714
case -2:
@@ -786,9 +787,15 @@ static int merge_working_tree(const struct checkout_opts *opts,
786787
if (new_branch_info->commit)
787788
BUG("'switch --orphan' should never accept a commit as starting point");
788789
new_tree = parse_tree_indirect(the_hash_algo->empty_tree);
789-
} else
790+
if (!new_tree)
791+
BUG("unable to read empty tree");
792+
} else {
790793
new_tree = repo_get_commit_tree(the_repository,
791794
new_branch_info->commit);
795+
if (!new_tree)
796+
return error(_("unable to read tree (%s)"),
797+
oid_to_hex(&new_branch_info->commit->object.oid));
798+
}
792799
if (opts->discard_changes) {
793800
ret = reset_tree(new_tree, opts, 1, writeout_error, new_branch_info);
794801
if (ret)
@@ -823,7 +830,8 @@ static int merge_working_tree(const struct checkout_opts *opts,
823830
oid_to_hex(old_commit_oid));
824831

825832
init_tree_desc(&trees[0], tree->buffer, tree->size);
826-
parse_tree(new_tree);
833+
if (parse_tree(new_tree) < 0)
834+
exit(128);
827835
tree = new_tree;
828836
init_tree_desc(&trees[1], tree->buffer, tree->size);
829837

@@ -1239,10 +1247,15 @@ static void setup_new_branch_info_and_source_tree(
12391247
if (!new_branch_info->commit) {
12401248
/* not a commit */
12411249
*source_tree = parse_tree_indirect(rev);
1250+
if (!*source_tree)
1251+
die(_("unable to read tree (%s)"), oid_to_hex(rev));
12421252
} else {
12431253
parse_commit_or_die(new_branch_info->commit);
12441254
*source_tree = repo_get_commit_tree(the_repository,
12451255
new_branch_info->commit);
1256+
if (!*source_tree)
1257+
die(_("unable to read tree (%s)"),
1258+
oid_to_hex(&new_branch_info->commit->object.oid));
12461259
}
12471260
}
12481261

builtin/clone.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,8 @@ static int checkout(int submodule_progress, int filter_submodules)
736736
tree = parse_tree_indirect(&oid);
737737
if (!tree)
738738
die(_("unable to parse commit %s"), oid_to_hex(&oid));
739-
parse_tree(tree);
739+
if (parse_tree(tree) < 0)
740+
exit(128);
740741
init_tree_desc(&t, tree->buffer, tree->size);
741742
if (unpack_trees(1, &t, &opts) < 0)
742743
die(_("unable to checkout working tree"));

builtin/commit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ static void create_base_index(const struct commit *current_head)
339339
tree = parse_tree_indirect(&current_head->object.oid);
340340
if (!tree)
341341
die(_("failed to unpack HEAD tree object"));
342-
parse_tree(tree);
342+
if (parse_tree(tree) < 0)
343+
exit(128);
343344
init_tree_desc(&t, tree->buffer, tree->size);
344345
if (unpack_trees(1, &t, &opts))
345346
exit(128); /* We've already reported the error, finish dying */

builtin/merge-tree.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,12 +447,18 @@ static int real_merge(struct merge_tree_options *o,
447447
if (repo_get_oid_treeish(the_repository, merge_base, &base_oid))
448448
die(_("could not parse as tree '%s'"), merge_base);
449449
base_tree = parse_tree_indirect(&base_oid);
450+
if (!base_tree)
451+
die(_("unable to read tree (%s)"), oid_to_hex(&base_oid));
450452
if (repo_get_oid_treeish(the_repository, branch1, &head_oid))
451453
die(_("could not parse as tree '%s'"), branch1);
452454
parent1_tree = parse_tree_indirect(&head_oid);
455+
if (!parent1_tree)
456+
die(_("unable to read tree (%s)"), oid_to_hex(&head_oid));
453457
if (repo_get_oid_treeish(the_repository, branch2, &merge_oid))
454458
die(_("could not parse as tree '%s'"), branch2);
455459
parent2_tree = parse_tree_indirect(&merge_oid);
460+
if (!parent2_tree)
461+
die(_("unable to read tree (%s)"), oid_to_hex(&merge_oid));
456462

457463
opt.ancestor = merge_base;
458464
merge_incore_nonrecursive(&opt, base_tree, parent1_tree, parent2_tree, &result);

builtin/read-tree.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
263263
cache_tree_free(&the_index.cache_tree);
264264
for (i = 0; i < nr_trees; i++) {
265265
struct tree *tree = trees[i];
266-
parse_tree(tree);
266+
if (parse_tree(tree) < 0)
267+
return 128;
267268
init_tree_desc(t+i, tree->buffer, tree->size);
268269
}
269270
if (unpack_trees(nr_trees, t, &opts))

builtin/reset.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ static int reset_index(const char *ref, const struct object_id *oid, int reset_t
119119

120120
if (reset_type == MIXED || reset_type == HARD) {
121121
tree = parse_tree_indirect(oid);
122+
if (!tree) {
123+
error(_("unable to read tree (%s)"), oid_to_hex(oid));
124+
goto out;
125+
}
122126
prime_cache_tree(the_repository, the_repository->index, tree);
123127
}
124128

cache-tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,8 @@ static void prime_cache_tree_rec(struct repository *r,
779779
struct cache_tree_sub *sub;
780780
struct tree *subtree = lookup_tree(r, &entry.oid);
781781

782-
if (!subtree->object.parsed)
783-
parse_tree(subtree);
782+
if (!subtree->object.parsed && parse_tree(subtree) < 0)
783+
exit(128);
784784
sub = cache_tree_sub(it, entry.path);
785785
sub->cache_tree = cache_tree();
786786

merge-ort.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4983,6 +4983,9 @@ static void merge_ort_nonrecursive_internal(struct merge_options *opt,
49834983

49844984
if (result->clean >= 0) {
49854985
result->tree = parse_tree_indirect(&working_tree_oid);
4986+
if (!result->tree)
4987+
die(_("unable to read tree (%s)"),
4988+
oid_to_hex(&working_tree_oid));
49864989
/* existence of conflicted entries implies unclean */
49874990
result->clean &= strmap_empty(&opt->priv->conflicted);
49884991
}

merge-recursive.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ static inline int merge_detect_rename(struct merge_options *opt)
410410

411411
static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
412412
{
413-
parse_tree(tree);
413+
if (parse_tree(tree) < 0)
414+
exit(128);
414415
init_tree_desc(desc, tree->buffer, tree->size);
415416
}
416417

merge.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ int checkout_fast_forward(struct repository *r,
8080
return -1;
8181
}
8282
for (i = 0; i < nr_trees; i++) {
83-
parse_tree(trees[i]);
83+
if (parse_tree(trees[i]) < 0) {
84+
rollback_lock_file(&lock_file);
85+
return -1;
86+
}
8487
init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
8588
}
8689

0 commit comments

Comments
 (0)