Skip to content

Commit

Permalink
feat: update Cast overflow logic
Browse files Browse the repository at this point in the history
  • Loading branch information
r0ohafza committed Jan 19, 2024
1 parent 08af2d0 commit bf1b8af
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 7 additions & 3 deletions packages/splits-v2/src/libraries/Cast.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
pragma solidity ^0.8.18;

library Cast {
error CastOverflow(uint256 value);
error Overflow();

function toAddress(uint256 value) internal pure returns (address) {
if (value > type(uint160).max) revert CastOverflow(value);
return address(uint160(value));
return address(toUint160(value));
}

function toUint256(address value) internal pure returns (uint256) {
return uint256(uint160(value));
}

function toUint160(uint256 x) internal pure returns (uint160) {
if (x >= 1 << 160) revert Overflow();
return uint160(x);
}
}
7 changes: 4 additions & 3 deletions packages/splits-v2/test/warehouse/SplitsWarehouse.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ contract SplitsWarehouseTest is BaseTest, Fuzzer {
error ReentrancyGuardReentrantCall();
error FailedInnerCall();
error CastOverflow(uint256 value);
error Overflow();

event Withdraw(
address indexed owner, address indexed token, address indexed withdrawer, uint256 amount, uint256 reward
Expand Down Expand Up @@ -50,7 +51,7 @@ contract SplitsWarehouseTest is BaseTest, Fuzzer {

function test_name_Revert_whenTokenIDGreaterThanUint160() public {
uint256 tokenId = uint256(type(uint160).max) + 1;
vm.expectRevert(abi.encodeWithSelector(CastOverflow.selector, tokenId));
vm.expectRevert(Overflow.selector);
warehouse.name(tokenId);
}

Expand All @@ -68,7 +69,7 @@ contract SplitsWarehouseTest is BaseTest, Fuzzer {

function test_symbol_Revert_whenTokenIDGreaterThanUint160() public {
uint256 tokenId = uint256(type(uint160).max) + 1;
vm.expectRevert(abi.encodeWithSelector(CastOverflow.selector, tokenId));
vm.expectRevert(Overflow.selector);
warehouse.symbol(tokenId);
}

Expand All @@ -86,7 +87,7 @@ contract SplitsWarehouseTest is BaseTest, Fuzzer {

function test_decimals_Revert_whenTokenIDGreaterThanUint160() public {
uint256 tokenId = uint256(type(uint160).max) + 1;
vm.expectRevert(abi.encodeWithSelector(CastOverflow.selector, tokenId));
vm.expectRevert(Overflow.selector);
warehouse.decimals(tokenId);
}

Expand Down

0 comments on commit bf1b8af

Please sign in to comment.