Skip to content

Instantly share code, notes, and snippets.

@wadealexc
Created June 29, 2018 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wadealexc/96ebeea5cc29af8d9a7909778178a302 to your computer and use it in GitHub Desktop.
Save wadealexc/96ebeea5cc29af8d9a7909778178a302 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.23;
contract Calldata {
// Alters the values on the stack for _a and _b, and returns values not located in calldata
function ignoreCalldata(address _a, bytes memory _b) public view returns (address, bytes memory) {
_a = address(this);
_b = new bytes(5);
_b[0] = 0xaa;
_b[4] = 0xdd;
return (_a, _b);
}
// Alters the values on the stack for _a and _b, but returns the calldata anyway
function returnCalldata(address _a, bytes memory _b) public view returns (address, bytes memory) {
_a = address(this);
_b = new bytes(5);
_b[0] = 0xaa;
_b[4] = 0xdd;
assembly {
calldatacopy(0, 0, calldatasize) // directly copy from calldata
return(0x04, sub(calldatasize, 0x04)) // return copied values
}
}
}
contract Returndata {
Calldata internal cd_contract;
constructor () public {
cd_contract = new Calldata();
}
address internal constant addr = address(0xa);
bytes internal constant bts = "Hello, world!";
// Gets returned data from the Calldata contract and returns values not located in returndata
function ignoreReturndata() public view returns (address a, bytes memory b) {
// Get values from Calldata.ignoreCalldata -
(a, b) = cd_contract.ignoreCalldata(msg.sender, 'test');
// Alter values on stack -
a = addr;
b = bts;
// Return altered values -
return (a, b);
}
// Gets returned data from the Calldata contract and returns values not located in returndata
function returnReturndata() public view returns (address a, bytes memory b) {
// Get values from Calldata.ignoreCalldata -
(a, b) = cd_contract.ignoreCalldata(msg.sender, 'test');
// Alter values on stack -
a = addr;
b = bts;
// Return values returned from Calldata, instead of the altered a and b -
assembly {
returndatacopy(0, 0, returndatasize)
return(0, returndatasize)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment