Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SymbolResolver incorrectly resolves unbound local variable #2035

Open
KuechA opened this issue Feb 7, 2025 · 1 comment
Open

SymbolResolver incorrectly resolves unbound local variable #2035

KuechA opened this issue Feb 7, 2025 · 1 comment

Comments

@KuechA
Copy link
Contributor

KuechA commented Feb 7, 2025

The SymbolResolver resolves local variables in the same scope even if the first assignment to this variable occurs after a usage. E.g. in python, this leads to an UnboundLocalError.

Example code causing this issue which is currently not implemented accordingly in the CPG (see https://github.com/Fraunhofer-AISEC/cpg/blob/ak/python-comprehension-scope/cpg-language-python/src/test/resources/python/comprehension.py#L70 and for the test see https://github.com/Fraunhofer-AISEC/cpg/blob/ak/python-comprehension-scope/cpg-language-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/CollectionComprehensionTest.kt#L1385):

def comprehension_with_list_assignment_and_index_variable_reversed():
    b = [0, 1, 2]
    a = 1
    [a for (b[a], a) in [('this', 0), ('is', 1), ('fun', 2)]] # This crashes because the "a" in the tuple shadows the outer variable and "UnboundLocalError: cannot access local variable 'a' where it is not associated with a value".
    print(b) # prints nothing due to crash

Note: This is not to be confused with a variable which has not been declared (yet) within the scope/context.

          I would say that `refersTo` should be null. I think we did that in other cases where we have an unbound error as well

Originally posted by @oxisto in #2019 (comment)

@KuechA
Copy link
Contributor Author

KuechA commented Feb 7, 2025

We should identify further cases which can cause such behavior and add respective test cases (I didn't find anything suitable in the python language module on a first glance)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant