From fb4e94f92c11d8c465b423dae1a18a696efe10a8 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Thu, 22 Aug 2019 17:11:49 -0400 Subject: [PATCH] Fix spurious 256-char limit for filters stored in bidi-trie Plain filters can be any length, while the bidi-trie was assuming max length of 256. The origin of this error is from when the bidi-trie code was originally imported from the hntrie code as start to develop bidi-trie. The erroneous code could cause issue when the following conditions were met: - Plain filter longer than 256 characters - Free space in bidi-trie's character buffer was less than 256 bytes Likely a rare occurrence, but some filter lists do contains long plain filters, for example: https://gitlab.com/curben/urlhaus-filter/raw/master/urlhaus-filter.txt Related feedback: - https://www.reddit.com/r/uBlockOrigin/comments/cs1y26/ --- src/js/strie.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/js/strie.js b/src/js/strie.js index ac783711d..3d1824987 100644 --- a/src/js/strie.js +++ b/src/js/strie.js @@ -241,20 +241,20 @@ const SEGMENT_INFO = 2; add(iroot, a, i = 0) { const aR = a.length; if ( aR === 0 ) { return 0; } - let icell = iroot; - // special case: first node in trie - if ( this.buf32[icell+SEGMENT_INFO] === 0 ) { - this.buf32[icell+SEGMENT_INFO] = this.addSegment(a, i, aR); - return this.addLeft(icell, a, i); - } // 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]) < 256 + (this.buf.length - this.buf32[CHAR1_SLOT]) < aR ) { - this.growBuf(MIN_FREE_CELL_BYTE_LENGTH, 256); + this.growBuf(MIN_FREE_CELL_BYTE_LENGTH, aR); } const buf32 = this.buf32; + let icell = iroot; + // special case: first node in trie + if ( buf32[icell+SEGMENT_INFO] === 0 ) { + buf32[icell+SEGMENT_INFO] = this.addSegment(a, i, aR); + return this.addLeft(icell, a, i); + } const buf8 = this.buf; const char0 = buf32[CHAR0_SLOT]; let al = i;