Skip to content

Commit ae76814

Browse files
committed
Merge branch 'jk/xdiff-clamp-funcname-context-index' into maint
The internal diff machinery can be made to read out of bounds while looking for --funcion-context line in a corner case, which has been corrected. * jk/xdiff-clamp-funcname-context-index: xdiff: clamp function context indices in post-image
2 parents 4d8ec15 + b777f3f commit ae76814

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

t/t4015-diff-whitespace.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,4 +2008,26 @@ test_expect_success 'compare mixed whitespace delta across moved blocks' '
20082008
test_cmp expected actual
20092009
'
20102010

2011+
# Note that the "6" in the expected hunk header below is funny, since we only
2012+
# show 5 lines (the missing one was blank and thus ignored). This is how
2013+
# --ignore-blank-lines behaves even without --function-context, and this test
2014+
# is just checking the interaction of the two features. Don't take it as an
2015+
# endorsement of that output.
2016+
test_expect_success 'combine --ignore-blank-lines with --function-context' '
2017+
test_write_lines 1 "" 2 3 4 5 >a &&
2018+
test_write_lines 1 2 3 4 >b &&
2019+
test_must_fail git diff --no-index \
2020+
--ignore-blank-lines --function-context a b >actual.raw &&
2021+
sed -n "/@@/,\$p" <actual.raw >actual &&
2022+
cat <<-\EOF >expect &&
2023+
@@ -1,6 +1,4 @@
2024+
1
2025+
2
2026+
3
2027+
4
2028+
-5
2029+
EOF
2030+
test_cmp expect actual
2031+
'
2032+
20112033
test_done

xdiff/xemit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
210210
if (fs1 < 0)
211211
fs1 = 0;
212212
if (fs1 < s1) {
213-
s2 -= s1 - fs1;
213+
s2 = XDL_MAX(s2 - (s1 - fs1), 0);
214214
s1 = fs1;
215215
}
216216
}
@@ -232,7 +232,7 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
232232
if (fe1 < 0)
233233
fe1 = xe->xdf1.nrec;
234234
if (fe1 > e1) {
235-
e2 += fe1 - e1;
235+
e2 = XDL_MIN(e2 + (fe1 - e1), xe->xdf2.nrec);
236236
e1 = fe1;
237237
}
238238

0 commit comments

Comments
 (0)