cache-tree: fix verify_cache() to catch non-adjacent D/F conflicts
verify_cache() checks that the index does not contain both "path" and
"path/file" before writing a tree. It does this by comparing only
adjacent entries, relying on the assumption that "path/file" would
immediately follow "path" in sorted order. Unfortunately, this
assumption does not always hold. For example:
docs <-- submodule entry
docs-internal/README.md <-- intervening entry
docs/requirements.txt <-- D/F conflict, NOT adjacent to "docs"
When this happens, verify_cache() silently misses the D/F conflict and
write-tree produces a corrupt tree object containing duplicate entries
(one for the submodule "docs" and one for the tree "docs").
I could not find any caller in current git that both allows the index to
get into this state and then tries to write it out without doing other
checks beyond the verify_cache() call in cache_tree_update(), but
verify_cache() is documented as a safety net for preventing corrupt
trees and should actually provide that guarantee. A downstream consumer
that relied solely on cache_tree_update()'s internal checking via
verify_cache() to prevent duplicate tree entries was bitten by the gap.
Add a test that constructs a corrupt index directly (bypassing the D/F
checks in add_index_entry) and verifies that write-tree now rejects it.
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Elijah Newren committedJun 14, 2026 at 06:37 UTC0c8eb46fbbfc48065bc93dcc4c9901031615516c
4 files changed+151-11
cache-tree.c
+53-11
index 7881b42aa2..4d2669b312 100644--- a/cache-tree.c+++ b/cache-tree.c@@ -161,6 +161,54 @@ void cache_tree_invalidate_path(struct index_state *istate, const char *path) istate->cache_changed |= CACHE_TREE_CHANGED; }+/*+ * Check whether this_ce and the next entry in the index form a D/F+ * conflict ("path" vs "path/file"). Returns the conflicting "path/..."+ * name when one is found, or NULL otherwise.+ *+ * The cache is sorted, so "path/file" sorts after "path" and the+ * conflict is usually visible as adjacent entries. But other entries+ * can sort between them -- e.g. "path-internal" sits between "path"+ * and "path/file" because '-' (0x2D) precedes '/' (0x2F) -- so when+ * the immediately following entry shares our prefix but starts with a+ * character that sorts before '/', binary search for "path/" instead.+ */+static const char *find_df_conflict(struct index_state *istate,+ const struct cache_entry *this_ce,+ const struct cache_entry *next_ce)+{+ const char *this_name = this_ce->name;+ const char *next_name = next_ce->name;+ int this_len = ce_namelen(this_ce);+ const struct cache_entry *other;+ struct strbuf probe = STRBUF_INIT;+ int pos;++ if (this_len >= ce_namelen(next_ce) ||+ next_name[this_len] > '/' ||+ strncmp(this_name, next_name, this_len))+ return NULL;++ if (next_name[this_len] == '/')+ return next_name;++ strbuf_add(&probe, this_name, this_len);+ strbuf_addch(&probe, '/');+ pos = index_name_pos_sparse(istate, probe.buf, probe.len);+ strbuf_release(&probe);++ if (pos < 0)+ pos = -pos - 1;+ if (pos >= (int)istate->cache_nr)+ return NULL;+ other = istate->cache[pos];+ if (ce_namelen(other) > this_len &&+ other->name[this_len] == '/' &&+ !strncmp(this_name, other->name, this_len))+ return other->name;+ return NULL;+}+ static int verify_cache(struct index_state *istate, int flags) { unsigned i, funny;@@ -190,24 +238,18 @@ static int verify_cache(struct index_state *istate, int flags) */ funny = 0; for (i = 0; i + 1 < istate->cache_nr; i++) {- /* path/file always comes after path because of the way- * the cache is sorted. Also path can appear only once,- * which means conflicting one would immediately follow.- */ const struct cache_entry *this_ce = istate->cache[i]; const struct cache_entry *next_ce = istate->cache[i + 1];- const char *this_name = this_ce->name;- const char *next_name = next_ce->name;- int this_len = ce_namelen(this_ce);- if (this_len < ce_namelen(next_ce) &&- next_name[this_len] == '/' &&- strncmp(this_name, next_name, this_len) == 0) {+ const char *conflict_name;++ conflict_name = find_df_conflict(istate, this_ce, next_ce);+ if (conflict_name) { if (10 < ++funny) { fprintf(stderr, "...\n"); break; } fprintf(stderr, "You have both %s and %s\n",- this_name, next_name);+ this_ce->name, conflict_name); } } if (funny)