Merge pull request #11581 from ethereum/fix-crash-on-empty-string-in-bytes-concat

Fix `bytes.concat("")`
This commit is contained in:
chriseth 2021-06-29 15:02:26 +02:00 committed by GitHub
commit ccad22bafa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 2 deletions

View File

@ -10,6 +10,7 @@ Compiler Features:
Bugfixes:
* Code Generator: Fix crash when passing an empty string literal to ``bytes.concat()``.
* Code Generator: Fix internal compiler error when calling functions bound to calldata structs and arrays.
* Code Generator: Fix internal compiler error when passing zero to ``bytes.concat()``.
* Type Checker: Fix internal error and prevent static calls to unimplemented modifiers.

View File

@ -1083,7 +1083,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
targetTypes.emplace_back(argument->annotation().type);
else if (
auto const* literalType = dynamic_cast<StringLiteralType const*>(argument->annotation().type);
literalType && literalType->value().size() <= 32
literalType && !literalType->value().empty() && literalType->value().size() <= 32
)
targetTypes.emplace_back(TypeProvider::fixedBytes(static_cast<unsigned>(literalType->value().size())));
else if (auto const* literalType = dynamic_cast<RationalNumberType const*>(argument->annotation().type))

View File

@ -2473,7 +2473,7 @@ string YulUtilFunctions::bytesConcatFunction(vector<Type const*> const& _argumen
targetTypes.emplace_back(argumentType);
else if (
auto const* literalType = dynamic_cast<StringLiteralType const*>(argumentType);
literalType && literalType->value().size() <= 32
literalType && !literalType->value().empty() && literalType->value().size() <= 32
)
targetTypes.emplace_back(TypeProvider::fixedBytes(static_cast<unsigned>(literalType->value().size())));
else if (auto const* literalType = dynamic_cast<RationalNumberType const*>(argumentType))

View File

@ -0,0 +1,27 @@
contract C {
function f() public returns (bytes memory) {
bytes memory b = "";
return bytes.concat(
bytes.concat(b),
bytes.concat(b, b),
bytes.concat("", b),
bytes.concat(b, "")
);
}
function g() public returns (bytes memory) {
return bytes.concat("", "abc", hex"", "abc", unicode"");
}
function h() public returns (bytes memory) {
bytes memory b = "";
return bytes.concat(b, "abc", b, "abc", b);
}
}
// ====
// compileToEwasm: also
// compileViaYul: also
// ----
// f() -> 0x20, 0
// g() -> 0x20, 6, "abcabc"
// h() -> 0x20, 6, "abcabc"

View File

@ -0,0 +1,6 @@
contract C {
function f() public pure {
bytes.concat(hex"", unicode"", "");
}
}
// ----