19.SWC-119_Shadowing State Variables
2023-07-13 16:11:50 # 09.SWC

SWC-119_Shadowing State Variables

Shadowing State Variables

  • Description: Solidity allows for ambiguous naming of state variables when inheritance is used. Contract A with a variable x could inherit contract B that also has a state variable x defined. This would result in two separate versions of x, one of them being accessed from contract A and the other one from contract B. In more complex contract systems this condition could go unnoticed and subsequently lead to security issues.

    Shadowing state variables can also occur within a single contract when there are multiple definitions on the contract and function level.

  • Remediation: Review storage variable layouts for your contract systems carefully and remove any ambiguities. Always check for compiler warnings as they can flag the issue within a single contract.

vulnerability contract 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pragma solidity 0.4.24;

contract ShadowingInFunctions {
uint n = 2;
uint x = 3;

function test1() constant returns (uint n) {
return n; // Will return 0
}

function test2() constant returns (uint n) {
n = 1;
return n; // Will return 1
}

function test3() constant returns (uint x) {
uint n = 4;
return n+x; // Will return 4
}
}

vulnerability contract 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
pragma solidity 0.4.24;

contract Tokensale {
uint hardcap = 10000 ether;

function Tokensale() {}

function fetchCap() public constant returns(uint) {
return hardcap;
}
}

contract Presale is Tokensale { // 这个问题在高版本的编译器中不存在,会直接报错
uint hardcap = 1000 ether;

function Presale() Tokensale() {}
}
Prev
2023-07-13 16:11:50 # 09.SWC
Next