fix: scope leakage in getOutVarNames for lambda, try-catch, and traditional for-loop - #468
Open
chenjunwenhao wants to merge 1 commit into
Open
Conversation
…tional for-loop Three scope management bugs in ScopeStackVisitor caused incorrect results from getOutVarNames(): 1. Lambda parameters leaked into the outer scope (both formal params and single-param shorthand), causing false negatives and false positives in external variable detection. 2. Catch exception variables (e.g. 'e' in 'catch(e)') were never registered in the catch block scope, causing false positives. 3. Traditional for-loop variables (e.g. 'i' in 'for(int i = 0; ...)') leaked into the outer scope, causing false negatives. All three fixes follow the push/pop scope pattern established by the for-each fix in PR alibaba#463, and are applied in the base ScopeStackVisitor class so both OutVarNamesVisitor and OutVarAttrsVisitor benefit. Co-authored-by: QoderWork
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
(a, b) ->formal params andx ->single-param shorthand) were not scoped to the lambda body, causing them to pollute the outer scope. This led to false negatives (externalamasked by leaked lambda param) and false positives (single-paramxnever registered, flagged as external).eincatch(e) { ... }) was never added to the catch block's scope, so it was incorrectly reported as an external variable.iinfor(int i = 0; i < n; i++)) leaked into the outer scope, causing false negatives wheniwas used after the loop.Root Cause
ScopeStackVisitor(the base class forOutVarNamesVisitorandOutVarAttrsVisitor) was missing scope push/pop for lambda expressions and traditional for-loops, and the existingvisitTryCatchpushed a scope but never registered the catch variable name.Fix
All three fixes are applied in
ScopeStackVisitor(the base class) following the push/pop pattern from PR #463, so bothOutVarNamesVisitorandOutVarAttrsVisitorbenefit:visitLambdaExpr: Push scope → register params (handling bothvarIdandFormalOrInferredParameterList) → visit body → pop scopevisitTryCatch: Push scope → register catch variable → visit catch body → pop scopevisitTraditionalForStatement: Push scope → visit init/condition/update/body → pop scopevisitForEachStatement: Moved fromOutVarNamesVisitorto the base class (same logic)Tests
7 new test cases added in
Express4RunnerTest:getOutVarNamesLambdaFormalParamsTest- formal params(a, b)scoped correctlygetOutVarNamesLambdaSingleParamTest- single paramx ->scoped correctlygetOutVarNamesLambdaParamNotLeakTest- lambda param doesn't leak to outer scopegetOutVarNamesCatchVariableTest- catch variableenot reported as externalgetOutVarNamesCatchVariableMultipleCatchesTest- multiple catch blocksgetOutVarNamesTraditionalForLoopTest- loop variableiscoped correctlygetOutVarNamesTraditionalForLoopVarNotLeakTest- loop variable doesn't leak after loopAll existing
getOutVarNames*andgetOutVarAttrs*tests continue to pass.