Skip to content

Commit 7ab1afa

Browse files
committed
feat: add LibSafeRange
1 parent 6895647 commit 7ab1afa

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/math/LibSafeRange.sol

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
library LibSafeRange {
5+
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
6+
unchecked {
7+
c = a + b;
8+
if (c < a) return type(uint256).max;
9+
}
10+
}
11+
12+
/**
13+
* @dev Returns value of a + b; in case result is larger than upperbound, upperbound is returned.
14+
*/
15+
function addWithUpperbound(uint256 a, uint256 b, uint256 ceil) internal pure returns (uint256 c) {
16+
if (a > ceil || b > ceil) return ceil;
17+
c = add(a, b);
18+
if (c > ceil) return ceil;
19+
}
20+
}

0 commit comments

Comments
 (0)