-
Notifications
You must be signed in to change notification settings - Fork 11.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected results using SafeMath for uint128 #1925
Comments
Yeah, you are right, |
Thanks for the suggestions all, these were very helpful. Will probably start looking for or working on a
Would this be something that should/could be included here in the openzeppelin repo? |
function unsafeAdd(uint128 a, uint128 b) public pure returns (uint128) {
return uint128(a.add(b));
} To clarify, the dangerous operation here is not the Thank you for reporting and proposing a mitigation! |
Summary
Trying to use SafeMath for
uint128
should throw/ revert in situations when overflows happen! Same holds for overflows of any integer type that are notuint256
📝 Example
Contract:
unit test:
yields a return value of
x = 1
(unexpected/should have reverted).The problem seems to be with the upcasting of everything to
uint256
as seen in the following snippet.openzeppelin-contracts/contracts/math/SafeMath.sol
Lines 26 to 31 in 54182bf
As far as I understand, this implies that SafeMath is only really "safe" for
uint256
and none of the other integer types.💻 Environment
Not really applicable, but this appears to be the case for all versions. Particularly, I ran my examples with
The text was updated successfully, but these errors were encountered: