Skip to content

Commit 7abd8fb

Browse files
committed
Merge branch 'jn/plug-empty-tree-leak'
* jn/plug-empty-tree-leak: merge-recursive: take advantage of hardcoded empty tree revert: plug memory leak in "cherry-pick root commit" codepath
2 parents 22f6578 + 03f622c commit 7abd8fb

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

builtin/revert.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,7 @@ static void write_message(struct strbuf *msgbuf, const char *filename)
258258

259259
static struct tree *empty_tree(void)
260260
{
261-
struct tree *tree = xcalloc(1, sizeof(struct tree));
262-
263-
tree->object.parsed = 1;
264-
tree->object.type = OBJ_TREE;
265-
pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
266-
return tree;
261+
return lookup_tree((const unsigned char *)EMPTY_TREE_SHA1_BIN);
267262
}
268263

269264
static NORETURN void die_dirty_index(const char *me)

merge-recursive.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,12 +1601,10 @@ int merge_recursive(struct merge_options *o,
16011601

16021602
merged_common_ancestors = pop_commit(&ca);
16031603
if (merged_common_ancestors == NULL) {
1604-
/* if there is no common ancestor, make an empty tree */
1605-
struct tree *tree = xcalloc(1, sizeof(struct tree));
1604+
/* if there is no common ancestor, use an empty tree */
1605+
struct tree *tree;
16061606

1607-
tree->object.parsed = 1;
1608-
tree->object.type = OBJ_TREE;
1609-
pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
1607+
tree = lookup_tree((const unsigned char *)EMPTY_TREE_SHA1_BIN);
16101608
merged_common_ancestors = make_virtual_commit(tree, "ancestor");
16111609
}
16121610

t/t3503-cherry-pick-root.sh

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@ test_expect_success setup '
1616
echo second > file2 &&
1717
git add file2 &&
1818
test_tick &&
19-
git commit -m "second"
19+
git commit -m "second" &&
20+
21+
git symbolic-ref HEAD refs/heads/third &&
22+
rm .git/index file2 &&
23+
echo third > file3 &&
24+
git add file3 &&
25+
test_tick &&
26+
git commit -m "third"
2027
2128
'
2229

2330
test_expect_success 'cherry-pick a root commit' '
2431
32+
git checkout second^0 &&
2533
git cherry-pick master &&
2634
echo first >expect &&
2735
test_cmp expect file1
@@ -50,4 +58,21 @@ test_expect_success 'revert a root commit with an external strategy' '
5058
5159
'
5260

61+
test_expect_success 'cherry-pick two root commits' '
62+
63+
echo first >expect.file1 &&
64+
echo second >expect.file2 &&
65+
echo third >expect.file3 &&
66+
67+
git checkout second^0 &&
68+
git cherry-pick master third &&
69+
70+
test_cmp expect.file1 file1 &&
71+
test_cmp expect.file2 file2 &&
72+
test_cmp expect.file3 file3 &&
73+
git rev-parse --verify HEAD^^ &&
74+
test_must_fail git rev-parse --verify HEAD^^^
75+
76+
'
77+
5378
test_done

0 commit comments

Comments
 (0)