mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Prevent encoding of weird types and support packed encoding of external function types.
This commit is contained in:
committed by
Alex Beregszaszi
parent
c2ae33f806
commit
5c8a6aac69
@@ -2077,6 +2077,31 @@ BOOST_AUTO_TEST_CASE(packed_keccak256)
|
||||
testContractAgainstCpp("a(bytes32)", f, u256(-1));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(packed_keccak256_complex_types)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract test {
|
||||
uint120[3] x;
|
||||
function f() view returns (bytes32 hash1, bytes32 hash2, bytes32 hash3) {
|
||||
uint120[] memory y = new uint120[](3);
|
||||
x[0] = y[0] = uint120(-2);
|
||||
x[1] = y[1] = uint120(-3);
|
||||
x[2] = y[2] = uint120(-4);
|
||||
hash1 = keccak256(x);
|
||||
hash2 = keccak256(y);
|
||||
hash3 = keccak256(this.f);
|
||||
}
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
// Strangely, arrays are encoded with intra-element padding.
|
||||
ABI_CHECK(callContractFunction("f()"), encodeArgs(
|
||||
dev::keccak256(encodeArgs(u256("0xfffffffffffffffffffffffffffffe"), u256("0xfffffffffffffffffffffffffffffd"), u256("0xfffffffffffffffffffffffffffffc"))),
|
||||
dev::keccak256(encodeArgs(u256("0xfffffffffffffffffffffffffffffe"), u256("0xfffffffffffffffffffffffffffffd"), u256("0xfffffffffffffffffffffffffffffc"))),
|
||||
dev::keccak256(fromHex(m_contractAddress.hex() + "26121ff0"))
|
||||
));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(packed_sha256)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
contract C {
|
||||
function f() public pure {
|
||||
bytes32 h = keccak256(keccak256, f, this.f.gas, block.blockhash);
|
||||
h;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: This type cannot be encoded.
|
||||
// TypeError: This type cannot be encoded.
|
||||
// TypeError: This type cannot be encoded.
|
||||
// TypeError: This type cannot be encoded.
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
contract C {
|
||||
function f() public pure {
|
||||
bool a = address(this).call(address(this).delegatecall, super);
|
||||
bool b = address(this).delegatecall(log0, tx, mulmod);
|
||||
a; b;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: This type cannot be encoded.
|
||||
// TypeError: This type cannot be encoded.
|
||||
// TypeError: This type cannot be encoded.
|
||||
// TypeError: This type cannot be encoded.
|
||||
// TypeError: This type cannot be encoded.
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
contract C {
|
||||
struct S { uint x; }
|
||||
S s;
|
||||
struct T { }
|
||||
T t;
|
||||
function f() public pure {
|
||||
bytes32 a = sha256(s, t);
|
||||
a;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: This type cannot be encoded.
|
||||
// TypeError: This type cannot be encoded.
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
contract C {
|
||||
struct S { uint x; }
|
||||
S s;
|
||||
struct T { }
|
||||
T t;
|
||||
enum A { X, Y }
|
||||
function f() public pure {
|
||||
bool a = address(this).delegatecall(S, A, A.X, T, uint, uint[]);
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: This type cannot be encoded.
|
||||
// TypeError: This type cannot be encoded.
|
||||
// TypeError: This type cannot be encoded.
|
||||
// TypeError: This type cannot be encoded.
|
||||
// TypeError: This type cannot be encoded.
|
||||
@@ -0,0 +1,9 @@
|
||||
contract C {
|
||||
uint[3] sarr;
|
||||
function f() view public {
|
||||
uint[3] memory arr;
|
||||
bytes32 h = keccak256(this.f, arr, sarr);
|
||||
h;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
Reference in New Issue
Block a user