-
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
Overflow-safe addition and multiplication by extending the result type #6270
Comments
I don't think this is a good proposal, as for most cases, if you find you use
I think the result of above is |
My motivation was that a
Thanks, I fixed the typo.
I think Solidity should be over/underflow-safe by default and not require the use of an external library. |
I am not sure, but there is a similar issue about this, solidity/issues/796 |
I think it is a valid proposal to consider the addition of |
Is there anything I can do to help push this proposal? Or is there any formal process for such a language change? |
Unfortunately, we do not have a formal process. Also, radical breaking changes are a little less likely to be adopted nowadays. One problem I see with this proposal is that you basically would have to write |
I wasn't aware of |
The below proposal is not 100% fletched out yet, but I'd like to get a general feedback of whether the idea is desirable. If that is the case, I am more than happy to write a more detailed proposal and discuss further corner cases (see Open ends).
Abstract
The result type of multiplication and addition is the same as the first operand, which may cause an overflow in case the result type cannot hold the multiplication/addition result. That issue could be solved if the multiplication/addition always returned a type that is safe to hold the result. (e.g.
uint16 * uint32 = uint48
)Motivation
Over- and underflow are sources of bugs that are usually hard to spot and easy to miss. It would be great if Solidity could assist the developer in catching those bugs by requiring to make potential overflow positions explicit.
Specification
For simplicity, I only consider
uint
.int
is handled analogously.I propose two changes:
a
andb
of typesuint<x>
anduint<y>
respectively should beuint<x + y>
. The semantics ofa + b
should be equivalent to the current behaviour ofuint<x+y>(a) + b
.a
andb
of typesuint<x>
anduint<y>
respectively should beuint<max(x, y) + 8>
. The semantics ofa + b
should be equivalent to the current behaviour ofuint<max(x, y) + 8>(a) + b
.Open ends
These are some areas I have thought about so far but not reached a definite conclusion on. Discussion on those could continue if the overall idea is desirable.
uint<n>
withn > 256
uint_too_large
that cannot spelled out in source. Hence the user would need to specify a cast and make the potential overflow explicityuint<x> << uint<y>
returnuint<x * y>
. In most cases this will result in auint_too_large
and force the developer to make any potential overflow explicit.+=
and*=
operators are still unsafe for overflow. Their behaviour would need to be considereda
,b
, andc
all have typeuint8
then with the proposed behavioura + b + c
has typeuint24
whileuint16
would be sufficient. That could be solved by introducing typesuint<n>
wheren % 8 != 0
. Those types are internally represented as the next multiple of 8 and are only there to aid the type checker. With those typesa + b
could return auint9
and the addition ofc
would result in auint10
that will then be represented as auint16
on the bytecode level.Backwards Compatibility
All addition and multiplication operations in existing Solidity source code that assign to a type that's not big enough to hold an overflow-free result would need to be audited for overflow. While this is a burden on the developer, I can imagine careful thinking about all of those operations may catch some bugs.
Also it changes behaviour of code like the following:
Previously, the result was
c = (255 + 255) % 256 = 254
. With the new behaviour, the result isc = 510
. I believe that the current behaviour is almost never what the developer wants to write and thus the behaviour change would be OK.The text was updated successfully, but these errors were encountered: