Skip to content

Commit 6767149

Browse files
committed
Merge branch 'rs/xdiff-context-length-fix'
The xdiff code on 32-bit platform misbehaved when an insanely large context size is given, which has been corrected. * rs/xdiff-context-length-fix: xdiff: avoid arithmetic overflow in xdl_get_hunk()
2 parents f76fe4a + d39e28e commit 6767149

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

t/t4055-diff-context.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,14 @@ test_expect_success '-U0 is valid, so is diff.context=0' '
8989
grep "^+MODIFIED" output
9090
'
9191

92+
test_expect_success '-U2147483647 works' '
93+
echo APPENDED >>x &&
94+
test_line_count = 16 x &&
95+
git diff -U2147483647 >output &&
96+
test_line_count = 22 output &&
97+
grep "^-ADDED" output &&
98+
grep "^+MODIFIED" output &&
99+
grep "^+APPENDED" output
100+
'
101+
92102
test_done

xdiff/xemit.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t *
4343
return 0;
4444
}
4545

46+
static long saturating_add(long a, long b)
47+
{
48+
return signed_add_overflows(a, b) ? LONG_MAX : a + b;
49+
}
4650

4751
/*
4852
* Starting at the passed change atom, find the latest change atom to be included
@@ -52,7 +56,9 @@ static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t *
5256
xdchange_t *xdl_get_hunk(xdchange_t **xscr, xdemitconf_t const *xecfg)
5357
{
5458
xdchange_t *xch, *xchp, *lxch;
55-
long max_common = 2 * xecfg->ctxlen + xecfg->interhunkctxlen;
59+
long max_common = saturating_add(saturating_add(xecfg->ctxlen,
60+
xecfg->ctxlen),
61+
xecfg->interhunkctxlen);
5662
long max_ignorable = xecfg->ctxlen;
5763
long ignored = 0; /* number of ignored blank lines */
5864

0 commit comments

Comments
 (0)