From a36566b3486b30ab15e3336033ca3a4ce7bd5ed8 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Thu, 19 Mar 2020 13:16:41 -0400 Subject: [PATCH] Allow empty needle in BidiTrieContainer.lastIndexOf() Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/882 Related commit: - https://github.com/gorhill/uBlock/commit/7c0294bd5f54 The changes in the commit above have been reverted, and the new fix is to add the ability to handle an empty needle in BidiTrieContainer.lastIndexOf() -- in which case the method will return the end of the currently matched pattern. --- src/js/static-net-filtering.js | 14 -------------- src/js/strie.js | 1 + src/js/wasm/biditrie.wasm | Bin 981 -> 990 bytes src/js/wasm/biditrie.wat | 7 +++++++ 4 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/js/static-net-filtering.js b/src/js/static-net-filtering.js index f726881fa..57314b6af 100644 --- a/src/js/static-net-filtering.js +++ b/src/js/static-net-filtering.js @@ -2599,20 +2599,6 @@ const FilterParser = class { this.token = matches[0]; this.tokenHash = urlTokenizer.tokenHashFromString(this.token); this.tokenBeg = matches.index; - - // https://www.reddit.com/r/uBlockOrigin/comments/dpcvfx/ - // Since we found a valid token, we can get rid of trailing - // wildcards if any. - if ( this.firstWildcardPos !== -1 ) { - const lastCharPos = this.f.length - 1; - if ( this.firstWildcardPos === lastCharPos ) { - this.f = this.f.slice(0, -1); - this.firstWildcardPos = -1; - } else if ( this.secondWildcardPos === lastCharPos ) { - this.f = this.f.slice(0, -1); - this.secondWildcardPos = -1; - } - } } findGoodToken() { diff --git a/src/js/strie.js b/src/js/strie.js index b7c62f27e..c084eb1a3 100644 --- a/src/js/strie.js +++ b/src/js/strie.js @@ -666,6 +666,7 @@ const roundToPageSize = v => (v + PAGE_SIZE-1) & ~(PAGE_SIZE-1); // Find the right-most instance of substring in main string. // WASMable. lastIndexOf(haystackBeg, haystackEnd, needleLeft, needleLen) { + if ( needleLen === 0 ) { return haystackBeg; } let haystackLeft = haystackEnd - needleLen; if ( haystackLeft < haystackBeg ) { return -1; } needleLeft += this.buf32[CHAR0_SLOT]; diff --git a/src/js/wasm/biditrie.wasm b/src/js/wasm/biditrie.wasm index a5d3aef9eed6ceb3bdb4ea2fd728ab446eb9083b..e1de9312c791266c644351b10c0a287b8ea10b37 100644 GIT binary patch delta 30 mcmcc0evf@Z2ji8Eop+gd%Nbeg6_{OF926M%xhH2aX9EDLnh7ER delta 21 dcmcb|ewBSf2jl6Dop+g7@)%j`Cl@nk0{~;A2r2*o diff --git a/src/js/wasm/biditrie.wat b/src/js/wasm/biditrie.wat index 5f7ec5e46..3cd76a618 100644 --- a/src/js/wasm/biditrie.wat +++ b/src/js/wasm/biditrie.wat @@ -639,6 +639,13 @@ (local $i i32) (local $j i32) (local $c0 i32) + ;; if ( needleLen === 0 ) { return haystackBeg; } + get_local $needleLen + i32.eqz + if + get_local $haystackBeg + return + end block $fail block $succeed ;; let haystackLeft = haystackEnd - needleLen;