Skip to content
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

Math.sqrt reverts given uint(-1) #48

Closed
MrChico opened this issue Jan 17, 2020 · 0 comments
Closed

Math.sqrt reverts given uint(-1) #48

MrChico opened this issue Jan 17, 2020 · 0 comments

Comments

@MrChico
Copy link

MrChico commented Jan 17, 2020

The current implementation of sqrt, copied here for convenience:

1.     function sqrt(uint y) internal pure returns (uint z) {
2.         if (y > 3) {
3.             uint x = (y + 1) / 2;
4.             z = y;
5.             while (x < z) {
6.                 z = x;
7.                 x = (y / x + x) / 2;
8.             }
9.         } else if (y != 0) {
10.            z = 1;
11.        }
12.    }

reverts given the argument uint(-1), as this sets x to zero, leading to a division by zero error at line 7.

An easy fix is to change L3 to uint x = y / 2 + 1;, which makes the function well defined for uint(-1) as well.

0xBeaver pushed a commit to 0xBeaver/uniswap-v2-core that referenced this issue Jun 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant