Skip to content

Commit 89e653d

Browse files
Martin Ågrengitster
authored andcommitted
merge: setup opts later in checkout_fast_forward()
After we initialize the various fields in `opts` but before we actually use them, we might return early. Move the initialization further down, to immediately before we use `opts`. This limits the scope of `opts` and will help a later commit fix a memory leak without having to worry about those early returns. This patch is best viewed using something like this (note the tab!): --color-moved --anchored=" trees[nr_trees] = parse_tree_indirect" Signed-off-by: Martin Ågren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6f10a09 commit 89e653d

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

merge.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,24 @@ int checkout_fast_forward(const struct object_id *head,
9494
return -1;
9595

9696
memset(&trees, 0, sizeof(trees));
97-
memset(&opts, 0, sizeof(opts));
9897
memset(&t, 0, sizeof(t));
98+
99+
trees[nr_trees] = parse_tree_indirect(head);
100+
if (!trees[nr_trees++]) {
101+
rollback_lock_file(&lock_file);
102+
return -1;
103+
}
104+
trees[nr_trees] = parse_tree_indirect(remote);
105+
if (!trees[nr_trees++]) {
106+
rollback_lock_file(&lock_file);
107+
return -1;
108+
}
109+
for (i = 0; i < nr_trees; i++) {
110+
parse_tree(trees[i]);
111+
init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
112+
}
113+
114+
memset(&opts, 0, sizeof(opts));
99115
if (overwrite_ignore) {
100116
memset(&dir, 0, sizeof(dir));
101117
dir.flags |= DIR_SHOW_IGNORED;
@@ -112,20 +128,6 @@ int checkout_fast_forward(const struct object_id *head,
112128
opts.fn = twoway_merge;
113129
setup_unpack_trees_porcelain(&opts, "merge");
114130

115-
trees[nr_trees] = parse_tree_indirect(head);
116-
if (!trees[nr_trees++]) {
117-
rollback_lock_file(&lock_file);
118-
return -1;
119-
}
120-
trees[nr_trees] = parse_tree_indirect(remote);
121-
if (!trees[nr_trees++]) {
122-
rollback_lock_file(&lock_file);
123-
return -1;
124-
}
125-
for (i = 0; i < nr_trees; i++) {
126-
parse_tree(trees[i]);
127-
init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
128-
}
129131
if (unpack_trees(nr_trees, t, &opts)) {
130132
rollback_lock_file(&lock_file);
131133
return -1;

0 commit comments

Comments
 (0)