Skip to content

Commit

Permalink
silkpre: be defensive
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Jul 28, 2023
1 parent 1717844 commit 0fdcdfd
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions test/state/precompiles_silkpre.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ namespace
ExecutionResult execute(const uint8_t* input, size_t input_size, uint8_t* output_buf,
[[maybe_unused]] size_t max_output_size, PrecompileId id) noexcept
{
const auto index = static_cast<size_t>(id) - 1;
const auto index = stdx::to_underlying(id) - 1;
const auto [output, output_size] = kSilkpreContracts[index].run(input, input_size);
if (output == nullptr)
return {EVMC_PRECOMPILE_FAILURE, 0};

// Check if max_output_size computed by analysis match the computed result.
assert(output_size <= max_output_size);
std::memcpy(output_buf, output, output_size);
std::free(output);
return {EVMC_SUCCESS, output_size};

auto trimmed_output_size = std::min(output_size, max_output_size);
std::memcpy(output_buf, output, trimmed_output_size);
std::free(output); // Free output allocation (required by silkpre API).
return {EVMC_SUCCESS, trimmed_output_size};
}
} // namespace

Expand Down

0 comments on commit 0fdcdfd

Please sign in to comment.