From 4af6d9755cba0dd07e881172f2a6e0efe9986ddc Mon Sep 17 00:00:00 2001 From: Yee Cheng Chin Date: Tue, 9 Dec 2025 13:05:17 +0100 Subject: [PATCH] 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 Signed-off-by: Christian Brabandt --- runtime/doc/options.txt | 9 +++++---- src/diff.c | 10 ++++++++++ src/version.c | 2 ++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index 03c0c547bb..d7adf555ab 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -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 diff --git a/src/diff.c b/src/diff.c index c15936b126..5f5376133e 100644 --- a/src/diff.c +++ b/src/diff.c @@ -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, ¶m, &emit_cfg, &emit_cb) < 0) diff --git a/src/version.c b/src/version.c index e9ecbef67f..6e48a748db 100644 --- a/src/version.c +++ b/src/version.c @@ -729,6 +729,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ +/**/ + 1963, /**/ 1962, /**/ -- 2.47.3