]> git.ipfire.org Git - thirdparty/git.git/commitdiff
diff: avoid segfault with freed entries
authorDerrick Stolee <stolee@gmail.com>
Mon, 29 Dec 2025 21:44:57 +0000 (21:44 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 30 Dec 2025 01:53:47 +0000 (10:53 +0900)
When computing a diff in a partial clone, there is a chance that we
could trigger a prefetch of missing objects at the same time as we are
freeing entries from the global diff queue. This is difficult to
reproduce, as we need to have some objects be freed from the queue
before triggering the prefetch of missing objects. There is a new test
in t4067 that does trigger the segmentation fault that results in this
case.

The fix is to set the queue pointer to NULL after it is freed, and then
to be careful about NULL values in the prefetch.

The more elaborate explanation is that within diffcore_std(), we may
skip the initial prefetch due to the output format (--name-only in the
test) and go straight to diffcore_skip_stat_unmatch(). In that method,
the index entries that have been invalidated by path changes show up as
entries but may be deleted because they are not actually content diffs
and only newer timestamps than expected. As those entries are deleted,
later entries are checked with diff_filespec_check_stat_unmatch(), which
uses diff_queued_diff_prefetch() as the missing_object_cb in its diff
options. That can trigger downloading missing objects if the appropriate
scenario occurs to trigger a call to diff_popoulate_filespec(). It's
finally within that callback to diff_queued_diff_prefetch() that the
segfault occurs.

The test was hard to find because it required some real differences,
some not-different files that had a newer modified time, and the order
of those files alphabetically was important to trigger the deletion
before the prefetch was triggered.

I briefly considered a "lock" member for the diff queue, but it was a
much larger diff and introduced many more possible error scenarios.

Signed-off-by: Derrick Stolee <stolee@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff.c
t/t4067-diff-partial-clone.sh

diff --git a/diff.c b/diff.c
index 436da250eb150de214db09030f2d69db3065ed94..a68ddd2168ba1c7f949751f30347d75d5e913295 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -7098,6 +7098,7 @@ static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
                        if (!diffopt->flags.no_index)
                                diffopt->skip_stat_unmatch++;
                        diff_free_filepair(p);
+                       q->queue[i] = NULL;
                }
        }
        free(q->queue);
@@ -7141,6 +7142,10 @@ void diff_queued_diff_prefetch(void *repository)
 
        for (i = 0; i < q->nr; i++) {
                struct diff_filepair *p = q->queue[i];
+
+               if (!p)
+                       continue;
+
                diff_add_if_missing(repo, &to_fetch, p->one);
                diff_add_if_missing(repo, &to_fetch, p->two);
        }
index 581250dd2d227adfdc323bdc6211c3073665b259..72f25de44950aeabec6fe921e4c9542b678a1679 100755 (executable)
@@ -132,6 +132,41 @@ test_expect_success 'diff with rename detection batches blobs' '
        test_line_count = 1 done_lines
 '
 
+test_expect_success 'diff succeeds even if entries are removed from queue' '
+       test_when_finished "rm -rf server client trace" &&
+
+       test_create_repo server &&
+       for l in a c e g i p
+       do
+               echo $l >server/$l &&
+               git -C server add $l || return 1
+       done &&
+       git -C server commit -m x &&
+
+       for l in a e i
+       do
+               git -C server rm $l || return 1
+       done &&
+
+       for l in b d f i
+               do
+               echo $l$l >server/$l &&
+               git -C server add $l || return 1
+       done &&
+       git -C server commit -a -m x &&
+
+       test_config -C server uploadpack.allowfilter 1 &&
+       test_config -C server uploadpack.allowanysha1inwant 1 &&
+       git clone --filter=blob:limit=0 "file://$(pwd)/server" client &&
+
+       for file in $(ls client)
+       do
+               cat client/$file >$file &&
+               mv $file client/$file || return 1
+       done &&
+       git -C client diff --name-only --relative HEAD^
+'
+
 test_expect_success 'diff does not fetch anything if inexact rename detection is not needed' '
        test_when_finished "rm -rf server client trace" &&