From 6cac64583059b6abdbc35d310847c5e225e78ef3 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 30 Sep 2024 10:16:48 -0400 Subject: [PATCH] Do not discard `!#else` block for unknown preprocessor tokens Related issue: https://github.com/uBlockOrigin/uBlock-issues/issues/3393 --- src/js/static-filtering-parser.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/js/static-filtering-parser.js b/src/js/static-filtering-parser.js index db5b2bd13..11858c20b 100644 --- a/src/js/static-filtering-parser.js +++ b/src/js/static-filtering-parser.js @@ -4430,14 +4430,14 @@ export const utils = (( ) => { const parts = [ 0 ]; let discard = false; - const shouldDiscard = ( ) => stack.some(v => v); + const shouldDiscard = ( ) => stack.some(v => v.known && v.discard); - const begif = (startDiscard, match) => { - if ( discard === false && startDiscard ) { - parts.push(match.index); + const begif = details => { + if ( discard === false && details.known && details.discard ) { + parts.push(details.pos); discard = true; } - stack.push(startDiscard); + stack.push(details); }; const endif = match => { @@ -4455,15 +4455,21 @@ export const utils = (( ) => { switch ( match[1] ) { case 'if': { - const startDiscard = this.evaluateExpr(match[2].trim(), env) === false; - begif(startDiscard, match); + const result = this.evaluateExpr(match[2].trim(), env); + begif({ + known: result !== undefined, + discard: result === false, + pos: match.index, + }); break; } case 'else': { if ( stack.length === 0 ) { break; } - const startDiscard = stack[stack.length-1] === false; + const details = stack[stack.length-1]; endif(match); - begif(startDiscard, match); + details.discard = details.discard === false; + details.pos = match.index; + begif(details); break; } case 'endif': {