fix: walk result expression value to prevent false positive in UnusedVariables#247
Open
frankentini wants to merge 1 commit intotrailofbits:masterfrom
Open
Conversation
…Variables The unused_assignments() method broke out of its loop when encountering the 'result = ...' assignment without first walking its value for variable references. This caused variables that were only referenced in the final result expression (e.g. via BINGET/memo sharing) to be incorrectly flagged as unused. Now the result assignment's value is walked before the break, properly recording any variables it references as used. Fixes trailofbits#226
|
|
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 #226.
unused_assignments()breaks out of its loop upon encountering theresult = ...assignment. However, it did so before walking the assignment's value for variable references. This caused any variable only referenced within the final result expression (e.g. via BINGET/memo-based sharing, or as part of a complex result tuple/dict) to be incorrectly flagged as unused.Root Cause
In
Interpreter.unused_assignments()(fickle.py), the loop processed statements sequentially:Variables referenced exclusively in the result expression were never added to the
usedset, so they appeared indefined - usedand were falsely reported as suspicious.Fix
Before breaking, walk the result assignment's value to record all variable references:
Test
Added
test_unused_variables_no_false_positive_for_result_refswhich crafts a minimal pickle where a REDUCE-created variable is referenced only in the result tuple via BINGET. Without the fix, this variable is falsely flagged; with the fix, it correctly reports zero unused variables.This directly reproduces the pattern seen in the scanpy pickle from #226, where variables created by
numpy.core.multiarray.scalarREDUCE calls end up referenced only within result-level structures.