-
Notifications
You must be signed in to change notification settings - Fork 5.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
Exception on overflow #796
Comments
The main Problem I see with this is that you still have to aware of the possibility of an arithmetic overflow. If we throw in that case, this could stall a smart contract. The only solution I see is to use our formal verification system that automatically verifies whether overflow protection checks are in place (or not needed). |
Could the same argument be made for the exception that is thrown for division by 0 and a negative array index? Those could have the same effect, but I can't see arguing that coming up with some default but incorrect behavior would be better (e.g. We could just wrap array indices around). My concern is that the failures that can come from overflow go undetected. I am definitely not suggesting that formal verification should not be done, there is no substitute. But I think allowing overflow offers no real advantage, unless you're hoping to hack a contract. |
You have a valid point. Still, I think these are not exactly comparable: But yes, it is probably better to stall a contract in case of overflow than to provoke undesired effects on state. What are cases where overflow is actually desired? I think for most of them, we can still use |
Note that this is quite expensive to do, especially for types shorter than 256 bits. |
Would you consider this for 0.4.0, or is it a bit involved for that? |
Maybe this should be implemented in the EVM? |
I think arithmetic overflow detection belongs to the EVM, but that would be a rather big change to it. |
For the EVM I think it would need extra opcodes as overflowing math is still useful. In the meantime we can have it in Solidity and get the safety benefits at the expense of a bit of gas (completely worth it I think because you end up checking it in safe code anyway). |
@rainbeam This is too involved for 0.4.0. Arithmetics is almost for free when compared to storage writes or even a transaction, so I think it is fine to do it. I do not really agree that this belongs to the EVM. Something like an overflow flag might be added to the EVM, but not a "force-stop" on overflow. |
By detection I meant the overflow flag. So perhaps that would then needs its on opcode for getting the overflow flag onto the stack and/or its own jump ( |
@pirapira Can you please list all these other issues as tasks for this issue and close the others, or do you think that each issues is a substantial amount of work? |
The main reason I opened separate issues is that each requires discussion if we want it or not, but if we are set to go over everything, here is a list.
|
This is labelled "soon" and how soon will this be? :) Does the long checklist need to be implemented at once, or can it be done iteratively?
|
@pirapira In your checklist, Also, how about overflow of INT_MIN/-1 ? |
@ethers Thank you. I fixed the checklist above. These overflow checkings are breaking changes, and for some reasons, Solidity release |
If Solidity doesn't already do this, a line item in the TODO list should be added for truncation during assignment.
|
@axic yes, an unchecked region where also the SMT checker is disabled is certainly a good idea. Whether we can enable runtime checks by default in the non-unchecked areas should also depend on an assessment about how well the optimizer can handle such cases, and perhaps an improvement in that area. |
The recently discovered overflow on batchTransfer in some ERC20 contracts is a good example of the type of issue that could be avoided if this is implemented. |
For the syntax (mentioned in #796 (comment)) I propose:
It follows the same syntax as not-so-well-known syntax of the assembly keyword: |
@axic yep, exactly my thought, that allows us to introduce other checks in the same way later. |
That was my motivation :) |
@axic what about making the current math operators safe and adding new operators for non-safe math
pick whatever symbol works, i chose Edit: |
In #796 (comment) I think there is one more case not mentioned, exponentiation. |
Update links to EIPs in Byzantium Informational
How many of these are completed till now? |
@johnsoncarl we are implementing this along with the transition to yul as an intermediate language. The reason is that we hope that the yul optimizer can better cope with all the additional checks that are introduced and complicate the control-flow. It is still not 100% clear if we really want that on explicit type conversions. It is implemented (or at least PRs in progress) for increment, decrement, unsigned multiplication and addition. |
We are thinking of using the existing yul implementation and adding it to 0.7.0. |
Implemented in #9465 |
It would be nice if Solidity, by default, threw an exception for arithmetic overflow, rather than allowing it.
Justification:
2: Even in cases where overflow is desired behavior, throwing an exception is a far safer failure mode and easier to detect/correct.
3: The goal of solidity, as I understand it, is to enable the creation of secure and reliable smart contacts. Therefore, although it is of course possible for the developer to include their own checks, including this as a feature of the language will make the common case both simple and more secure.
If this is not achievable, I welcome alternatives that do not require each and every developer to remember these corner cases for each and every arithmetic operation. A solution that states "everybody should do XYZ" will inevitably lead to failures.
I'm am not familiar enough with the code to propose a detailed solution. However, a compiler switch that is on by default and can be disabled for backward compatibility is the sort of approach I was imagining.
The text was updated successfully, but these errors were encountered: