-
Notifications
You must be signed in to change notification settings - Fork 383
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
StableMath::_calculateInvariant
can, in fact, overflow
#2491
Comments
Hello @xenide! You are correct in that the comments don't make sense given the current state of the code. I believe we wrote those a while ago, and later decided that the risk of undetected overflow wasn't worth it and switched to using checked arithmetic.
Hm, this seems correct at a glance, but I'd need to review the implementation more thoroughly to be sure. I don't think this is much of an issue however as this would be a huge edge case for a number of reasons. Was there anything about this that had you worried? |
@nventuro nope not any real life cases in particular. Just occurred to me that this is a possibility. Not sure if it's a concern if / when it happens and withdrawing liquidity is affected. A quick glance at |
StableMath:: _calculateInvariant
can, in fact, overflowStableMath::_calculateInvariant
can, in fact, overflow
The reason why it's not a concern is that we added recovery mode, which completely bypasses any invariant calculations and simply performs a proportional withdrawal. That's (if I recall correctly) when we added checked arithmetic in order to avoid silent errors. |
Would like to highlight 2 things:
_calculateInvariant
actually makes use of theMath
library, which actually checks for overflow.type(uint112).max
That would result in the
invariant
andD_P
in the range ofuint172
, which will then cause an overflow when multiplyingD_P
withinvariant
on this lineThe text was updated successfully, but these errors were encountered: