Skip to content

Commit 9f7e385

Browse files
committed
Code review fix re. max string length in bidi-trie
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.
1 parent 432aed4 commit 9f7e385

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/js/static-net-filtering.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,9 @@ const FilterPlain = class {
333333
}
334334

335335
addToTrie(trie) {
336+
if ( this.s.length > 255 ) { return false; }
336337
trie.add(this.s, this.tokenBeg);
338+
return true;
337339
}
338340

339341
static compile(details) {
@@ -351,7 +353,9 @@ const FilterPlain = class {
351353
}
352354

353355
static addToTrie(args, trie) {
356+
if ( args[1].length > 255 ) { return false; }
354357
trie.add(args[1], args[2]);
358+
return true;
355359
}
356360
};
357361

@@ -547,7 +551,9 @@ const FilterPlainHnAnchored = class {
547551
}
548552

549553
addToTrie(trie) {
554+
if ( this.s.length > 255 ) { return false; }
550555
trie.add(this.s, this.tokenBeg);
556+
return true;
551557
}
552558

553559
static compile(details) {
@@ -562,7 +568,9 @@ const FilterPlainHnAnchored = class {
562568
}
563569

564570
static addToTrie(args, trie) {
571+
if ( args[1].length > 255 ) { return false; }
565572
trie.add(args[1], args[2]);
573+
return true;
566574
}
567575
};
568576

@@ -1644,25 +1652,24 @@ const FilterBucket = class {
16441652
const fclass = filterClasses[fdata[0]];
16451653
if ( fclass.trieableId === 0 ) {
16461654
if ( this.plainTrie !== null ) {
1647-
return fclass.addToTrie(fdata, this.plainTrie);
1648-
}
1649-
if ( this.plainCount === 3 ) {
1655+
if ( fclass.addToTrie(fdata, this.plainTrie) ) { return; }
1656+
} else if ( this.plainCount < 3 ) {
1657+
this.plainCount += 1;
1658+
} else {
16501659
this.plainTrie = FilterBucket.trieContainer.createOne();
16511660
this._transferTrieable(0, this.plainTrie);
1652-
return fclass.addToTrie(fdata, this.plainTrie);
1661+
if ( fclass.addToTrie(fdata, this.plainTrie) ) { return; }
16531662
}
1654-
this.plainCount += 1;
1655-
}
1656-
if ( fclass.trieableId === 1 ) {
1663+
} else if ( fclass.trieableId === 1 ) {
16571664
if ( this.plainHnAnchoredTrie !== null ) {
1658-
return fclass.addToTrie(fdata, this.plainHnAnchoredTrie);
1659-
}
1660-
if ( this.plainHnAnchoredCount === 3 ) {
1665+
if ( fclass.addToTrie(fdata, this.plainHnAnchoredTrie) ) { return; }
1666+
} else if ( this.plainHnAnchoredCount < 3 ) {
1667+
this.plainHnAnchoredCount += 1;
1668+
} else {
16611669
this.plainHnAnchoredTrie = FilterBucket.trieContainer.createOne();
16621670
this._transferTrieable(1, this.plainHnAnchoredTrie);
1663-
return fclass.addToTrie(fdata, this.plainHnAnchoredTrie);
1671+
if ( fclass.addToTrie(fdata, this.plainHnAnchoredTrie) ) { return; }
16641672
}
1665-
this.plainHnAnchoredCount += 1;
16661673
}
16671674
this.filters.push(filterFromCompiledData(fdata));
16681675
}
@@ -1736,8 +1743,8 @@ const FilterBucket = class {
17361743
let i = filters.length;
17371744
while ( i-- ) {
17381745
const f = filters[i];
1739-
if ( f.trieableId !== trieableId || f.s.length > 255 ) { continue; }
1740-
f.addToTrie(trie);
1746+
if ( f.trieableId !== trieableId ) { continue; }
1747+
if ( f.addToTrie(trie) === false ) { continue; }
17411748
filters.splice(i, 1);
17421749
}
17431750
}
@@ -1764,7 +1771,7 @@ const FilterBucket = class {
17641771
}
17651772

17661773
static optimize() {
1767-
const trieDetails = this.trieContainer.optimize();
1774+
const trieDetails = FilterBucket.trieContainer.optimize();
17681775
vAPI.localStorage.setItem(
17691776
'FilterBucket.trieDetails',
17701777
JSON.stringify(trieDetails)

src/js/strie.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ const SEGMENT_INFO = 2;
244244
// grow buffer if needed
245245
if (
246246
(this.buf32[CHAR0_SLOT] - this.buf32[TRIE1_SLOT]) < MIN_FREE_CELL_BYTE_LENGTH ||
247-
(this.buf.length - this.buf32[CHAR1_SLOT]) < aR
247+
(this.buf.length - this.buf32[CHAR1_SLOT]) < 256
248248
) {
249-
this.growBuf(MIN_FREE_CELL_BYTE_LENGTH, aR);
249+
this.growBuf(MIN_FREE_CELL_BYTE_LENGTH, 256);
250250
}
251251
const buf32 = this.buf32;
252252
let icell = iroot;

0 commit comments

Comments
 (0)