Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid potential overflow in num_words #945

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/evmone/instructions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ constexpr auto word_size = 32;
/// i.e. it rounds up the number bytes to number of words.
inline constexpr int64_t num_words(uint64_t size_in_bytes) noexcept
{
return static_cast<int64_t>((size_in_bytes + (word_size - 1)) / word_size);
return static_cast<int64_t>(
size_in_bytes / word_size + static_cast<uint64_t>(size_in_bytes % word_size != 0));
}

/// Computes gas cost of copying the given amount of bytes to/from EVM memory.
Expand Down
2 changes: 1 addition & 1 deletion test/state/precompiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ constexpr auto GasCostMax = std::numeric_limits<int64_t>::max();

inline constexpr int64_t num_words(size_t size_in_bytes) noexcept
{
return static_cast<int64_t>((size_in_bytes + 31) / 32);
return static_cast<int64_t>(size_in_bytes / 32 + static_cast<size_t>(size_in_bytes % 32 != 0));
}

template <int BaseCost, int WordCost>
Expand Down
2 changes: 1 addition & 1 deletion test/state/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ constexpr auto MAX_INITCODE_COUNT = 256;

inline constexpr int64_t num_words(size_t size_in_bytes) noexcept
{
return static_cast<int64_t>((size_in_bytes + 31) / 32);
return static_cast<int64_t>(size_in_bytes / 32 + static_cast<size_t>(size_in_bytes % 32 != 0));
}

int64_t compute_tx_data_cost(evmc_revision rev, bytes_view data) noexcept
Expand Down
15 changes: 15 additions & 0 deletions test/unittests/instructions_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,18 @@ TEST(instructions, compare_with_evmc_instruction_names)
EXPECT_STREQ(instr::traits[i].name, evmc_tbl[i]);
}
}


TEST(instructions, num_words)
{
EXPECT_EQ(num_words(0), 0);
EXPECT_EQ(num_words(1), 1);
EXPECT_EQ(num_words(31), 1);
EXPECT_EQ(num_words(32), 1);
EXPECT_EQ(num_words(33), 2);
EXPECT_EQ(num_words(0xFFFFFFFFFFFFFFDF), 0x7FFFFFFFFFFFFFF);
EXPECT_EQ(num_words(0xFFFFFFFFFFFFFFE0), 0x7FFFFFFFFFFFFFF);
EXPECT_EQ(num_words(0xFFFFFFFFFFFFFFE1), 0x800000000000000);
EXPECT_EQ(num_words(0xFFFFFFFFFFFFFFFE), 0x800000000000000);
EXPECT_EQ(num_words(0xFFFFFFFFFFFFFFFF), 0x800000000000000);
}