Merge pull request #9870 from ethereum/bytes-literal

Report why assigning oversized hex strings to bytes fail
This commit is contained in:
chriseth 2020-09-23 18:06:26 +02:00 committed by GitHub
commit 031840697c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 59 additions and 1 deletions

View File

@ -13,6 +13,7 @@ Compiler Features:
* SMTChecker: Support ``type(T).min``, ``type(T).max``, and ``type(I).interfaceId``.
* SMTChecker: Support ``address`` type conversion with literals, e.g. ``address(0)``.
* Type Checker: More detailed error messages why implicit conversions fail.
* Type Checker: Explain why oversized hex string literals can not be explicitly converted to a shorter ``bytesNN`` type.
* Yul Optimizer: Prune unused parameters in functions.
* Yul Optimizer: Inline into functions further down in the call graph first.
* Yul Optimizer: Try to simplify function names.

View File

@ -1351,7 +1351,11 @@ StringLiteralType::StringLiteralType(string _value):
BoolResult StringLiteralType::isImplicitlyConvertibleTo(Type const& _convertTo) const
{
if (auto fixedBytes = dynamic_cast<FixedBytesType const*>(&_convertTo))
return static_cast<size_t>(fixedBytes->numBytes()) >= m_value.size();
{
if (static_cast<size_t>(fixedBytes->numBytes()) < m_value.size())
return BoolResult::err("Literal is larger than the type.");
return true;
}
else if (auto arrayType = dynamic_cast<ArrayType const*>(&_convertTo))
return
arrayType->location() != DataLocation::CallData &&

View File

@ -0,0 +1,7 @@
contract C {
function f() public pure returns (bytes2) {
return bytes2(hex"123456");
}
}
// ----
// TypeError 9640: (76-95): Explicit type conversion not allowed from "literal_string "4V"" to "bytes2". Literal is larger than the type.

View File

@ -0,0 +1,20 @@
contract C {
function f() public pure {
bytes1 b1 = bytes1(hex"");
bytes1 b2 = bytes1(hex"1234");
bytes2 b3 = bytes2(hex"12");
bytes2 b4 = bytes2(hex"1234");
bytes2 b5 = bytes2(hex"123456");
bytes3 b6 = bytes3(hex"1234");
bytes3 b7 = bytes3(hex"123456");
bytes3 b8 = bytes3(hex"12345678");
bytes4 b9 = bytes4(hex"123456");
bytes4 b10 = bytes4(hex"12345678");
bytes4 b11 = bytes4(hex"1234567890");
}
}
// ----
// TypeError 9640: (92-109): Explicit type conversion not allowed from "literal_string "4"" to "bytes1". Literal is larger than the type.
// TypeError 9640: (198-217): Explicit type conversion not allowed from "literal_string "4V"" to "bytes2". Literal is larger than the type.
// TypeError 9640: (310-331): Explicit type conversion not allowed from "literal_string "4Vx"" to "bytes3". Literal is larger than the type.
// TypeError 9640: (430-453): Explicit type conversion not allowed from "literal_string (contains invalid UTF-8 sequence at position 4)" to "bytes4". Literal is larger than the type.

View File

@ -0,0 +1,13 @@
contract C {
function f() public pure {
bytes1 b1 = bytes1(hex"01");
bytes1 b2 = bytes1(hex"FF");
bytes2 b3 = bytes2(hex"0100");
bytes2 b4 = bytes2(hex"FFFF");
bytes3 b5 = bytes3(hex"010000");
bytes3 b6 = bytes3(hex"FFFFFF");
bytes4 b7 = bytes4(hex"01000000");
bytes4 b8 = bytes4(hex"FFFFFFFF");
b1; b2; b3; b4; b5; b6; b7; b8;
}
}

View File

@ -0,0 +1,13 @@
contract C {
function f() public pure {
bytes1 b1 = hex"01";
bytes1 b2 = hex"FF";
bytes2 b3 = hex"0100";
bytes2 b4 = hex"FFFF";
bytes3 b5 = hex"010000";
bytes3 b6 = hex"FFFFFF";
bytes4 b7 = hex"01000000";
bytes4 b8 = hex"FFFFFFFF";
b1; b2; b3; b4; b5; b6; b7; b8;
}
}