<!-- canonical: https://0xsimao.com/findings/m-0-code-panic-underflow -->

# Code should not panic underflow

Low/Info · Three Sigma · Stablecoin framework · 8th January, 2024

Finding 3S-M^0-N06 of the M^0 Minter Gateway security review.

- Protocol: https://www.m0.org/
- Report: /reports/m-0
- Codebase: https://github.com/0xsimao/ttg/tree/a8127901fa1f24a2e821cf4d9854a1aa6ac8088c
- Source: https://cdn.sanity.io/files/qoqld077/production/1cdafafad874aba76e062ad8c216c98338c096db.pdf

---

### Description

There's several places in the protocol where proper code validation is missing by, instead,
relying on panic errors.
This is not recommended behaviour as per [Solidity Docs](https://docs.soliditylang.org/en/v0.8.23/control-structures.html#panic-via-assert-and-error-via-require):
Properly functioning code should never create a Panic, not even on invalid
external input. If this happens, then there is a bug in your contract which
you should fix.
Here are the instances that rely on panic errors caught during the review:
- [MToken._subtractEarningAmount](https://github.com/MZero-Labs/protocol/blob/3499f50ff3382729f3e59565b19386ba61ef8e36/src/MToken.sol#L303)
- [MToken._subtractNonEarningAmount](https://github.com/MZero-Labs/protocol/blob/3499f50ff3382729f3e59565b19386ba61ef8e36/src/MToken.sol#L316)
- [ERC20Extended.transferFrom](https://github.com/MZero-Labs/common/blob/4a37119f2da946c6d8ad7b9a70dfdd219225115b/src/ERC20Extended.sol#L86)
- [DistributionVault.getClaimable](https://github.com/MZero-Labs/ttg/blob/a8127901fa1f24a2e821cf4d9854a1aa6ac8088c/src/DistributionVault.sol#L141)
- [PowerToken._divideUp](https://github.com/MZero-Labs/ttg/blob/a8127901fa1f24a2e821cf4d9854a1aa6ac8088c/src/PowerToken.sol#L427)

### Recommendation

Raise proper errors instead of relying on panic.

### Status

Addressed in [#20b3b62](https://github.com/MZero-Labs/protocol/commit/20b3b62f7d5dd97f5541f79cef51f8146a2dcbd5), [#160d105](https://github.com/MZero-Labs/common/commit/160d1058eab98ddb1e0406ae519c13f8b3d9674d),

---

Related findings:

- [API3 oracle timestamp can be set to future timestamp and block API3 Oracle usage to make code revert in underflow](https://0xsimao.com/findings/init-capital-ii-oracle-timestamp-revert-underflow): INIT Capital
