SWC智能合约漏洞库

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

SWC-111/使用过时的Solidity函数

Solidity中的多个函数和运算符已弃用,使用它们会降低代码质量。对于新的主要版本的Solidity编译器, 使用已弃用的函数和运算符可能会导致副作用和编译错误。

CWE漏洞份额

CWE-477:使用过时的函数

整改方案

Solidity为不推荐使用的结构提供了替代方案。它们大多数是别名,因此替换旧结构不会破坏当前行为。 例如,sha3可以替换为keccak256。

弃用 替代
suicide(address) selfdestruct(address)
block.blockhash(uint) blockhash(uint)
sha3(...) keccak256(...)
callcode(...) delegatecall(...)
throw revert()
msg.gas gasleft
constant view
var 对应的类型名称

参考文献

合约示例

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.yaml

description: Use of deprecated functions, aliases and keywords
issues:
- id: SWC-111
  count: 9
  locations:
  - bytecode_offsets: {}
    line_numbers:
      deprecated_simple.sol: [7]
  - bytecode_offsets: {}
    line_numbers:
      deprecated_simple.sol: [9]
  - bytecode_offsets: {}
    line_numbers:
      deprecated_simple.sol: [10]
  - bytecode_offsets: {}
    line_numbers:
      deprecated_simple.sol: [12]
  - bytecode_offsets: {}
    line_numbers:
      deprecated_simple.sol: [15]
  - bytecode_offsets: {}
    line_numbers:
      deprecated_simple.sol: [18]
  - bytecode_offsets: {}
    line_numbers:
      deprecated_simple.sol: [20]
  - bytecode_offsets: {}
    line_numbers:
      deprecated_simple.sol: [22]
  - bytecode_offsets: {}
    line_numbers:
      deprecated_simple.sol: [24]

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 {}

}

deprecated_simple_fixed.yaml

description: Use of deprecated functions, aliases and keywords
issues:
- id: SWC-111
  count: 0
  locations: []