Title
Use of Deprecated Solidity Functions
Relationships
CWE-477: Use of Obsolete Function
Description
Several functions and operators in Solidity are deprecated. Using them leads to reduced code quality. With new major versions of the Solidity compiler, deprecated functions and operators may result in side effects and compile errors.
Remediation
Solidity provides alternatives to the deprecated constructions. Most of them are aliases, thus replacing old constructions will not break current behavior. For example, sha3
can be replaced with keccak256
.
Deprecated | Alternative |
---|---|
suicide(address) |
selfdestruct(address) |
block.blockhash(uint) |
blockhash(uint) |
sha3(...) |
keccak256(...) |
callcode(...) |
delegatecall(...) |
throw |
revert() |
msg.gas |
gasleft |
constant |
view |
var |
corresponding type name |
References
- List of global variables and functions, as of Solidity 0.4.25
- Error handling: Assert, Require, Revert and Exceptions
- View functions
- Untyped declaration is deprecated as of Solidity 0.4.20
- Solidity compiler changelog
Samples
deprecated_simple.sol
pragma solidity ^0.4.24;
contract DeprecatedSimple {
// Do everything that's deprecated, then commit suicide.
function useDeprecated() public constant {
bytes32 blockhash = block.blockhash(0);
bytes32 hashofhash = sha3(blockhash);
uint gas = msg.gas;
if (gas == 0) {
throw;
}
address(this).callcode();
var a = [1,2,3];
var (x, y, z) = (false, "test", 0);
suicide(address(0));
}
function () public {}
}
deprecated_simple_fixed.sol
pragma solidity ^0.4.24;
contract DeprecatedSimpleFixed {
function useDeprecatedFixed() public view {
bytes32 bhash = blockhash(0);
bytes32 hashofhash = keccak256(bhash);
uint gas = gasleft();
if (gas == 0) {
revert();
}
address(this).delegatecall();
uint8[3] memory a = [1,2,3];
(bool x, string memory y, uint8 z) = (false, "test", 0);
selfdestruct(address(0));
}
function () external {}
}