Skip to content

Commit 98e06c8

Browse files
committed
diff: avoid segfault with freed entries
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>
1 parent 9a2fb14 commit 98e06c8

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

diff.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7046,6 +7046,7 @@ static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
70467046
if (!diffopt->flags.no_index)
70477047
diffopt->skip_stat_unmatch++;
70487048
diff_free_filepair(p);
7049+
q->queue[i] = NULL;
70497050
}
70507051
}
70517052
free(q->queue);
@@ -7089,6 +7090,10 @@ void diff_queued_diff_prefetch(void *repository)
70897090

70907091
for (i = 0; i < q->nr; i++) {
70917092
struct diff_filepair *p = q->queue[i];
7093+
7094+
if (!p)
7095+
continue;
7096+
70927097
diff_add_if_missing(repo, &to_fetch, p->one);
70937098
diff_add_if_missing(repo, &to_fetch, p->two);
70947099
}

t/t4067-diff-partial-clone.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,41 @@ test_expect_success 'diff with rename detection batches blobs' '
132132
test_line_count = 1 done_lines
133133
'
134134

135+
test_expect_success 'diff succeeds even if entries are removed from queue' '
136+
test_when_finished "rm -rf server client trace" &&
137+
138+
test_create_repo server &&
139+
for l in a c e g i p
140+
do
141+
echo $l >server/$l &&
142+
git -C server add $l || return 1
143+
done &&
144+
git -C server commit -m x &&
145+
146+
for l in a e i
147+
do
148+
git -C server rm $l || return 1
149+
done &&
150+
151+
for l in b d f i
152+
do
153+
echo $l$l >server/$l &&
154+
git -C server add $l || return 1
155+
done &&
156+
git -C server commit -a -m x &&
157+
158+
test_config -C server uploadpack.allowfilter 1 &&
159+
test_config -C server uploadpack.allowanysha1inwant 1 &&
160+
git clone --filter=blob:limit=0 "file://$(pwd)/server" client &&
161+
162+
for file in $(ls client)
163+
do
164+
cat client/$file >$file &&
165+
mv $file client/$file || return 1
166+
done &&
167+
git -C client diff --name-only --relative HEAD^
168+
'
169+
135170
test_expect_success 'diff does not fetch anything if inexact rename detection is not needed' '
136171
test_when_finished "rm -rf server client trace" &&
137172

0 commit comments

Comments
 (0)