Skip to content

Commit

Permalink
fix: return a capped value instead of overflowing
Browse files Browse the repository at this point in the history
  • Loading branch information
MerlinEgalite committed Nov 9, 2023
1 parent 80e075e commit 50a2612
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
3 changes: 0 additions & 3 deletions src/libraries/ErrorsLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ library ErrorsLib {
/// @dev Thrown when passing the zero input.
string internal constant ZERO_INPUT = "zero input";

/// @dev Thrown when wExp overflows.
string internal constant WEXP_OVERFLOW = "wExp overflow";

/// @dev Thrown when the caller is not Morpho.
string internal constant NOT_MORPHO = "not Morpho";
}
14 changes: 10 additions & 4 deletions src/libraries/MathLib.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import {ErrorsLib} from "./ErrorsLib.sol";
import {WAD} from "../../lib/morpho-blue/src/libraries/MathLib.sol";

int256 constant WAD_INT = int256(WAD);
Expand All @@ -16,13 +15,20 @@ library MathLib {
using {wDivDown} for int256;

/// @dev ln(2).
int256 private constant LN2_INT = 0.693147180559945309 ether;
int256 internal constant LN2_INT = 0.693147180559945309 ether;

/// @dev Above this limit `wExp` would overflow. Instead, we return a capped value.
int256 internal constant UPPER_LIMIT = 135.999582271169154765 ether;

/// @dev The value of wExp(UPPER_LIMIT).
uint256 internal constant CAPPED_VALUE = 115792089237316195323137357242501015631897353894317901381819896896488577433600;

/// @dev Returns an approximation of exp.
function wExp(int256 x) internal pure returns (uint256) {
unchecked {
// Revert if x > ln(2^256-1) ~ 177.
require(x <= 177.44567822334599921 ether, ErrorsLib.WEXP_OVERFLOW);
// Return a capped value to avoid overflows.
if (x >= UPPER_LIMIT) return CAPPED_VALUE;

// Return zero if x < -(2**255-1) + (ln(2)/2).
if (x < type(int256).min + LN2_INT / 2) return 0;

Expand Down
6 changes: 2 additions & 4 deletions test/MathLibTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ contract MathLibTest is Test {
}

function testWExpTooLarge(int256 x) public {
// Bound between ln(2**256-1) ~ 177 and 2**255-1.
x = bound(x, 178 ether, type(int256).max);
vm.expectRevert(bytes(ErrorsLib.WEXP_OVERFLOW));
MathLib.wExp(x);
x = bound(x, MathLib.UPPER_LIMIT, type(int256).max);
assertEq(MathLib.wExp(x), MathLib.CAPPED_VALUE);
}
}

0 comments on commit 50a2612

Please sign in to comment.