solidity 语言安全·整型溢出

本文节选自《Netkiller Blockchain 手札》

 

Netkiller Blockchain 手札

Mr. Neo Chan, 陈景峯(BG7NYT)

中国广东省深圳市龙华新区民治街道溪山美地 518131 +86 13113668890 <netkiller@msn.com>html

文档始创于2018-02-10git

版权 © 2018 Netkiller(Neo Chan). All rights reserved.github

版权声明web

转载请与做者联系,转载时请务必标明文章原始出处和做者信息及本声明。运维

内容摘要函数

这一部关于区块链开发及运维的电子书。区块链

为何会写区块链电子书?由于2018年是区块链年,区块链是一个风口,前几个风口我都错过了。例如web2.0, 云, 大数据等等,都从身旁擦肩而过。因此我要抓住此次。测试

这本电子书是否会出版(纸质图书)? 不会,由于互联网技术更迭太快,纸质书籍的内容没法实时更新,一本书动辄百元,很快就成为垃圾,你会发现目前市面的上区块链书籍至少是一年前写的,内容已通过时,不少例子没法正确运行。因此我不会出版,电子书的内容会追逐技术发展,及时跟进软件版本的升级,作到内容最新,至少是主流。大数据

这本电子书与其余区块链书籍有什么不一样?市面上大部分区块链书籍都是用2/3去讲区块链原理,只要不到 1/3 的干货,干货不够理论来凑,通篇将理论或是大谈特谈区块链行业,这些内容更可能是头脑风暴,展望区块链,均没法落地实施。本书与那些书籍彻底不一样,不讲理论和原理,面向应用落地,注重例子,均是干货。ui

写做原则,没法落地的项目做者绝对不会写。凡是写入电子的内容均具有可操做,可落地。

电子书更新频率?天天都会有新内容加入,更新频率最迟不会超过一周,更新内容请关注 https://github.com/netkiller/netkiller.github.io/commits/master

本文采用碎片化写做,原文会不按期更新,请尽可能阅读原文 http://www.netkiller.cn/blockchain/index.html

您的打赏是个人写做动力:http://www.netkiller.cn/blockchain/donations.html

接受 ETH 打赏:0x3e827461Cc53ed7c75A29187CfF39629FCAE3661

 

什么是整型溢出呢?在solidity编写合约时,定义整型通常是用uint8, uint256。一个变量若是定义为uint8表示的无符号的8位整型,即取值范围为0-255。当给这个变量赋值256时,即整型溢出变成了0,以此类推257变成了1。

pragma solidity ^0.4.24;

//author: netkiller <netkiller@msn.com>
//homepage: http://www.netkiller.cn

contract NetkillerOverflowTest{
    

    function add(uint8 a, uint8 b) pure public returns (uint8){
        
        uint8 result = a + b;
        
        return result;
        
    }
    
    function sub(uint8 a, uint8 b) pure public returns (uint8){
        
        uint8 result = a - b;
        
        return result;
        
    }
    
    function mul(uint8 a, uint8 b) pure public returns (uint8){
        
        uint8 result = a * b;
        
        return result;
        
    }
    
    function div(uint8 a, uint8 b) pure public returns (uint8){
        
        uint8 result = a / b;
        
        return result;
        
    }
    
}

调用上面合约,运行结果

254 + 1 = 255
254 + 2 = 0
254 + 3 = 1

减法运行结果

10 - 20 = 246

乘法运行结果

51 * 5 = 255
51 * 6 = 50

再来测试乘法

255 / 10 = 25

这有点想千年虫问题,即99年变成00年后,你没法区分1900年仍是2000年。

如今测试一下uint256,uint256支持的取值范围是0到2^256-1

pragma solidity ^0.4.24;

//author: netkiller <netkiller@msn.com>
//homepage: http://www.netkiller.cn

contract TestUint256Overflow {
    // (2**256 – 1) + 1 = 0 向上溢出测试
    function overflow() pure public returns (uint256 _overflow) {
        uint256 max = 2 ** 256 - 1;
        return max + 1;
    }

    // 0 – 1 = 2**256 – 1 向下溢出测试
    function underflow() pure public returns (uint256 _underflow) {
        uint256 min = 0;
        return min - 1;
    }
}

运行结果

_overflow : 0
_underflow : 115792089237316195423570985008687907853269984665640564039457584007913129639935

第一个函数溢出为 0,第二个函数 0 - 1 = 115792089237316195423570985008687907853269984665640564039457584007913129639935

解决溢出问题使用SafeMath库

pragma solidity ^0.4.24;

//author: netkiller <netkiller@msn.com>
//homepage: http://www.netkiller.cn

library SafeMath {

  function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
    if (a == 0) {
      return 0;
    }

    c = a * b;
    assert(c / a == b);
    return c;
  }

  function div(uint256 a, uint256 b) internal pure returns (uint256) {
    return a / b;
  }

  function sub(uint256 a, uint256 b) internal pure returns (uint256) {
    assert(b <= a);
    return a - b;
  }

  function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
    c = a + b;
    assert(c >= a);
    return c;
  }
}

contract NetkillerSafeMath {
 
    using SafeMath for uint256;    
    
    function add(uint256 a, uint256 b) pure public returns (uint256){
        uint256 result = a.add(b);
        return result;
    }
    function sub(uint256 a, uint256 b) pure public returns (uint256){
        uint256 result = a.sub(b);
        return result;
    }
    function mul(uint256 a, uint256 b) pure public returns (uint256){
        uint256 result = a.mul(b);
        return result;
    }
    function div(uint256 a, uint256 b) pure public returns (uint256){
        uint256 result = a.div(b);
        return result;
    }
}

测试 SafeMath

add(115792089237316195423570985008687907853269984665640564039457584007913129639934,1)  => 115792089237316195423570985008687907853269984665640564039457584007913129639935
add(115792089237316195423570985008687907853269984665640564039457584007913129639935,1)  => 抛出异常
相关文章
相关标签/搜索