Skip to content

Commit 0d9ada8

Browse files
author
bohendo
committed
describe solc version impact on integer over-/under-flows
1 parent dac979c commit 0d9ada8

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Integer Overflow
22

3-
It is possible to cause `add` and `sub` to overflow (or underflow) on any type of integer in Solidity.
3+
It is possible to cause `+` and `-` to overflow (or underflow) on any type of integer in Solidity versions <0.8.0 or within `unchecked` blocks of solidity >=0.8.0
44

55
## Attack Scenarios
66

@@ -12,13 +12,14 @@ the array and alter other variables in the contract.
1212

1313
## Mitigations
1414

15-
- Use openZeppelin's [safeMath library](https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/math/SafeMath.sol)
16-
- Validate all arithmetic
15+
- Use solidity >=0.8.0 and use `unchecked` blocks carefully and only where required.
16+
- If using solidity <0.8.0, use OpenZeppelin's [SafeMath library](https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/math/SafeMath.sol) for arithmetic.
17+
- Validate all arithmetic with both manual review and property-based fuzz testing.
1718

1819
## Examples
1920

2021
- In [integer_overflow_1](interger_overflow_1.sol), we give both unsafe and safe version of
2122
the `add` operation.
2223

23-
- [A submission](https://github.com/Arachnid/uscc/tree/master/submissions-2017/doughoyte) to the Underhanded Solidity Coding Contest that explots the unsafe dynamic array bug outlined above
24+
- [A submission](https://github.com/Arachnid/uscc/tree/master/submissions-2017/doughoyte) to the Underhanded Solidity Coding Contest that exploits the unsafe dynamic array bug outlined above
2425

not-so-smart-contracts/solidity/integer_overflow/integer_overflow_1.sol

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ pragma solidity ^0.4.15;
22

33
contract Overflow {
44
uint private sellerBalance=0;
5-
5+
66
function add(uint value) returns (bool){
77
sellerBalance += value; // possible overflow
8-
9-
// possible auditor assert
10-
// assert(sellerBalance >= value);
11-
}
8+
// the following assertion will revert if the above overflows
9+
// assert(sellerBalance >= value);
10+
}
1211

1312
function safe_add(uint value) returns (bool){
1413
require(value + sellerBalance >= sellerBalance);
15-
sellerBalance += value;
16-
}
14+
sellerBalance += value;
15+
}
1716
}

0 commit comments

Comments
 (0)