Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Example Analysis of solidity Integer overflow

Shulou Source: shulou.com Published: 2022-06-01 16:51:05 10月01日 Update

This article mainly explains the "solidity integer spillover example analysis", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "solidity integer spillover example analysis"!

What is an integer overflow? When solidity writes a contract, integers are generally defined in uint8, uint256. If a variable is defined as an unsigned 8-bit integer represented by uint8, its value ranges from 0 to 255. When this variable is assigned a value of 256, the integer overflow becomes 0, and so on 257 becomes 1.

Pragma solidity ^ 0.4.24 pure public returns impact author: netkiller / / homepage: http://www.netkiller.cncontract 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;}}

Call the above contract to run the result

255254 + 2 = 0254 + 3 = 1

Subtraction operation result

10-20 = 246

Multiplication run result

51 * 5 = 25551 * 6 = 50

Let's test the multiplication.

255 / 10 = 25

It's kind of like the millennium bug problem, that is, after 1999 becomes year 2000, you can't tell the difference between 1900 and 2000.

Now test the range of values supported by uint256,uint256 is 0 to 2 ^ 256-1

Pragma solidity ^ 0.4.24 uint256 max _ overflow author: netkiller / / homepage: http://www.netkiller.cncontract TestUint256Overflow {/ / (2 stories 256-1) + 1 = 0 upward overflow Test function overflow () pure public returns (uint256 _ spill) {uint256 max = 2 cycles 256-1; return max + 1 } / / 0-1 = 2 downward overflow Test function underflow () pure public returns (uint256 _ underflow) {uint256 min = 0; return min-1;}}

Running result

_ overflow: 0_underflow: 115792089237316195423570985008687907853269984665640564039457584007913129639935

The first function overflow is 0, and the second function 0-1 = 1157920892373161954235709850086879078532699846656405640397584007913129639935

Use the SafeMath library to resolve overflow problems

Pragma solidity ^ 0.4.24 Helplink author: netkiller / / homepage: http://www.netkiller.cnlibrary 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 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;}}

Test SafeMath

Add (11579208923731619542357098500868790532699846640397584007913129639934j1) = > 115792089237316195423570985008687907853269984665640564039457584007913129639935add (1157920892373161954235709850086879053269984664040397584007913129639935991) = > Thank you for your reading. The above is the content of "solidity Integer spillover example Analysis". After the study of this article, I believe you have a deeper understanding of solidity integer spillover analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Test analysis result run problem learning multiplication content function variable contract scope and so on subtraction millennium millennium bug that is ideas situations articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Apple OPPO Reno Shulou Technology Microsoft