Skip to content

Commit 7f81d8f

Browse files
committed
Assert against overflow in header.data_offset
1 parent 7ad882c commit 7f81d8f

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
@@ -670,18 +670,18 @@ TEST_F(state_transition, eofcreate_not_enough_gas_for_initcode_charge)
670670

671671
const auto init_code = returncontract(0, 0, 0);
672672
auto init_container = eof_bytecode(init_code, 2).container(deploy_container);
673-
// add max size data
674-
const auto init_data =
675-
bytes(std::numeric_limits<uint16_t>::max() - bytecode(init_container).size(), 0);
676-
init_container.data(init_data);
677-
EXPECT_EQ(bytecode(init_container).size(), std::numeric_limits<uint16_t>::max());
673+
const uint16_t init_data_size = std::numeric_limits<uint16_t>::max() / 2 -
674+
static_cast<uint16_t>(bytecode(init_container).size());
675+
const auto init_data = bytes(init_data_size, 0);
676+
init_container.data(init_data, init_data_size);
677+
EXPECT_EQ(bytecode(init_container).size(), std::numeric_limits<uint16_t>::max() / 2);
678678

679679
const auto factory_code = sstore(0, eofcreate().container(0).salt(Salt)) + OP_STOP;
680680
const auto factory_container = eof_bytecode(factory_code, 4).container(init_container);
681681

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

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

0 commit comments

Comments
 (0)