Code review fix re. max string length in bidi-trie

Related commit:
- fb4e94f92c

A bidi-trie can't store strings longer than 255 characters
because the string segment lengths are encoded into a single
byte. This commit ensures only strings smaller than
256 characters are stored in the bidi-tries.
This commit is contained in:
Raymond Hill 2019-08-23 11:30:10 -04:00
parent 432aed493e
commit 9f7e385a5c
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 24 additions and 17 deletions

View File

@ -333,7 +333,9 @@ const FilterPlain = class {
} }
addToTrie(trie) { addToTrie(trie) {
if ( this.s.length > 255 ) { return false; }
trie.add(this.s, this.tokenBeg); trie.add(this.s, this.tokenBeg);
return true;
} }
static compile(details) { static compile(details) {
@ -351,7 +353,9 @@ const FilterPlain = class {
} }
static addToTrie(args, trie) { static addToTrie(args, trie) {
if ( args[1].length > 255 ) { return false; }
trie.add(args[1], args[2]); trie.add(args[1], args[2]);
return true;
} }
}; };
@ -547,7 +551,9 @@ const FilterPlainHnAnchored = class {
} }
addToTrie(trie) { addToTrie(trie) {
if ( this.s.length > 255 ) { return false; }
trie.add(this.s, this.tokenBeg); trie.add(this.s, this.tokenBeg);
return true;
} }
static compile(details) { static compile(details) {
@ -562,7 +568,9 @@ const FilterPlainHnAnchored = class {
} }
static addToTrie(args, trie) { static addToTrie(args, trie) {
if ( args[1].length > 255 ) { return false; }
trie.add(args[1], args[2]); trie.add(args[1], args[2]);
return true;
} }
}; };
@ -1644,25 +1652,24 @@ const FilterBucket = class {
const fclass = filterClasses[fdata[0]]; const fclass = filterClasses[fdata[0]];
if ( fclass.trieableId === 0 ) { if ( fclass.trieableId === 0 ) {
if ( this.plainTrie !== null ) { if ( this.plainTrie !== null ) {
return fclass.addToTrie(fdata, this.plainTrie); if ( fclass.addToTrie(fdata, this.plainTrie) ) { return; }
} } else if ( this.plainCount < 3 ) {
if ( this.plainCount === 3 ) { this.plainCount += 1;
} else {
this.plainTrie = FilterBucket.trieContainer.createOne(); this.plainTrie = FilterBucket.trieContainer.createOne();
this._transferTrieable(0, this.plainTrie); this._transferTrieable(0, this.plainTrie);
return fclass.addToTrie(fdata, this.plainTrie); if ( fclass.addToTrie(fdata, this.plainTrie) ) { return; }
} }
this.plainCount += 1; } else if ( fclass.trieableId === 1 ) {
}
if ( fclass.trieableId === 1 ) {
if ( this.plainHnAnchoredTrie !== null ) { if ( this.plainHnAnchoredTrie !== null ) {
return fclass.addToTrie(fdata, this.plainHnAnchoredTrie); if ( fclass.addToTrie(fdata, this.plainHnAnchoredTrie) ) { return; }
} } else if ( this.plainHnAnchoredCount < 3 ) {
if ( this.plainHnAnchoredCount === 3 ) { this.plainHnAnchoredCount += 1;
} else {
this.plainHnAnchoredTrie = FilterBucket.trieContainer.createOne(); this.plainHnAnchoredTrie = FilterBucket.trieContainer.createOne();
this._transferTrieable(1, this.plainHnAnchoredTrie); this._transferTrieable(1, this.plainHnAnchoredTrie);
return fclass.addToTrie(fdata, this.plainHnAnchoredTrie); if ( fclass.addToTrie(fdata, this.plainHnAnchoredTrie) ) { return; }
} }
this.plainHnAnchoredCount += 1;
} }
this.filters.push(filterFromCompiledData(fdata)); this.filters.push(filterFromCompiledData(fdata));
} }
@ -1736,8 +1743,8 @@ const FilterBucket = class {
let i = filters.length; let i = filters.length;
while ( i-- ) { while ( i-- ) {
const f = filters[i]; const f = filters[i];
if ( f.trieableId !== trieableId || f.s.length > 255 ) { continue; } if ( f.trieableId !== trieableId ) { continue; }
f.addToTrie(trie); if ( f.addToTrie(trie) === false ) { continue; }
filters.splice(i, 1); filters.splice(i, 1);
} }
} }
@ -1764,7 +1771,7 @@ const FilterBucket = class {
} }
static optimize() { static optimize() {
const trieDetails = this.trieContainer.optimize(); const trieDetails = FilterBucket.trieContainer.optimize();
vAPI.localStorage.setItem( vAPI.localStorage.setItem(
'FilterBucket.trieDetails', 'FilterBucket.trieDetails',
JSON.stringify(trieDetails) JSON.stringify(trieDetails)

View File

@ -244,9 +244,9 @@ const SEGMENT_INFO = 2;
// grow buffer if needed // grow buffer if needed
if ( if (
(this.buf32[CHAR0_SLOT] - this.buf32[TRIE1_SLOT]) < MIN_FREE_CELL_BYTE_LENGTH || (this.buf32[CHAR0_SLOT] - this.buf32[TRIE1_SLOT]) < MIN_FREE_CELL_BYTE_LENGTH ||
(this.buf.length - this.buf32[CHAR1_SLOT]) < aR (this.buf.length - this.buf32[CHAR1_SLOT]) < 256
) { ) {
this.growBuf(MIN_FREE_CELL_BYTE_LENGTH, aR); this.growBuf(MIN_FREE_CELL_BYTE_LENGTH, 256);
} }
const buf32 = this.buf32; const buf32 = this.buf32;
let icell = iroot; let icell = iroot;