diff --git a/test/state/precompiles_silkpre.cpp b/test/state/precompiles_silkpre.cpp index 76ea3719d3..cc77a935aa 100644 --- a/test/state/precompiles_silkpre.cpp +++ b/test/state/precompiles_silkpre.cpp @@ -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(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