From db2e830da4089410108fd0c3164809879aa58717 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 30 Oct 2019 19:03:57 +0100 Subject: [PATCH] examples: Fix and update ExampleHost::copy_code() --- examples/example_host.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/example_host.cpp b/examples/example_host.cpp index 8707a40..08df029 100644 --- a/examples/example_host.cpp +++ b/examples/example_host.cpp @@ -10,6 +10,7 @@ #include +#include #include #include @@ -104,21 +105,20 @@ public: uint8_t* buffer_data, size_t buffer_size) noexcept final { - auto it = accounts.find(addr); + const auto it = accounts.find(addr); if (it == accounts.end()) return 0; - auto code = it->second.code; + + const auto& code = it->second.code; + if (code_offset >= code.size()) return 0; - // TODO: implement this more nicely? - auto begin = code.begin(); - auto end = code.end(); - std::advance(begin, static_cast(code_offset)); - auto len = std::distance(begin, end); - if (len > static_cast(buffer_size)) - std::advance(end, len - static_cast(buffer_size)); - std::copy(begin, end, buffer_data); - return static_cast(len); + + const auto n = std::min(buffer_size, code.size() - code_offset); + + if (n > 0) + std::copy_n(&code[code_offset], n, buffer_data); + return n; } void selfdestruct(const evmc::address& addr, const evmc::address& beneficiary) noexcept final