Subject: Re: [PATCH v2 6/9] t4058: explore duplicate tree entry handling in a bit more detail

From: Ævar Arnfjörð Bjarmason <[email-masked]>
Date: Wed, 21 Apr 2021 14:29:53 +0200

On Fri, Dec 11 2020, Elijah Newren via GitGitGadget wrote:

> While creating the last commit, I found a number of other cases where
> git would segfault when faced with trees that have duplicate entries.
> None of these segfaults are in the diffcore-rename code (they all occur
> in cache-tree and unpack-trees).  Further, to my knowledge, no one has
> ever been adversely affected by these bugs, and given that it has been
> 15 years and folks have fixed a few other issues with historical
> duplicate entries (as noted in the last commit), I am not sure we will
> ever run into anyone having problems with these.  So I am not sure these
> are worth fixing, but it doesn't hurt to at least document these
> failures in the same test file that is concerned with duplicate tree
> entries.
> [...]
> +test_expect_failure 'fast-forward from duplicate entries to non-duplicate' '
> +	git merge update
> +'
> +
>  test_done

Per https://lore.kernel.org/git/[email-masked]/
isn't the noise of having a segfault from "git" worth fixing in itself
though? I.e. something like this, so we at least se why it fails:

diff --git a/t/t4058-diff-duplicates.sh b/t/t4058-diff-duplicates.sh
index 54614b814db..ed91d9f7fe9 100755
--- a/t/t4058-diff-duplicates.sh
+++ b/t/t4058-diff-duplicates.sh
@@ -182,8 +182,10 @@ test_expect_success 'switch to base branch and force status to be clean' '
 	test_must_be_empty actual
 '
 
-test_expect_failure 'fast-forward from duplicate entries to non-duplicate' '
-	git merge update
+test_expect_success 'fast-forward from duplicate entries to non-duplicate' '
+	! git merge update 2>err &&
+	grep "^BUG: " err &&
+	grep -F "should have entry at o->src_index->cache[1]" err
 '
 
 test_done
diff --git a/unpack-trees.c b/unpack-trees.c
index 8a1afbc1e49..230cb073fe1 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -789,8 +789,11 @@ static int traverse_by_cache_tree(int pos, int nr_entries, int nr_names,
 	 */
 	for (i = 0; i < nr_entries; i++) {
 		int new_ce_len, len, rc;
+		int j = pos + i;
 
-		src[0] = o->src_index->cache[pos + i];
+		src[0] = o->src_index->cache[j];
+		if (!src[0])
+			BUG("should have entry at o->src_index->cache[%d]", j);
 
 		len = ce_namelen(src[0]);
 		new_ce_len = cache_entry_size(len);