Skip to content
New issue

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

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

Already on GitHub? # to your account

Python incorrectly resolves fields in unqualified lookup #1983

Closed
oxisto opened this issue Jan 24, 2025 · 0 comments · Fixed by #1984
Closed

Python incorrectly resolves fields in unqualified lookup #1983

oxisto opened this issue Jan 24, 2025 · 0 comments · Fixed by #1984
Assignees

Comments

@oxisto
Copy link
Member

oxisto commented Jan 24, 2025

Currently our python frontend (and probably others?) resolve fields when doing an unqualified lookup, i.e., just looking for symbol (but not self.symbol). But that is not allowed in Python (as well as in Go actually) and this leads to horrendous wrong results.

Consider the following snippet:

a = "Hello"

class MyClass:
    def foo(self):
        self.a = 1
        print(a)

The a in the print statement should resolve to the global a. Instead it currently resolves to the field declaration of a, which is only accessible through self.a.

This also applies to imports as well

import os

class MyClass:
    def bar(self):
        self.os = 1
        print(os.name)

Here, os is also wrongfully resolved to MyClass.os.

This might be a bigger issue and effecting all language frontends that require some sort of "qualified" lookup.

oxisto added a commit that referenced this issue Jan 25, 2025
oxisto added a commit that referenced this issue Jan 25, 2025
oxisto added a commit that referenced this issue Jan 25, 2025
oxisto added a commit that referenced this issue Jan 26, 2025
oxisto added a commit that referenced this issue Jan 26, 2025
oxisto added a commit that referenced this issue Jan 26, 2025
oxisto added a commit that referenced this issue Jan 26, 2025
@oxisto oxisto changed the title Python resolves fields in unqualified lookup Python incorrectly resolves fields in unqualified lookup Jan 26, 2025
oxisto added a commit that referenced this issue Jan 26, 2025
oxisto added a commit that referenced this issue Jan 26, 2025
oxisto added a commit that referenced this issue Jan 28, 2025
oxisto added a commit that referenced this issue Jan 28, 2025
oxisto added a commit that referenced this issue Jan 28, 2025
oxisto added a commit that referenced this issue Jan 28, 2025
oxisto added a commit that referenced this issue Jan 28, 2025
@oxisto oxisto self-assigned this Jan 28, 2025
oxisto added a commit that referenced this issue Jan 28, 2025
oxisto added a commit that referenced this issue Jan 29, 2025
oxisto added a commit that referenced this issue Jan 29, 2025
oxisto added a commit that referenced this issue Jan 29, 2025
@oxisto oxisto closed this as completed in c72becd Jan 29, 2025
oxisto added a commit that referenced this issue Jan 29, 2025
…icitReceiver` (#1984)

* Introduce `HasExplicitMemberAccess` trait

Fixes #1983

* Added trait to GoLanguage as well to be safe

* Added more documentation

* Spotless

* Addressed code review

* Using existing trait

* added more tests

* Fixed the tests as good as they can be fixed in this PR

* a -> globalA

* Fixed another bug where field declarations were declared twice

* Update cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/PythonAddDeclarationsPass.kt

Co-authored-by: Maximilian Kaul <maximilian.kaul@aisec.fraunhofer.de>

* Better documentation

---------

Co-authored-by: Maximilian Kaul <maximilian.kaul@aisec.fraunhofer.de>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
1 participant