Skip to content

Commit

Permalink
Code review fix re. max string length in bidi-trie
Browse files Browse the repository at this point in the history
Related commit:
- fb4e94f

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.
  • Loading branch information
gorhill committed Aug 23, 2019
1 parent 432aed4 commit 9f7e385
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
37 changes: 22 additions & 15 deletions src/js/static-net-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,9 @@ const FilterPlain = class {
}

addToTrie(trie) {
if ( this.s.length > 255 ) { return false; }
trie.add(this.s, this.tokenBeg);
return true;
}

static compile(details) {
Expand All @@ -351,7 +353,9 @@ const FilterPlain = class {
}

static addToTrie(args, trie) {
if ( args[1].length > 255 ) { return false; }
trie.add(args[1], args[2]);
return true;
}
};

Expand Down Expand Up @@ -547,7 +551,9 @@ const FilterPlainHnAnchored = class {
}

addToTrie(trie) {
if ( this.s.length > 255 ) { return false; }
trie.add(this.s, this.tokenBeg);
return true;
}

static compile(details) {
Expand All @@ -562,7 +568,9 @@ const FilterPlainHnAnchored = class {
}

static addToTrie(args, trie) {
if ( args[1].length > 255 ) { return false; }
trie.add(args[1], args[2]);
return true;
}
};

Expand Down Expand Up @@ -1644,25 +1652,24 @@ const FilterBucket = class {
const fclass = filterClasses[fdata[0]];
if ( fclass.trieableId === 0 ) {
if ( this.plainTrie !== null ) {
return fclass.addToTrie(fdata, this.plainTrie);
}
if ( this.plainCount === 3 ) {
if ( fclass.addToTrie(fdata, this.plainTrie) ) { return; }
} else if ( this.plainCount < 3 ) {
this.plainCount += 1;
} else {
this.plainTrie = FilterBucket.trieContainer.createOne();
this._transferTrieable(0, this.plainTrie);
return fclass.addToTrie(fdata, this.plainTrie);
if ( fclass.addToTrie(fdata, this.plainTrie) ) { return; }
}
this.plainCount += 1;
}
if ( fclass.trieableId === 1 ) {
} else if ( fclass.trieableId === 1 ) {
if ( this.plainHnAnchoredTrie !== null ) {
return fclass.addToTrie(fdata, this.plainHnAnchoredTrie);
}
if ( this.plainHnAnchoredCount === 3 ) {
if ( fclass.addToTrie(fdata, this.plainHnAnchoredTrie) ) { return; }
} else if ( this.plainHnAnchoredCount < 3 ) {
this.plainHnAnchoredCount += 1;
} else {
this.plainHnAnchoredTrie = FilterBucket.trieContainer.createOne();
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));
}
Expand Down Expand Up @@ -1736,8 +1743,8 @@ const FilterBucket = class {
let i = filters.length;
while ( i-- ) {
const f = filters[i];
if ( f.trieableId !== trieableId || f.s.length > 255 ) { continue; }
f.addToTrie(trie);
if ( f.trieableId !== trieableId ) { continue; }
if ( f.addToTrie(trie) === false ) { continue; }
filters.splice(i, 1);
}
}
Expand All @@ -1764,7 +1771,7 @@ const FilterBucket = class {
}

static optimize() {
const trieDetails = this.trieContainer.optimize();
const trieDetails = FilterBucket.trieContainer.optimize();
vAPI.localStorage.setItem(
'FilterBucket.trieDetails',
JSON.stringify(trieDetails)
Expand Down
4 changes: 2 additions & 2 deletions src/js/strie.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ const SEGMENT_INFO = 2;
// grow buffer if needed
if (
(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;
let icell = iroot;
Expand Down

0 comments on commit 9f7e385

Please sign in to comment.