patch 9.2.0601: matchfuzzypos() returns garbage positions for long candidates
Commit:
https://github.com/vim/vim/commit/52003f7fc11e8fffa4cc8b0969282224a8b9a52e
Author: glepnir <
gleph...@gmail.com>
Date: Sun Jun 7 18:42:50 2026 +0000
patch 9.2.0601: matchfuzzypos() returns garbage positions for long candidates
Problem: A needle that only matches past char 1024 gives an INT_MIN + 1
score with unset positions, e.g.
matchfuzzypos([repeat('a',1024)..'z'], 'az').
Solution: Drop the candidate when match_positions() returns SCORE_MIN.
closes: #20435
Signed-off-by: glepnir <
gleph...@gmail.com>
Signed-off-by: Christian Brabandt <
c...@256bit.org>
diff --git a/src/fuzzy.c b/src/fuzzy.c
index c8709c107..fddfeb450 100644
--- a/src/fuzzy.c
+++ b/src/fuzzy.c
@@ -135,10 +135,10 @@ fuzzy_match(
if (has_match(pat, str))
{
fzy_score = match_positions(pat, str, matches + numMatches);
- score = (fzy_score == SCORE_MIN) ? INT_MIN + 1
- : (fzy_score == SCORE_MAX) ? INT_MAX
- : (fzy_score < 0) ? (int)ceil(fzy_score * SCORE_SCALE - 0.5)
- : (int)floor(fzy_score * SCORE_SCALE + 0.5);
+ if (fzy_score != SCORE_MIN)
+ score = (fzy_score == SCORE_MAX) ? INT_MAX
+ : (fzy_score < 0) ? (int)ceil(fzy_score * SCORE_SCALE - 0.5)
+ : (int)floor(fzy_score * SCORE_SCALE + 0.5);
}
if (score == FUZZY_SCORE_NONE)
@@ -1137,8 +1137,6 @@ match_positions(char_u *needle, char_u *haystack, int_u *positions)
if (m > MATCH_MAX_LEN || n > m)
{
// Unreasonably large candidate: return no score
- // If it is a valid match it will still be returned, it will
- // just be ranked below any reasonably sized candidates
return SCORE_MIN;
}
else if (n == m)
diff --git a/src/testdir/test_matchfuzzy.vim b/src/testdir/test_matchfuzzy.vim
index dfeb074d7..8e1c2f882 100644
--- a/src/testdir/test_matchfuzzy.vim
+++ b/src/testdir/test_matchfuzzy.vim
@@ -331,4 +331,31 @@ func Test_matchfuzzy_long_multiword_no_overflow()
call assert_equal([[], [], []], matchfuzzypos([word], pat_overflow))
endfunc
+func Test_matchfuzzy_long_candidate()
+ let str = repeat('a', 1024) .. 'z'
+ call assert_equal([], matchfuzzy([str], 'az'))
+ call assert_equal([[], [], []], matchfuzzypos([str], 'az'))
+
+ call assert_equal([str], matchfuzzy([str], 'a'))
+ let r = matchfuzzypos([str], 'a')
+ call assert_equal([str], r[0])
+ call assert_equal([0], r[1][0])
+
+ let edge = 'a' .. repeat('x', 1022) .. 'a'
+ call assert_equal([edge], matchfuzzy([edge], 'aa'))
+ let e = matchfuzzypos([edge], 'aa')
+ call assert_equal([edge], e[0])
+ call assert_equal([0, 1023], e[1][0])
+endfunc
+
+func Test_matchfuzzy_long_candidate_mbyte()
+ let ok = repeat('好', 1023) .. '界'
+ call assert_equal([ok], matchfuzzy([ok], '好界'))
+ call assert_notequal([], matchfuzzypos([ok], '好界')[0])
+
+ let toolong = repeat('好', 1024) .. '界'
+ call assert_equal([], matchfuzzy([toolong], '好界'))
+ call assert_equal([[], [], []], matchfuzzypos([toolong], '好界'))
+endfunc
+
" vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/version.c b/src/version.c
index a58178aed..9d7170c20 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 */
+/**/
+ 601,
/**/
600,
/**/