-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Checked arithmetic by default. #9465
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -246,21 +246,32 @@ Two's Complement / Underflows / Overflows | |||||
========================================= | ||||||
|
||||||
As in many programming languages, Solidity's integer types are not actually integers. | ||||||
They resemble integers when the values are small, but behave differently if the numbers are larger. | ||||||
For example, the following is true: ``uint8(255) + uint8(1) == 0``. This situation is called | ||||||
an *overflow*. It occurs when an operation is performed that requires a fixed size variable | ||||||
to store a number (or piece of data) that is outside the range of the variable's data type. | ||||||
An *underflow* is the converse situation: ``uint8(0) - uint8(1) == 255``. | ||||||
They resemble integers when the values are small, but cannot represent arbitrarily large numbers. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't precision something else? |
||||||
|
||||||
The following code causes an overflow because the result of the addition is too large | ||||||
to be stored in the type ``uint8``: | ||||||
|
||||||
:: | ||||||
|
||||||
uint8 x = 255; | ||||||
uint8 y = 1; | ||||||
return x + y; | ||||||
|
||||||
Solidity has two modes in which it deals with these overflows: Checked and Unchecked or "wrapping" mode. | ||||||
|
||||||
The default checked mode will detect overflows and cause a failing assertion. You can disable this check | ||||||
using ``unchecked { ... }``, causing the overflow to be silently ignored. The above code would return | ||||||
``0`` if wrapped in ``unchecked { ... }``. | ||||||
|
||||||
Even in checked mode, do not assume you are protected from overflow bugs. | ||||||
In this mode, overflows will always revert. If it is not possible to avoid the | ||||||
overflow, this can lead to a smart contract being stuck in a certain state. | ||||||
|
||||||
In general, read about the limits of two's complement representation, which even has some | ||||||
more special edge cases for signed numbers. | ||||||
|
||||||
Try to use ``require`` to limit the size of inputs to a reasonable range and use the | ||||||
:ref:`SMT checker<smt_checker>` to find potential overflows, or use a library like | ||||||
`SafeMath <https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol>`_ | ||||||
if you want all overflows to cause a revert. | ||||||
|
||||||
Code such as ``require((balanceOf[_to] + _value) >= balanceOf[_to])`` can also help you check if values are what you expect. | ||||||
:ref:`SMT checker<smt_checker>` to find potential overflows. | ||||||
|
||||||
.. _clearing-mappings: | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allows for nested unchecked blocks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is not a parser but a syntax checker restriction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
if (<sth>) unchecked { x; }
invalid? (I'll probably know when I'm done reviewing...)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, apparently it isn't - not sure if it couldn't be valid, but fine!