]> git.ipfire.org Git - thirdparty/git.git/commit
cache-tree: fix verify_cache() to catch non-adjacent D/F conflicts
authorElijah Newren <newren@gmail.com>
Sun, 14 Jun 2026 06:37:26 +0000 (06:37 +0000)
committerJunio C Hamano <gitster@pobox.com>
Sun, 14 Jun 2026 14:50:47 +0000 (07:50 -0700)
commit0c8eb46fbbfc48065bc93dcc4c9901031615516c
treea1df1bf5b641a04895f03936fb7fa2cf7f38b9d4
parent43a5fa7f5a9b7c44dd958a21368d690fa55d4f50
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>
cache-tree.c
t/meson.build
t/t0093-direct-index-write.pl [new file with mode: 0644]
t/t0093-verify-cache-df-gap.sh [new file with mode: 0755]