Skip to content

Commit

Permalink
Use memory-safe for the SafeMock Fallback Implementation (#200)
Browse files Browse the repository at this point in the history
I noticed that the `fallback` function implementation was not memory
safe for the SafeMock contract. This PR rectifies this.

Additionally, FCL assembly is also not tagged as memory safe (but should
be AFAIU). We should probably submit a PR upstream to tag assembly as
memory safe.
  • Loading branch information
nlordell authored Jan 9, 2024
1 parent 4782541 commit b37e394
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions 4337/contracts/test/SafeMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,24 @@ contract SafeMock {
// solhint-disable-next-line payable-fallback,no-complex-fallback
fallback() external payable {
// solhint-disable-next-line no-inline-assembly
assembly {
assembly ("memory-safe") {
let handler := sload(fallbackHandler.slot)
if iszero(handler) {
return(0, 0)
}
calldatacopy(0, 0, calldatasize())

let ptr := mload(0x40)
calldatacopy(ptr, 0, calldatasize())
// The msg.sender address is shifted to the left by 12 bytes to remove the padding
// Then the address without padding is stored right after the calldata
mstore(calldatasize(), shl(96, caller()))
mstore(add(ptr, calldatasize()), shl(96, caller()))
// Add 20 bytes for the address appended add the end
let success := call(gas(), handler, 0, 0, add(calldatasize(), 20), 0, 0)
returndatacopy(0, 0, returndatasize())
let success := call(gas(), handler, 0, ptr, add(calldatasize(), 20), 0, 0)
returndatacopy(ptr, 0, returndatasize())
if iszero(success) {
revert(0, returndatasize())
revert(ptr, returndatasize())
}
return(0, returndatasize())
return(ptr, returndatasize())
}
}

Expand Down

0 comments on commit b37e394

Please sign in to comment.