diff mbox series

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

Message ID 8db27892c598a3976c0742e23563f1d360b8dee1.1607677728.git.gitgitgadget@gmail.com (mailing list archive)
State Accepted
Commit ac14de13b228285b798ed805812fe20d1bc55eb2
Headers show
Series diffcore-rename improvements | expand

Commit Message

Elijah Newren Dec. 11, 2020, 9:08 a.m. UTC
From: Elijah Newren <newren@gmail.com>

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.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 t/t4058-diff-duplicates.sh | 67 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)

Comments

Ævar Arnfjörð Bjarmason April 21, 2021, 12:29 p.m. UTC | #1
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/87lf9b3mth.fsf@evledraar.gmail.com/
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);
Elijah Newren April 21, 2021, 5:38 p.m. UTC | #2
On Wed, Apr 21, 2021 at 5:29 AM Ævar Arnfjörð Bjarmason
<avarab@gmail.com> wrote:
>
>
> 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/87lf9b3mth.fsf@evledraar.gmail.com/
> 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);
>

Seems reasonable to me.  Are you planning to add a commit message and
turn it into a proper patch?  If so, I'll give my Thumbs-up-by or
whatever we need.  :-)
diff mbox series

Patch

diff --git a/t/t4058-diff-duplicates.sh b/t/t4058-diff-duplicates.sh
index bd685089561..ad3f37d388d 100755
--- a/t/t4058-diff-duplicates.sh
+++ b/t/t4058-diff-duplicates.sh
@@ -119,4 +119,71 @@  test_expect_success 'diff-tree FROM duplicate tree, with renames' '
 	test_cmp expect actual
 '
 
+test_expect_success 'create a few commits' '
+	git commit-tree -m "Duplicate Entries" two^{tree} >commit_id &&
+	git branch base $(cat commit_id) &&
+
+	git commit-tree -p $(cat commit_id) -m "Just one" three^{tree} >up &&
+	git branch update $(cat up) &&
+
+	git commit-tree -p $(cat up) -m "Back to weird" two^{tree} >final &&
+	git branch final $(cat final) &&
+
+	rm commit_id up final
+'
+
+test_expect_failure 'git read-tree does not segfault' '
+	test_when_finished rm .git/index.lock &&
+	test_might_fail git read-tree --reset base
+'
+
+test_expect_failure 'reset --hard does not segfault' '
+	test_when_finished rm .git/index.lock &&
+	git checkout base &&
+	test_might_fail git reset --hard
+'
+
+test_expect_failure 'git diff HEAD does not segfault' '
+	git checkout base &&
+	GIT_TEST_CHECK_CACHE_TREE=false &&
+	git reset --hard &&
+	test_might_fail git diff HEAD
+'
+
+test_expect_failure 'can switch to another branch when status is empty' '
+	git clean -ffdqx &&
+	git status --porcelain -uno >actual &&
+	test_must_be_empty actual &&
+	git checkout update
+'
+
+test_expect_success 'forcibly switch to another branch, verify status empty' '
+	git checkout -f update &&
+	git status --porcelain -uno >actual &&
+	test_must_be_empty actual
+'
+
+test_expect_success 'fast-forward from non-duplicate entries to duplicate' '
+	git merge final
+'
+
+test_expect_failure 'clean status, switch branches, status still clean' '
+	git status --porcelain -uno >actual &&
+	test_must_be_empty actual &&
+	git checkout base &&
+	git status --porcelain -uno >actual &&
+	test_must_be_empty actual
+'
+
+test_expect_success 'switch to base branch and force status to be clean' '
+	git checkout base &&
+	GIT_TEST_CHECK_CACHE_TREE=false git reset --hard &&
+	git status --porcelain -uno >actual &&
+	test_must_be_empty actual
+'
+
+test_expect_failure 'fast-forward from duplicate entries to non-duplicate' '
+	git merge update
+'
+
 test_done