Skip to content
Myr

Normalize char before pattern lookup (#4252)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

There is an edge-case in FuzzyMatchV1 during backward scan, related to normalization: if string is initially denormalized (e.g. Unicode symbol), backward scan will proceed further to the next char; however, when the score is computed, the string is normalized first, then scanned based on the pattern. This leads to accessing pattern index increment, which itself leads to out-of-bound index access, resulting in a panic. To illustrate the process, here's the sequence of operations when search is perfored: 1. during backward scan by "minim" pattern ``` xxxxx Minímal example ^^^^^^^^^^^^ |||||||||||| miniiiiiiiim <- compute score for this substring ``` 2. during compute score by "minim" pattern ``` Minímal exam minimal exam <- normalize chars before computing the score ^^^^^^ |||||| minim <- at this point the pattern is already fully scanned and index is out-of-the-bound ``` In this commit the char is normalized during backward scan, to detect properly the boundaries for the pattern.

authored by Alexei Șerșun · committed by GitHub · parent 1eafc4e · Browse files · 2 files+12 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+12 added−0 removed

  • other:+12 added−0 removed

2 files+12 added−0 removed

modified:src/algo/algo_test.go+9 added
198 unmodified lines
199 }200 bytes[math.MaxUint16] = 'z'201 assertMatch(t, FuzzyMatchV2, true, true, string(bytes), "zx", math.MaxUint16, math.MaxUint16+2, scoreMatch*2+bonusConsecutive)202}203204func TestLongStringWithNormalize(t *testing.T) {205 bytes := make([]byte, 30000)206 for i := range bytes {207 bytes[i] = 'x'208 }209 unicodeString := string(bytes) + " Minímal example"210 assertMatch2(t, FuzzyMatchV1, false, true, false, unicodeString, "minim", 30001, 30006, 140)211}
modified:src/algo/algo.go+3 added
765 unmodified lines
766 } else if char > unicode.MaxASCII {767 char = unicode.To(unicode.LowerCase, char)768 }769 }770 if normalize {771 char = normalizeRune(char)772 }773774 pidx_ := indexAt(pidx, lenPattern, forward)775 pchar := pattern[pidx_]776 if char == pchar {
247 unmodified lines