Do not discard `!#else` block for unknown preprocessor tokens

Related issue:
https://github.com/uBlockOrigin/uBlock-issues/issues/3393
This commit is contained in:
Raymond Hill 2024-09-30 10:16:48 -04:00
parent 3d6984aeaf
commit 6cac645830
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 15 additions and 9 deletions

View File

@ -4430,14 +4430,14 @@ export const utils = (( ) => {
const parts = [ 0 ]; const parts = [ 0 ];
let discard = false; let discard = false;
const shouldDiscard = ( ) => stack.some(v => v); const shouldDiscard = ( ) => stack.some(v => v.known && v.discard);
const begif = (startDiscard, match) => { const begif = details => {
if ( discard === false && startDiscard ) { if ( discard === false && details.known && details.discard ) {
parts.push(match.index); parts.push(details.pos);
discard = true; discard = true;
} }
stack.push(startDiscard); stack.push(details);
}; };
const endif = match => { const endif = match => {
@ -4455,15 +4455,21 @@ export const utils = (( ) => {
switch ( match[1] ) { switch ( match[1] ) {
case 'if': { case 'if': {
const startDiscard = this.evaluateExpr(match[2].trim(), env) === false; const result = this.evaluateExpr(match[2].trim(), env);
begif(startDiscard, match); begif({
known: result !== undefined,
discard: result === false,
pos: match.index,
});
break; break;
} }
case 'else': { case 'else': {
if ( stack.length === 0 ) { break; } if ( stack.length === 0 ) { break; }
const startDiscard = stack[stack.length-1] === false; const details = stack[stack.length-1];
endif(match); endif(match);
begif(startDiscard, match); details.discard = details.discard === false;
details.pos = match.index;
begif(details);
break; break;
} }
case 'endif': { case 'endif': {