diff options
| author | Junio C Hamano <gitster@pobox.com> | 2025-03-29 16:39:10 +0900 |
|---|---|---|
| committer | Junio C Hamano <gitster@pobox.com> | 2025-03-29 16:39:10 +0900 |
| commit | 6767149eca352db846ae065fa67614a1b4bc45a6 (patch) | |
| tree | 06391cb7a25ad57afb17760c3610d93438ec7eb1 | |
| parent | Merge branch 'jk/use-wunreachable-code-for-devs' (diff) | |
| parent | xdiff: avoid arithmetic overflow in xdl_get_hunk() (diff) | |
| download | git-6767149eca352db846ae065fa67614a1b4bc45a6.tar.gz git-6767149eca352db846ae065fa67614a1b4bc45a6.zip | |
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()
| -rwxr-xr-x | t/t4055-diff-context.sh | 10 | ||||
| -rw-r--r-- | xdiff/xemit.c | 8 |
2 files changed, 17 insertions, 1 deletions
diff --git a/t/t4055-diff-context.sh b/t/t4055-diff-context.sh index f7ff234cf9..ec2804eea6 100755 --- a/t/t4055-diff-context.sh +++ b/t/t4055-diff-context.sh @@ -89,4 +89,14 @@ test_expect_success '-U0 is valid, so is diff.context=0' ' grep "^+MODIFIED" output ' +test_expect_success '-U2147483647 works' ' + echo APPENDED >>x && + test_line_count = 16 x && + git diff -U2147483647 >output && + test_line_count = 22 output && + grep "^-ADDED" output && + grep "^+MODIFIED" output && + grep "^+APPENDED" output +' + test_done diff --git a/xdiff/xemit.c b/xdiff/xemit.c index f8e3f25b03..1d40c9cb40 100644 --- a/xdiff/xemit.c +++ b/xdiff/xemit.c @@ -43,6 +43,10 @@ static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t * return 0; } +static long saturating_add(long a, long b) +{ + return signed_add_overflows(a, b) ? LONG_MAX : a + b; +} /* * 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 * xdchange_t *xdl_get_hunk(xdchange_t **xscr, xdemitconf_t const *xecfg) { xdchange_t *xch, *xchp, *lxch; - long max_common = 2 * xecfg->ctxlen + xecfg->interhunkctxlen; + long max_common = saturating_add(saturating_add(xecfg->ctxlen, + xecfg->ctxlen), + xecfg->interhunkctxlen); long max_ignorable = xecfg->ctxlen; long ignored = 0; /* number of ignored blank lines */ |
