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

fixed State variables shadowed by a local variable are incorrectly set to the local variable #2060

Closed

Conversation

Tiko7454
Copy link
Contributor

if variables was not seen before, search for it in the state variables

@Tiko7454
Copy link
Contributor Author

this pull request fixes #2058

@Tiko7454 Tiko7454 changed the title fixed #2058 fixed State variables shadowed by a local variable are incorrectly set to the local variable Jul 20, 2023
@Tiko7454 Tiko7454 closed this Aug 21, 2023
@Tiko7454 Tiko7454 force-pushed the fix-shadowed-before-shadowing-bug branch from 5cabcb3 to 0de7a25 Compare August 21, 2023 12:24
@Tiko7454 Tiko7454 reopened this Aug 21, 2023
@0xalpharush
Copy link
Contributor

// SPDX-License-Identifier: UNLICENSED

pragma solidity 0.8.7;

contract B {
    uint public x = 21;
    function a()  public {
        uint x;
        uint u = 2 * x;
    }
}

Won't this now resolve x to the state variable instead of local?

@Tiko7454
Copy link
Contributor Author

No, it works properly

>>> from slither import Slither
>>> a = Slither('a.sol').contracts[0].functions[0]
>>> a.local_variables
[<slither.core.variables.local_variable.LocalVariable object at 0x7ff15a5ac3d0>, <slither.core.variables.local_variable.LocalVariable object at 0x7ff15af1eb50>]
>>> a.nodes[2].variables_read
[<slither.core.variables.local_variable.LocalVariable object at 0x7ff15af1eb50>]
>>> a.nodes[2].variables_read[0]
<slither.core.variables.local_variable.LocalVariable object at 0x7ff15af1eb50>
>>> print(a.nodes[2].variables_read[0])
x

@0xalpharush
Copy link
Contributor

0xalpharush commented Aug 31, 2023

It works for the compact AST because we have the referenced declaration. I'm not sure how to solve this for legacy AST without reworking name resolution quite a bit

You can try it by removing the pragma:
solc-select use 0.7.6 --always-installl && slither example.sol --print declaration --solc-force-legacy-json
u references the state variable

# Contracts
# B
        - Declaration: example.sol#5 (10 - 12)
        - Implementation: example.sol#5-11 (1 - 2)
        - References: []

        ## Function
                - B.a()
                        - Declaration: example.sol#7 (14 - 16)
                        - Implementation: example.sol#7-10 (5 - 6)
                        - References: []
                - B.slitherConstructorVariables()
                        - Declaration: example.sol#5-6 (1 - 16)
                        - Implementation: example.sol#5-11 (1 - 2)
                        - References: []

        ## State variables
                - x
                        - Declaration: example.sol#6 (17 - 19)
                        - Implementation: example.sol#6 (5 - 23)
                        - References: ['example.sol#9 (22 - 23)']

@Tiko7454
Copy link
Contributor Author

Tiko7454 commented Sep 1, 2023

When is legacy json considered as default option?

@0xalpharush
Copy link
Contributor

It is enabled by default for solc 0.4.0 until 0.4.12. Then it must be enabled manually. It was deprecated in 0.8.0

@montyly
Copy link
Member

montyly commented Sep 15, 2023

I am fine if we don't consider the legacy AST for new fixes moving forward.

In the long term we need to rewrite the solc parsing without the legacy AST to reduce complexity, and keep the current parser as "best effort" for the legacy format

@0xalpharush
Copy link
Contributor

#2121 fixes both

@0xalpharush
Copy link
Contributor

@Tiko7454 We merged #2121 as fix. Thanks for reporting and purposing a fix this issue!

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

Successfully merging this pull request may close these issues.

3 participants