git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] sparse-index: avoid crash on intent-to-add entry outside the cone

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 29, 2026, 21:51 UTC
Message-ID
<xmqq33x1o465.fsf@gitster.g>
In-Reply-To
<pull.2167.git.1783345853272.gitgitgadget@gmail.com>
"Derrick Stolee via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 15 quoted lines
> From: Derrick Stolee <stolee@gmail.com>
>
> When collapsing a full index to a sparse index, the recursive
> convert_to_sparse_rec() walks the cache tree to determine if any
> of the cache tree entries can be used to represent a sparse directory.
>
> As it goes, the method tracks how many cache entries are being represented
> by the cache tree entry. The cache tree node's 'entry_count' represents how
> many cache entries are covered by the node.
>
> However, this value can be negative, representing that a node is invalid,
> and is no longer reflecting the number of cache entries fit within. This can
> happen when the user uses 'git add --intent-to-add' to mark an untracked
> file with the intent-to-add bit to avoid committing without finishing the
> add.

Yes. If the code were not anticipating this, I can understand how a bug can arise ;-)

Show 7 quoted lines
> Theoretically, we could scan to find the range of entries that match this
> directory and determine if they truly do have an intent-to-add bit and then
> collapse as many child trees as possible (the ones with valid cache tree
> nodes). That would be a non-trivial change for performance-only benefit.
> Since this combination of the intent-to-add and sparse index features has so
> far gone undetected by real users, this scenario is unlikely to be worth
> such a change.
I tend to agree.  That does sound nasty.
Show 15 quoted lines
> diff --git a/sparse-index.c b/sparse-index.c
> index 1ed769b78d..c1fa231a89 100644
> --- a/sparse-index.c
> +++ b/sparse-index.c
> @@ -113,10 +113,17 @@ static int convert_to_sparse_rec(struct index_state *istate,
>  			continue;
>  		}
>  
> +		span = ct->down[pos]->cache_tree->entry_count;
> +		if (span < 0) {
> +			/* cache-tree entry is invalidated, cannot collapse. */
> +			istate->cache[num_converted++] = ce;
> +			i++;
> +			continue;
> +		}
OK.  That is an easy and safe cop-out that is much better than segfaulting.
Shall we mark the topic for 'next'?
Thanks.
Previous: Derrick Stolee via GitGitGadgetNext: Derrick Stolee
Message 2 of 4 in “sparse-index: avoid crash on intent-to-add entry outside the cone”
  1. sparse-index: avoid crash on intent-to-add entry outside the coneDerrick Stolee via GitGitGadget, Jul 6, 2026
  2. Junio C HamanoJul 29, 2026
  3. Derrick StoleeJul 31, 2026
  4. Junio C HamanoJul 31, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.