Skip to content

Commit

Permalink
fix: check selector length (#912)
Browse files Browse the repository at this point in the history
  • Loading branch information
rndquu authored Mar 4, 2024
1 parent 4eea344 commit 49f9572
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/contracts/src/dollar/Diamond.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ contract Diamond {
ds.slot := position
}
address facet = ds.selectorToFacetAndPosition[msg.sig].facetAddress;
require(msg.data.length >= 4, "Diamond: Selector is too short");
require(facet != address(0), "Diamond: Function does not exist");
assembly {
calldatacopy(0, 0, calldatasize())
Expand Down
10 changes: 10 additions & 0 deletions packages/contracts/test/diamond/DiamondTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,14 @@ contract TestDiamond is DiamondTestSetup {
}
}
}

function testFallback_ShouldRevert_IfSelectorIsTooShort() public {
// pass 3 bytes selector while normally it should be 4 bytes length
vm.expectRevert(bytes("Diamond: Selector is too short"));
(bool revertsAsExpected, ) = address(diamond).call(bytes("000"));
// NOTICE: for low level calls (like `address.call()`) the returned result
// is NOT the success of the low level call but the success of the `vm.expectRevert()`
// expression, more info: https://book.getfoundry.sh/cheatcodes/expect-revert
assertTrue(revertsAsExpected);
}
}

0 comments on commit 49f9572

Please sign in to comment.