Skip to content

Commit 4807f11

Browse files
committed
Use unordered_set for accessed code sections, update unit test
1 parent 7f1487c commit 4807f11

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

lib/evmone/eof.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <limits>
1414
#include <numeric>
1515
#include <ostream>
16-
#include <set>
16+
#include <unordered_set>
1717
#include <span>
1818
#include <stack>
1919
#include <variant>
@@ -215,7 +215,7 @@ std::variant<std::vector<EOFCodeType>, EOFValidationError> validate_types(
215215
}
216216

217217
EOFValidationError validate_instructions(evmc_revision rev, const EOF1Header& header,
218-
size_t code_idx, bytes_view container, std::set<uint16_t>& accessed_code_sections) noexcept
218+
size_t code_idx, bytes_view container, std::unordered_set<uint16_t>& accessed_code_sections) noexcept
219219
{
220220
const bytes_view code{header.get_code(container, code_idx)};
221221
assert(!code.empty()); // guaranteed by EOF headers validation
@@ -519,7 +519,7 @@ std::variant<EOF1Header, EOFValidationError> validate_eof1(
519519
offset += code_size;
520520
}
521521

522-
std::set<uint16_t> accessed_code_sections = {0};
522+
std::unordered_set<uint16_t> accessed_code_sections = {0};
523523
EOF1Header header{container[2], code_sizes, code_offsets, data_size, types};
524524

525525
for (size_t code_idx = 0; code_idx < header.code_sizes.size(); ++code_idx)

test/unittests/eof_validation_test.cpp

+13-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <test/utils/bytecode.hpp>
99
#include <test/utils/utils.hpp>
1010

11-
using namespace std;
1211
using namespace evmone;
1312

1413
namespace
@@ -313,18 +312,28 @@ TEST(eof_validation, EOF1_invalid_section_0_type)
313312
TEST(eof_validation, EOF1_too_many_code_sections)
314313
{
315314
std::string code_sections_1024;
316-
for (int i = 0; i < 1024; ++i)
315+
std::string code_sections_1025;
316+
for (int i = 0; i < 1025; ++i)
317317
if (i < 1023)
318+
{
318319
code_sections_1024 += "E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
319-
else
320+
code_sections_1025 += "E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
321+
}
322+
else if (i == 1023)
323+
{
320324
code_sections_1024 += "5B5B00";
325+
code_sections_1025 += "E5" + hex(big_endian(static_cast<uint16_t>(i + 1)));
326+
}
327+
else
328+
code_sections_1025 += "5B5B00";
329+
321330

322331
const auto valid = "EF0001 011000" + bytecode{"020400"} + 0x400 * bytecode{"0003"} +
323332
"040000 00" + 0x400 * bytecode{"00800000"} + code_sections_1024;
324333
EXPECT_EQ(validate_eof(valid), EOFValidationError::success);
325334

326335
const auto invalid = "EF0001 011002" + bytecode{"020401"} + 0x401 * bytecode{"0001"} +
327-
"040000 00" + 0x401 * bytecode{"00800000"} + 0x401 * bytecode{"FE"};
336+
"040000 00" + 0x401 * bytecode{"00800000"} + code_sections_1025;
328337
EXPECT_EQ(validate_eof(invalid), EOFValidationError::too_many_code_sections);
329338
}
330339

0 commit comments

Comments
 (0)