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

Storage variables not written back to storage #34

Open
enitrat opened this issue Jul 16, 2023 · 1 comment
Open

Storage variables not written back to storage #34

enitrat opened this issue Jul 16, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@enitrat
Copy link

enitrat commented Jul 16, 2023

Describe the desired feature

Detect if the value read from a storage variable is mutated, but the updated value is not written back to storage.

Associated example:

#[contract]
mod StorageVarNotUpdated {
    struct Storage {
        _value: u128
    }

    #[external]
    fn bad() {
        let mut value = _value::read();
        value += 1;
    }

    #[external]
    fn good() {
        let mut value = _value::read();
        value += 1;
        _value::write(value);
    }

    #[external]
    fn good2() {
        update_value();
    }

    // update value in a private function
    fn update_value() {
        let mut value = _value::read();
        value += 1;
        _value::write(value);
    }
}

This is probably a bit tricky to do at the Sierra level, because we don't know whether a storage variable is read as mut or not; which makes it hard to catch mutability intents at the Cairo level.

@enitrat enitrat added the enhancement New feature or request label Jul 16, 2023
@smonicas
Copy link
Collaborator

smonicas commented Aug 8, 2023

Hi, thanks for the great idea. I looked into it and i think at the moment by using only sierra it's not possible to implement a detector without too many false positives because for example these two functions have the same sierra. I keep the issue open to revisit when there will be sierra to source code mapping.

    #[external(v0)]
    fn good(ref self: ContractState) {
        let mut value = self.a.read();
        value += 435;
        self.a.write(value);
    }

    #[external(v0)]
    fn good2(ref self: ContractState) {
        let mut value = self.a.read();
        let b = value + 435;
        self.a.write(b);
    }

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

No branches or pull requests

2 participants