]> git.ipfire.org Git - thirdparty/git.git/commit
xdiff: avoid arithmetic overflow in xdl_get_hunk()
authorRené Scharfe <l.s.r@web.de>
Fri, 14 Mar 2025 22:00:42 +0000 (23:00 +0100)
committerJunio C Hamano <gitster@pobox.com>
Fri, 14 Mar 2025 23:19:40 +0000 (16:19 -0700)
commitd39e28e68c2b1bba25c5b1213fded95e525db15e
tree6cd4551bd70709127ef6ac7f30508f673828c6b9
parent5c21db3a0d5f4414b65e114ca21c5a1fe736f2bc
xdiff: avoid arithmetic overflow in xdl_get_hunk()

xdl_get_hunk() calculates the maximum number of common lines between two
changes that would fit into the same hunk for the given context options.
It involves doubling and addition and thus can overflow if the terms are
huge.

The type of ctxlen and interhunkctxlen in xdemitconf_t is long, while
the type of the corresponding context and interhunkcontext in struct
diff_options is int.  On many platforms longs are bigger that ints,
which prevents the overflow.  On Windows they have the same range and
the overflow manifests as hunks that are split erroneously and lines
being repeated between them.

Fix the overflow by checking and not going beyond LONG_MAX.  This allows
specifying a huge context line count and getting all lines of a changed
files in a single hunk, as expected.

Reported-by: Jason Cho <jason11choca@proton.me>
Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/t4055-diff-context.sh
xdiff/xemit.c