mirror of
https://github.com/autistic-symposium/blockchains-security-toolkit.git
synced 2025-05-13 12:12:19 -04:00
Rename advanced_expert/vulnerabilities/overflow.md to advanced_expert/vulnerabilities/arithmetic_errors/overflow.md
This commit is contained in:
parent
b208ff451a
commit
1c1ccd7cb6
1 changed files with 0 additions and 0 deletions
|
@ -0,0 +1,54 @@
|
|||
## overflow of numbers
|
||||
|
||||
<br>
|
||||
|
||||
|
||||
<br>
|
||||
|
||||
----
|
||||
|
||||
### unchecked math
|
||||
|
||||
<br>
|
||||
|
||||
* overflow and underflow of numbers in solidity 0.8 throw an error. this can be disabled with `unchecked`.
|
||||
* disabling overflow / underflow check saves gas.
|
||||
|
||||
<br>
|
||||
|
||||
```
|
||||
contract UncheckedMath {
|
||||
function add(uint x, uint y) external pure returns (uint) {
|
||||
// 22291 gas
|
||||
// return x + y;
|
||||
|
||||
// 22103 gas
|
||||
unchecked {
|
||||
return x + y;
|
||||
}
|
||||
}
|
||||
|
||||
function sub(uint x, uint y) external pure returns (uint) {
|
||||
// 22329 gas
|
||||
// return x - y;
|
||||
|
||||
// 22147 gas
|
||||
unchecked {
|
||||
return x - y;
|
||||
}
|
||||
}
|
||||
|
||||
function sumOfCubes(uint x, uint y) external pure returns (uint) {
|
||||
// Wrap complex math logic inside unchecked
|
||||
unchecked {
|
||||
uint x3 = x * x * x;
|
||||
uint y3 = y * y * y;
|
||||
|
||||
return x3 + y3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
<br>
|
Loading…
Add table
Add a link
Reference in a new issue