<!-- canonical: https://0xsimao.com/findings/m-0-custom-overflowing-principal-raised -->

# Custom error for overflowing the total principal is not raised

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

Finding 3S-M^0-L07 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

In the [MToken._mint](https://github.com/MZero-Labs/protocol/blob/3499f50ff3382729f3e59565b19386ba61ef8e36/src/MToken.sol#L217-L233) function, there's a check to make sure the total principal doesn't
become larger than a uint112:
if (
principalOfTotalEarningSupply +
_getPrincipalAmountRoundedDown(totalNonEarningSupply) >= type(uint112).max

```solidity
) {
revert OverflowsPrincipalOfTotalSupply();
}
```

Inside the condition, we're adding two uint112 numbers, which will panic overflow if the
result is greater than type(uint112).max (solidity 0.8 is being used). For this reason, the
error inside the if statement will never actually be reached.

### Recommendation

Cast one of the numbers to uint256 to prevent the addition from panic overflowing:

```solidity
if ( uint256(principalOfTotalEarningSupply) +
_getPrincipalAmountRoundedDown(totalNonEarningSupply) >= type(uint112).max
) {
revert OverflowsPrincipalOfTotalSupply();
}
```

### Status

Addressed in [#bdd94b9](https://github.com/MZero-Labs/protocol/commit/bdd94b9952952c525f9e91ccbd762af792da7d4b).
