Fix block refactoring breaking return and foreach loop variable aliasing#255
Merged
Fix block refactoring breaking return and foreach loop variable aliasing#255
Conversation
…sing
Two bugs fixed:
1. ControlFlowDetectorVisitor: Mark `return` as unsafe control flow.
When large blocks are refactored into `sub { ... }->(@_)` to avoid
JVM "Method too large" errors, a `return` inside the block would
only return from the anonymous sub, not from the enclosing function.
This caused infinite loops in ExifTool's SetNewValue (Writer.pl)
where `return` inside a `while` loop was silently swallowed.
2. BytecodeInterpreter & EmitForeach: Reset loop variable register
after foreach loop exits. Previously the register still aliased
the last array element, so subsequent writes to the variable
would corrupt the source array's last element.
Generated with [Devin](https://cli.devin.ai/docs)
Co-Authored-By: Devin <noreply@cognition.ai>
Generated with [Devin](https://cli.devin.ai/docs) Co-Authored-By: Devin <noreply@cognition.ai>
Replace BytecodeCompiler ad-hoc variableScopes Stack + allDeclaredVariables HashMap with ScopedSymbolTable for proper scope-aware variable tracking. The root cause: allDeclaredVariables was a flat map that overwrote entries when the same variable name was declared in different scopes. eval $val would capture the register from the LAST declaration, not the one visible at the eval site. This broke ~30 ExifTool write tests. Key changes: - SymbolTable/ScopedSymbolTable: add addVariableWithIndex(), getVisibleVariableRegistry() - BytecodeCompiler: use symbolTable for variable tracking, add per-eval-site registry snapshots via evalSiteRegistries list - CompileOperator: snapshot visible variables at each EVAL_STRING emission - InterpretedCode: carry evalSiteRegistries for runtime lookup - SlowOpcodeHandler/EvalStringHandler: use site-specific registry with fallback to global registry for backward compatibility Generated with [Devin](https://cli.devin.ai/docs) Co-Authored-By: Devin <noreply@cognition.ai>
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
Fixes discovered while investigating ExifTool test failures:
Block refactoring breaking
return:ControlFlowDetectorVisitornow marksreturnas unsafe control flow, preventing large blocks containingreturnfrom being refactored intosub { ... }->(@_). This caused infinite loops in ExifTool'sSetNewValue(Writer.pl) wherereturninside awhileloop only returned from the anonymous wrapper sub, not the enclosing function.Foreach loop variable aliasing after exit: After a foreach loop exits, the loop variable register is reset to a fresh
RuntimeScalar()in both the bytecode interpreter (BytecodeInterpreter.java) and JVM emitter (EmitForeach.java). Previously the register still aliased the last array element, so subsequent writes to the variable would corrupt the source array's last element.Test plan
Generated with Devin