Skip to content

Commit 8cc21ce

Browse files
committed
read-tree A B: do not corrupt cache-tree
An earlier commit aab3b9a (read-tree A B C: do not create a bogus index and do not segfault, 2009-03-12) resurrected the support for an obscure (but useful) feature to read and overlay more than one tree into the index without the -m (merge) option. But the fix was not enough. Exercising this feature exposes a longstanding bug in the code that primes the cache-tree in the index from the tree that was read. The intention was that when we know that the index must exactly match the tree we just read, we prime the entire cache-tree with it. However, the logic to detect that case incorrectly triggered if you read two trees without -m. This resulted in a corrupted cache-tree, and write-tree would have produced an incorrect tree object out of such an index. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1f9b620 commit 8cc21ce

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

builtin-read-tree.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
211211
case 3:
212212
default:
213213
opts.fn = threeway_merge;
214-
cache_tree_free(&active_cache_tree);
215214
break;
216215
}
217216

@@ -221,6 +220,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
221220
opts.head_idx = 1;
222221
}
223222

223+
cache_tree_free(&active_cache_tree);
224224
for (i = 0; i < nr_trees; i++) {
225225
struct tree *tree = trees[i];
226226
parse_tree(tree);
@@ -235,10 +235,8 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
235235
* valid cache-tree because the index must match exactly
236236
* what came from the tree.
237237
*/
238-
if (nr_trees && !opts.prefix && (!opts.merge || (stage == 2))) {
239-
cache_tree_free(&active_cache_tree);
238+
if (nr_trees == 1 && !opts.prefix)
240239
prime_cache_tree();
241-
}
242240

243241
if (write_cache(newfd, active_cache, active_nr) ||
244242
commit_locked_index(&lock_file))

0 commit comments

Comments
 (0)