-
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
Safe Casting Library from uint256 to uintXX #1926
Safe Casting Library from uint256 to uintXX #1926
Conversation
Please note that the failing unit test here is independent of these changes.
|
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 is a cool addition in my opinion. Really good unit tests!
nice contribution @bh2smith |
Hello @bh2smith, thanks for opening this PR and contributing to the library! An issue that may arise from this is that |
Hello @nventuro and thanks for your reply!
This has become apparent to me (the hard way), but the solidity compiler does not complain when "using SafeMath for uint128". I consider this to be somewhat misleading and can lead to serious security vulnerabilities.
At first I considered implementing I would argue that this new layer only enhances the developer toolkit at the expense of knowing to use it in combination with SafeMath where appropriate. Perhaps it would be a good idea to update the Observe the following example contract that demonstrates the use case.
which will act as expected when a + b does not overflow (returning the expected Thanks again for getting back, please let me know your thoughts. |
I'm guessing the compiler allows implicit upcasts? In that case however the return type of the arithmetic operations will still be
Neat! I like this proposal. Since upcasts to |
…eppelin-contracts into safecasting/adresses#1925
Great suggestion, I have included these changes! Please let me know if there is anything else I can do to keep this moving. |
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.
…eppelin-contracts into safecasting/adresses#1925
I will certainly do this @nventuro. Only after I have taken care of the more involved final suggestion regarding the auto-generated describe blocks. If you have any further insights regarding this that might make things a bit quicker on from my side, I'd be happy to hear. Otherwise I will look into other unit tests on this project in hopes to get an idea of how to proceed. As for the change log, I assume it's only appropriate to update it once this last change has been made and the PR is approved. Then, my guess is that I would include a point in the "New Features" Section of version 2.4.0 (unreleased) section. |
I am also unsure and somewhat consider it a utility. However, I would be happy to move it to math if you'd prefer! |
No worries! I went ahead and did a commit directly on your branch with the change. As you can see, it is very simple: you can dynamically call the |
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.
Looks great!
@frangio this is pretty much ready, content-wise. There are a couple things that still need figuring out though:
Depending on these, we will then want to update our documentation. |
Not sure if The only reason why I would consider a different name is if we'd want to provide an alternative implementation in the future that returns a boolean or something instead of reverting. I don't see this happening. I would keep I would also keep this as a separate library. And I would include it in 2.5 since 2.4 is already in beta. |
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.
LGTM! I've added the necessary annotation so that this shows up in the docs.
I've updated the changelog to put this under 2.5 |
The 2.4 final release should be out soon (this week?), and we already have some stuff ready for 2.5, so it shouldn't take long. It mostly depends on whether or not we include any other big things on it, such as ERC1155. Perhaps one month from now?
We can merge right away! The 2.4 release branch has already been created, so this will not cause issues. |
Addresses #1925 and would be a viable alternative to discussions in #1625
Fixes #1925
Downcasting from uint256 in Solidity does not revert by default on overflow. This can easily result in undesired exploitation or bugs, since developers usually assume that overflows raise errors.
SafeCast
restores this intuition by reverting the transaction when such an operation overflows.