Skip to content

Commit

Permalink
feat: add ConversionHelpers lib for converting memory types
Browse files Browse the repository at this point in the history
  • Loading branch information
sohkai committed Mar 26, 2019
1 parent dca0b4b commit 0281046
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
13 changes: 2 additions & 11 deletions contracts/acl/ACL.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pragma solidity 0.4.24;

import "../apps/AragonApp.sol";
import "../common/ConversionHelpers.sol";
import "../common/TimeHelpers.sol";
import "./ACLSyntaxSugar.sol";
import "./IACL.sol";
Expand Down Expand Up @@ -242,17 +243,7 @@ contract ACL is IACL, TimeHelpers, AragonApp, ACLHelpers {
* @return boolean indicating whether the ACL allows the role or not
*/
function hasPermission(address _who, address _where, bytes32 _what, bytes memory _how) public view returns (bool) {
// Force cast the bytes array into a uint256[], by overwriting its length
// Note that the uint256[] doesn't need to be initialized as we immediately overwrite it
// with _how and a new length, and _how becomes invalid from this point forward
uint256[] memory how;
uint256 intsLength = _how.length / 32;
assembly {
how := _how
mstore(how, intsLength)
}

return hasPermission(_who, _where, _what, how);
return hasPermission(_who, _where, _what, ConversionHelpers.castBytesToUintArray(_how));
}

function hasPermission(address _who, address _where, bytes32 _what, uint256[] memory _how) public view returns (bool) {
Expand Down
14 changes: 3 additions & 11 deletions contracts/apps/AragonApp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
pragma solidity ^0.4.24;

import "./AppStorage.sol";
import "../acl/ACLSyntaxSugar.sol";
import "../common/Autopetrified.sol";
import "../common/ConversionHelpers.sol";
import "../common/VaultRecoverable.sol";
import "../evmscript/EVMScriptRunner.sol";
import "../acl/ACLSyntaxSugar.sol";


// Contracts inheriting from AragonApp are, by default, immediately petrified upon deployment so
Expand Down Expand Up @@ -47,16 +48,7 @@ contract AragonApp is AppStorage, Autopetrified, VaultRecoverable, EVMScriptRunn
return false;
}

// Force cast the uint256[] into a bytes array, by overwriting its length
// Note that the bytes array doesn't need to be initialized as we immediately overwrite it
// with _params and a new length, and _params becomes invalid from this point forward
bytes memory how;
uint256 byteLength = _params.length * 32;
assembly {
how := _params
mstore(how, byteLength)
}
return linkedKernel.hasPermission(_sender, address(this), _role, how);
return linkedKernel.hasPermission(_sender, address(this), _role, ConversionHelpers.castUintArrayToBytes(_params));
}

/**
Expand Down
26 changes: 26 additions & 0 deletions contracts/common/ConversionHelpers.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pragma solidity ^0.4.24;


library ConversionHelpers {
function castUintArrayToBytes(uint256[] memory _input) internal pure returns (bytes memory output) {
// Force cast the uint256[] into a bytes array, by overwriting its length
// Note that the bytes array doesn't need to be initialized as we immediately overwrite it
// with the input and a new length. The input becomes invalid from this point forward.
uint256 byteLength = _input.length * 32;
assembly {
output := _input
mstore(output, byteLength)
}
}

function castBytesToUintArray(bytes memory _input) internal pure returns (uint256[] memory output) {
// Force cast the bytes array into a uint256[], by overwriting its length
// Note that the uint256[] doesn't need to be initialized as we immediately overwrite it
// with the input and a new length. The input becomes invalid from this point forward.
uint256 intsLength = _input.length / 32;
assembly {
output := _input
mstore(output, intsLength)
}
}
}
20 changes: 7 additions & 13 deletions contracts/kernel/Kernel.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import "./KernelConstants.sol";
import "./KernelStorage.sol";
import "../acl/IACL.sol";
import "../acl/ACLSyntaxSugar.sol";
import "../lib/misc/ERCProxy.sol";
import "../common/ConversionHelpers.sol";
import "../common/IsContract.sol";
import "../common/Petrifiable.sol";
import "../common/VaultRecoverable.sol";
import "../factory/AppProxyFactory.sol";
import "../lib/misc/ERCProxy.sol";


// solium-disable-next-line max-len
Expand Down Expand Up @@ -227,18 +228,11 @@ contract Kernel is IKernel, KernelStorage, KernelAppIds, KernelNamespaceConstant
}
}

modifier auth(bytes32 _role, uint256[] memory params) {
// Force cast the uint256[] into a bytes array, by overwriting its length
// Note that the bytes array doesn't need to be initialized as we immediately overwrite it
// with params and a new length, and params becomes invalid from this point forward
bytes memory how;
uint256 byteLength = params.length * 32;
assembly {
how := params
mstore(how, byteLength)
}

require(hasPermission(msg.sender, address(this), _role, how), ERROR_AUTH_FAILED);
modifier auth(bytes32 _role, uint256[] memory _params) {
require(
hasPermission(msg.sender, address(this), _role, ConversionHelpers.castUintArrayToBytes(_params)),
ERROR_AUTH_FAILED
);
_;
}
}

0 comments on commit 0281046

Please sign in to comment.