Skip to content

Commit ccad22b

Browse files
authored
Merge pull request #11581 from ethereum/fix-crash-on-empty-string-in-bytes-concat
Fix `bytes.concat("")`
2 parents 7bce83e + fa36968 commit ccad22b

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Compiler Features:
1010

1111

1212
Bugfixes:
13+
* Code Generator: Fix crash when passing an empty string literal to ``bytes.concat()``.
1314
* Code Generator: Fix internal compiler error when calling functions bound to calldata structs and arrays.
1415
* Code Generator: Fix internal compiler error when passing zero to ``bytes.concat()``.
1516
* Type Checker: Fix internal error and prevent static calls to unimplemented modifiers.

libsolidity/codegen/ExpressionCompiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
10831083
targetTypes.emplace_back(argument->annotation().type);
10841084
else if (
10851085
auto const* literalType = dynamic_cast<StringLiteralType const*>(argument->annotation().type);
1086-
literalType && literalType->value().size() <= 32
1086+
literalType && !literalType->value().empty() && literalType->value().size() <= 32
10871087
)
10881088
targetTypes.emplace_back(TypeProvider::fixedBytes(static_cast<unsigned>(literalType->value().size())));
10891089
else if (auto const* literalType = dynamic_cast<RationalNumberType const*>(argument->annotation().type))

libsolidity/codegen/YulUtilFunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2473,7 +2473,7 @@ string YulUtilFunctions::bytesConcatFunction(vector<Type const*> const& _argumen
24732473
targetTypes.emplace_back(argumentType);
24742474
else if (
24752475
auto const* literalType = dynamic_cast<StringLiteralType const*>(argumentType);
2476-
literalType && literalType->value().size() <= 32
2476+
literalType && !literalType->value().empty() && literalType->value().size() <= 32
24772477
)
24782478
targetTypes.emplace_back(TypeProvider::fixedBytes(static_cast<unsigned>(literalType->value().size())));
24792479
else if (auto const* literalType = dynamic_cast<RationalNumberType const*>(argumentType))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
contract C {
2+
function f() public returns (bytes memory) {
3+
bytes memory b = "";
4+
return bytes.concat(
5+
bytes.concat(b),
6+
bytes.concat(b, b),
7+
bytes.concat("", b),
8+
bytes.concat(b, "")
9+
);
10+
}
11+
12+
function g() public returns (bytes memory) {
13+
return bytes.concat("", "abc", hex"", "abc", unicode"");
14+
}
15+
16+
function h() public returns (bytes memory) {
17+
bytes memory b = "";
18+
return bytes.concat(b, "abc", b, "abc", b);
19+
}
20+
}
21+
// ====
22+
// compileToEwasm: also
23+
// compileViaYul: also
24+
// ----
25+
// f() -> 0x20, 0
26+
// g() -> 0x20, 6, "abcabc"
27+
// h() -> 0x20, 6, "abcabc"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
contract C {
2+
function f() public pure {
3+
bytes.concat(hex"", unicode"", "");
4+
}
5+
}
6+
// ----

0 commit comments

Comments
 (0)