mirror of
https://github.com/vim/vim.git
synced 2026-06-10 15:37:26 +02:00
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 <glephunter@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
committed by
Christian Brabandt
parent
5e22756b03
commit
52003f7fc1
+4
-6
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -729,6 +729,8 @@ static char *(features[]) =
|
||||
|
||||
static int included_patches[] =
|
||||
{ /* Add new patch number below this line */
|
||||
/**/
|
||||
601,
|
||||
/**/
|
||||
600,
|
||||
/**/
|
||||
|
||||
Reference in New Issue
Block a user