Skip to content

Commit 30335c1

Browse files
authored
Merge pull request #13001 from matheusaaguiar/develop
Don't add an empty data slot to ABI-encoded empty strings from storage
2 parents 02567fd + 33ea6a6 commit 30335c1

File tree

10 files changed

+110
-9
lines changed

10 files changed

+110
-9
lines changed

Changelog.md

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

99

1010
Bugfixes:
11+
* ABI Encoder: When encoding an empty string coming from storage do not add a superfluous empty slot for data.
1112

1213

1314
### 0.8.14 (2022-05-17)

libsolidity/codegen/ABIFunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ string ABIFunctions::abiEncodingFunctionCompactStorageArray(
699699
case 0 {
700700
// short byte array
701701
mstore(pos, and(slotValue, not(0xff)))
702-
ret := add(pos, <lengthPaddedShort>)
702+
ret := add(pos, mul(<lengthPaddedShort>, iszero(iszero(length))))
703703
}
704704
case 1 {
705705
// long byte array

test/libsolidity/semanticTests/array/copying/bytes_storage_to_storage.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ contract c {
1717
// ----
1818
// f(uint256): 0 -> 0x20, 0x00
1919
// f(uint256): 31 -> 0x20, 0x1f, 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e00
20-
// gas irOptimized: 121735
20+
// gas irOptimized: 121752
2121
// gas legacy: 123884
2222
// gas legacyOptimized: 119139
2323
// f(uint256): 32 -> 0x20, 0x20, 1780731860627700044960722568376592200742329637303199754547598369979440671

test/libsolidity/semanticTests/array/copying/copy_byte_array_in_struct_to_storage.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ contract C {
3535
}
3636
// ----
3737
// f() -> 0x40, 0x80, 6, 0x6162636465660000000000000000000000000000000000000000000000000000, 0x49, 0x3132333435363738393031323334353637383930313233343536373839303120, 0x3132333435363738393031323334353637383930313233343536373839303120, 0x3132333435363738390000000000000000000000000000000000000000000000
38-
// gas irOptimized: 179895
38+
// gas irOptimized: 179912
3939
// gas legacy: 180676
4040
// gas legacyOptimized: 180070
4141
// g() -> 0x40, 0xc0, 0x49, 0x3132333435363738393031323334353637383930313233343536373839303120, 0x3132333435363738393031323334353637383930313233343536373839303120, 0x3132333435363738390000000000000000000000000000000000000000000000, 0x11, 0x3132333435363738393233343536373839000000000000000000000000000000
42-
// gas irOptimized: 107274
42+
// gas irOptimized: 107291
4343
// gas legacy: 107877
4444
// gas legacyOptimized: 107236
4545
// h() -> 0x40, 0x60, 0x00, 0x00

test/libsolidity/semanticTests/array/copying/storage_memory_nested_bytes.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ contract C {
1111
}
1212
// ----
1313
// f() -> 0x20, 0x02, 0x40, 0x80, 3, 0x6162630000000000000000000000000000000000000000000000000000000000, 0x99, 44048183304486788312148433451363384677562265908331949128489393215789685032262, 32241931068525137014058842823026578386641954854143559838526554899205067598957, 49951309422467613961193228765530489307475214998374779756599339590522149884499, 0x54555658595a6162636465666768696a6b6c6d6e6f707172737475767778797a, 0x4142434445464748494a4b4c4d4e4f5051525354555658595a00000000000000
14-
// gas irOptimized: 202840
14+
// gas irOptimized: 202864
1515
// gas legacy: 204441
1616
// gas legacyOptimized: 203419

test/libsolidity/semanticTests/array/pop/byte_array_pop_copy_long.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ contract c {
1010

1111
// ----
1212
// test() -> 0x20, 29, 0x0303030303030303030303030303030303030303030303030303030303000000
13-
// gas irOptimized: 109499
13+
// gas irOptimized: 109515
1414
// gas legacy: 126728
1515
// gas legacyOptimized: 123444

test/libsolidity/semanticTests/constructor/bytes_in_constructors_unpacker.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ contract Test {
88
}
99
// ----
1010
// constructor(): 7, 0x40, 78, "abcdefghijklmnopqrstuvwxyzabcdef", "ghijklmnopqrstuvwxyzabcdefghijkl", "mnopqrstuvwxyz" ->
11-
// gas irOptimized: 283829
11+
// gas irOptimized: 284909
1212
// gas legacy: 309607
1313
// gas legacyOptimized: 260566
1414
// m_x() -> 7
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
contract C {
2+
3+
string uninitializedString;
4+
string emptyString = "";
5+
string nonEmptyString = "This is a non empty string";
6+
string nonEmptyString2 = "Another string";
7+
bytes uninitializedBytes;
8+
bytes emptyBytes = "";
9+
error EmptyError(string);
10+
event EmptyEvent(string);
11+
12+
function f() public returns (string memory) {
13+
return uninitializedString;
14+
}
15+
16+
function g() public returns (string memory, string memory) {
17+
return (uninitializedString, emptyString);
18+
}
19+
20+
function h() public returns (string memory, string memory) {
21+
return (uninitializedString, nonEmptyString);
22+
}
23+
24+
function i() public returns (string memory, string memory) {
25+
return (nonEmptyString, emptyString);
26+
}
27+
28+
function j(string calldata _s) public returns (string memory) {
29+
return _s;
30+
}
31+
32+
function k() public returns (string memory) {
33+
nonEmptyString2 = "";
34+
return nonEmptyString2;
35+
}
36+
37+
function l(string calldata _s) public returns (bytes memory) {
38+
return abi.encode(_s);
39+
}
40+
41+
function m() public returns (string memory) {
42+
bytes memory b = abi.encode(emptyString);
43+
return string(b);
44+
}
45+
46+
function n() public {
47+
revert EmptyError(uninitializedString);
48+
}
49+
50+
function o() public {
51+
emit EmptyEvent(emptyString);
52+
}
53+
54+
function p() public {
55+
emit EmptyEvent("");
56+
}
57+
58+
function q() public returns (bytes memory) {
59+
return uninitializedBytes;
60+
}
61+
62+
function r() public returns (bytes memory) {
63+
emptyBytes = abi.encode("");
64+
return emptyBytes;
65+
}
66+
67+
function s() public returns (bytes memory) {
68+
emptyBytes = abi.encode(uninitializedString);
69+
return emptyBytes;
70+
}
71+
72+
function set(string calldata _s) public {
73+
nonEmptyString = _s;
74+
}
75+
76+
function get() public returns (string memory) {
77+
return nonEmptyString;
78+
}
79+
}
80+
// ====
81+
// compileViaYul: also
82+
// ----
83+
// f() -> 0x20, 0
84+
// g() -> 0x40, 0x60, 0, 0
85+
// h() -> 0x40, 0x60, 0, 0x1a, 38178759162904981154304545770567765692299154484752076569098748838215919075328
86+
// i() -> 0x40, 0x80, 0x1a, 38178759162904981154304545770567765692299154484752076569098748838215919075328, 0
87+
// j(string): 0x20, 0, "" -> 0x20, 0
88+
// k() -> 0x20, 0
89+
// l(string): 0x20, 0, "" -> 0x20, 0x40, 0x20, 0
90+
// m() -> 0x20, 0x40, 0x20, 0
91+
// n() -> FAILURE, hex"d3f13430", hex"0000000000000000000000000000000000000000000000000000000000000020", hex"0000000000000000000000000000000000000000000000000000000000000000"
92+
// o() ->
93+
// ~ emit EmptyEvent(string): 0x20, 0x00
94+
// p() ->
95+
// ~ emit EmptyEvent(string): 0x20, 0x00
96+
// q() -> 0x20, 0
97+
// r() -> 0x20, 0x40, 0x20, 0
98+
// s() -> 0x20, 0x40, 0x20, 0
99+
// set(string): 0x20, 0, "" ->
100+
// get() -> 0x20, 0

test/libsolidity/semanticTests/various/address_code.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ contract C {
1616
// compileToEwasm: also
1717
// ----
1818
// constructor() ->
19-
// gas irOptimized: 192317
19+
// gas irOptimized: 193397
2020
// gas legacy: 240889
2121
// gas legacyOptimized: 155314
2222
// initCode() -> 0x20, 0

test/libsolidity/semanticTests/various/skip_dynamic_types_for_structs.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ contract C {
2020

2121
// ----
2222
// g() -> 2, 6
23-
// gas irOptimized: 178805
23+
// gas irOptimized: 178822
2424
// gas legacy: 180753
2525
// gas legacyOptimized: 179472

0 commit comments

Comments
 (0)