Skip to content

Commit

Permalink
False positive matches when pattern.length > 32 #136 #254 bugfix (#333)
Browse files Browse the repository at this point in the history
* Initial tests (that fail) for #136

* Fix for maximum integer overflow in signed 32 bit number by applying ceiling. Also tests. Issue #136
  • Loading branch information
ErikLarsson82 authored and krisk committed Oct 31, 2019
1 parent 0aa34e0 commit b1fe7cc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/bitap/bitap_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports = (text, pattern, patternAlphabet, { location = 0, distance = 100
let finalScore = 1
let binMax = patternLen + textLen

const mask = 1 << (patternLen - 1)
const mask = 1 << (patternLen <= 31 ? patternLen - 1 : 30)

for (let i = 0; i < patternLen; i += 1) {
// Scan for the best match; each iteration allows for one more error.
Expand Down Expand Up @@ -135,7 +135,7 @@ module.exports = (text, pattern, patternAlphabet, { location = 0, distance = 100
distance
})

// console.log('score', score, finalScore)
//console.log('score', score, finalScore)

if (score > currentThreshold) {
break
Expand All @@ -144,7 +144,7 @@ module.exports = (text, pattern, patternAlphabet, { location = 0, distance = 100
lastBitArr = bitArr
}

// console.log('FINAL SCORE', finalScore)
//console.log('FINAL SCORE', finalScore)

// Count exact matches (those with a score of 0) to be "almost" exact
return {
Expand Down
43 changes: 43 additions & 0 deletions test/fuse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -989,3 +989,46 @@ describe('Searching through a deeply nested object', () => {
})
})
})

describe('Searching using string large strings', () => {

const list = [{
text: 'pizza'
}, {
text: 'feast'
}, {
text: 'super+large+much+unique+36+very+wow+'
}]
const options = {
include: ['score', 'matches'],
shouldSort: true,
threshold: 0.5,
location: 0,
distance: 0,
maxPatternLength: 50,
minMatchCharLength: 4,
keys: [
'text'
]
}
const fuse = new Fuse(list, options)

test('finds delicious pizza', () => {
expect(fuse.search('pizza')[0].text).toBe('pizza')
})
test('finds pizza when clumbsy', () => {
expect(fuse.search('pizze')[0].text).toBe('pizza')
})
test('finds no matches when string is exactly 31 characters', () => {
expect(fuse.search('this-string-is-exactly-31-chars')).toStrictEqual([])
})
test('finds no matches when string is exactly 32 characters', () => {
expect(fuse.search('this-string-is-exactly-32-chars-')).toStrictEqual([])
})
test('finds no matches when string is larger than 32 characters', () => {
expect(fuse.search('this-string-is-more-than-32-chars')).toStrictEqual([])
})
test('should find one match that is larger than 32 characters', () => {
expect(fuse.search('super+large+much+unique+36+very+wow+')[0].text).toBe('super+large+much+unique+36+very+wow+')
})
})

0 comments on commit b1fe7cc

Please sign in to comment.