Solidity: Uninitialised storage references should not be allowed

Created on 16 Mar 2017  路  8Comments  路  Source: ethereum/solidity

Defining a struct instance within a function defaults to storage. This shouldn't be allowed.

contract c {
    struct s { uint a; }
    function f() {
        s x;
        x.a = 2;
    }
}

(From https://www.pivotaltracker.com/story/show/82613514.)

breaking change bug

All 8 comments

We already have a warning here. The reason I did not turn this into an error was that there are use-cases where the warning can only be suppressed in a weird way:

contract c {
    struct s { uint a; }
    mapping(uint => s) data;
    function f(uint i) internal returns (s) {
        s x;
        if (i % 2 == 1)
          x = data[10 * i];
        else
           x = data[i / 2];
        return x;
    }
}

The far more dangerous situation where we do not have a warning yet (I guess I missed that) is:

contract c {
    struct s { uint a; }
    function f() internal returns (s) {
    }
}

Storage references have to be assigned to at every point in the control flow they are used. This especially applies to functions returning storage pointers.

'Use after free' is out of scope for this task.

This has been fixed.

Will this become an error in future releases? I'm unsure of what "this has been fixed" means.

@microbecode yes, this will be a warning in future releases and an error starting from 0.5.0. Please try the nightly compiler builds if you want to check.

@chriseth Will compiler throw error on below code from 0.5.0?

contract A {
   uint a = 0;

   function(){
      uint[] a;
      a[0] = 1;
   }
}

@sjmini yes, multiple.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chriseth picture chriseth  路  3Comments

axic picture axic  路  4Comments

walter-weinmann picture walter-weinmann  路  4Comments

bshastry picture bshastry  路  3Comments

chriseth picture chriseth  路  4Comments