Skip to content

Commit 282f575

Browse files
pdobaczchfast
authored andcommitted
Assert against overflow in header.data_offset
1 parent 7f65121 commit 282f575

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

lib/evmone/eof.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ std::variant<EOF1Header, EOFValidationError> validate_header(
296296
container_offsets.emplace_back(static_cast<uint16_t>(offset));
297297
offset += container_size;
298298
}
299+
// NOTE: assertion always satisfied only as long as initcode limits apply (48K).
300+
assert(offset <= std::numeric_limits<uint16_t>::max());
299301
const auto data_offset = static_cast<uint16_t>(offset);
300302

301303
return EOF1Header{

test/unittests/eof_validation_test.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -1205,9 +1205,12 @@ TEST_F(eof_validation, EOF1_subcontainer_containing_unreachable_code_sections)
12051205

12061206
TEST_F(eof_validation, max_nested_containers)
12071207
{
1208-
bytecode code = eof_bytecode(OP_INVALID);
1209-
while (code.size() <= std::numeric_limits<uint16_t>::max())
1210-
code = eof_bytecode(OP_INVALID).container(code);
1211-
1208+
bytecode code{};
1209+
bytecode nextcode = eof_bytecode(OP_INVALID);
1210+
while (nextcode.size() <= std::numeric_limits<uint16_t>::max())
1211+
{
1212+
code = nextcode;
1213+
nextcode = eof_bytecode(OP_INVALID).container(nextcode);
1214+
}
12121215
add_test_case(code, EOFValidationError::success);
12131216
}

test/unittests/state_transition_eof_create_test.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -715,18 +715,18 @@ TEST_F(state_transition, eofcreate_not_enough_gas_for_initcode_charge)
715715

716716
const auto init_code = returncontract(0, 0, 0);
717717
auto init_container = eof_bytecode(init_code, 2).container(deploy_container);
718-
// add max size data
719-
const auto init_data =
720-
bytes(std::numeric_limits<uint16_t>::max() - bytecode(init_container).size(), 0);
721-
init_container.data(init_data);
722-
EXPECT_EQ(bytecode(init_container).size(), std::numeric_limits<uint16_t>::max());
718+
const uint16_t init_data_size = std::numeric_limits<uint16_t>::max() / 2 -
719+
static_cast<uint16_t>(bytecode(init_container).size());
720+
const auto init_data = bytes(init_data_size, 0);
721+
init_container.data(init_data, init_data_size);
722+
EXPECT_EQ(bytecode(init_container).size(), std::numeric_limits<uint16_t>::max() / 2);
723723

724724
const auto factory_code = sstore(0, eofcreate().container(0).salt(Salt)) + OP_STOP;
725725
const auto factory_container = eof_bytecode(factory_code, 4).container(init_container);
726726

727727
tx.to = To;
728728
// tx intrinsic cost + EOFCREATE cost + initcode charge - not enough for pushes before EOFCREATE
729-
tx.gas_limit = 21'000 + 32'000 + (std::numeric_limits<uint16_t>::max() + 31) / 32 * 6;
729+
tx.gas_limit = 21'000 + 32'000 + (std::numeric_limits<uint16_t>::max() / 2 + 31) / 32 * 6;
730730

731731
pre.insert(*tx.to, {.nonce = 1, .code = factory_container});
732732

0 commit comments

Comments
 (0)