patch 9.1.1963: diff: missing diff size limit for xdiff

Problem:  diff: missing diff size limit for xdiff
Solution: Impose file size limit for internal diff (xdiff)
          (Yee Cheng Chin).

Git imposes a hard cap on file size for content that it passes to xdiff
(added to Git in dcd1742e56e, defined in xdiff-interface.h), due to
integer overflow concerns in xdiff. Vim doesn't specify such a limit
right now, which means it's possible for a user to diff a large file
(1GB+) and trigger these overflow issues.

Add the same size limit (1GB minus 1MB) to Vim and simply throws an
error when Vim encounters files larger than said limit. For now, reuse
the same error message regarding internal diff failures. There is no
need to add the same limit for external diff as it's up to each tool to
error check their input to decide what is appropriate or not.

closes: #18891

Signed-off-by: Yee Cheng Chin <ychin.git@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Yee Cheng Chin
2025-12-09 13:05:17 +01:00
committed by Christian Brabandt
parent f958d35723
commit 4af6d9755c
3 changed files with 17 additions and 4 deletions

View File

@ -1,4 +1,4 @@
*options.txt* For Vim version 9.1. Last change: 2025 Nov 27
*options.txt* For Vim version 9.1. Last change: 2025 Dec 09
VIM REFERENCE MANUAL by Bram Moolenaar
@ -3202,9 +3202,10 @@ A jump table for the options with a short description can be found at |Q_op|.
internal Use the internal diff library. This is
ignored when 'diffexpr' is set. *E960*
When running out of memory when writing a
buffer this item will be ignored for diffs
involving that buffer. Set the 'verbose'
option to see when this happens.
buffer or the diff is larger than 1 GB this
item will be ignored for diffs involving that
buffer. Set the 'verbose' option to see when
this happens.
iwhite Ignore changes in amount of white space. Adds
the "-b" flag to the "diff" command if

View File

@ -52,6 +52,10 @@ static long diff_algorithm = XDF_INDENT_HEURISTIC;
#define LBUFLEN 50 // length of line in diff file
// Max file size xdiff is eqipped to deal with. The value (1GB - 1MB) comes
// from Git's implementation.
#define MAX_XDIFF_SIZE (1024L * 1024 * 1023)
static int diff_a_works = MAYBE; // TRUE when "diff -a" works, FALSE when it
// doesn't work, MAYBE when not checked yet
#if defined(MSWIN)
@ -1318,6 +1322,12 @@ diff_file_internal(diffio_T *diffio)
emit_cfg.hunk_func = xdiff_out_indices;
else
emit_cb.out_line = xdiff_out_unified;
if (diffio->dio_orig.din_mmfile.size > MAX_XDIFF_SIZE ||
diffio->dio_new.din_mmfile.size > MAX_XDIFF_SIZE)
{
emsg(_(e_problem_creating_internal_diff));
return FAIL;
}
if (xdl_diff(&diffio->dio_orig.din_mmfile,
&diffio->dio_new.din_mmfile,
&param, &emit_cfg, &emit_cb) < 0)

View File

@ -729,6 +729,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
1963,
/**/
1962,
/**/