Problem: matchfuzzy() can crash on long multi-word patterns.
Solution: Clamp pat_chars to maxMatches and stop before calling
match_positions() when the buffer is full (glepnir).
closes: #20209
Signed-off-by: glepnir <glephunter@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
int complete = FALSE;
int score = 0;
int numMatches = 0;
+ int pat_chars = 0;
score_t fzy_score;
*outScore = 0;
complete = TRUE;
*p = NUL;
}
+ // match_positions() always writes pat_chars entries,
+ // so bail if they won't fit.
+ pat_chars = MB_CHARLEN(pat);
+ if (pat_chars > maxMatches)
+ pat_chars = maxMatches;
+ if (numMatches > maxMatches - pat_chars)
+ {
+ numMatches = 0;
+ *outScore = FUZZY_SCORE_NONE;
+ break;
+ }
score = FUZZY_SCORE_NONE;
if (has_match(pat, str))
else
*outScore += score;
- numMatches += MB_CHARLEN(pat);
+ numMatches += pat_chars;
if (complete || numMatches >= maxMatches)
break;
call StopVimInTerminal(buf)
endfunc
+func Test_matchfuzzy_long_multiword_no_overflow()
+ let word = repeat('a', 100)
+ let pat_ok = repeat(word . ' ', 9) . word
+ call assert_equal([word], matchfuzzy([word], pat_ok))
+
+ let pat_overflow = repeat(word . ' ', 14) . word
+ call assert_equal([[], [], []], matchfuzzypos([word], pat_overflow))
+endfunc
+
" vim: shiftwidth=2 sts=2 expandtab
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 490,
/**/
489,
/**/