From 2ca88aca077f03d0a4b49385562840c7ee772e7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 9 May 2019 21:47:22 +0200 Subject: [PATCH 1/2] docs: Update docs of evmc_result::release --- include/evmc/evmc.h | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/include/evmc/evmc.h b/include/evmc/evmc.h index 4c569ac..15c75ab 100644 --- a/include/evmc/evmc.h +++ b/include/evmc/evmc.h @@ -362,25 +362,23 @@ struct evmc_result size_t output_size; /** - * The pointer to a function releasing all resources associated with - * the result object. + * The method releasing all resources associated with the result object. * - * This function pointer is optional (MAY be NULL) and MAY be set by - * the EVM implementation. If set it MUST be used by the user to - * release memory and other resources associated with the result object. - * After the result resources are released the result object - * MUST NOT be used any more. + * This method (function pointer) is optional (MAY be NULL) and MAY be set + * by the VM implementation. If set it MUST be called by the user once to + * release memory and other resources associated with the result object. + * Once the resources are released the result object MUST NOT be used again. * - * The suggested code pattern for releasing EVM results: - * @code - * struct evmc_result result = ...; - * if (result.release) - * result.release(&result); - * @endcode + * The suggested code pattern for releasing execution results: + * @code + * struct evmc_result result = ...; + * if (result.release) + * result.release(&result); + * @endcode * - * @note - * It works similarly to C++ virtual destructor. Attaching the release - * function to the result itself allows EVM composition. + * @note + * It works similarly to C++ virtual destructor. Attaching the release + * function to the result itself allows VM composition. */ evmc_release_result_fn release; From 3b834de0957ef7cc9b5709d89f4d38ee2d2dd0d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 9 May 2019 21:51:42 +0200 Subject: [PATCH 2/2] helpers: Handle results with null release() method --- include/evmc/helpers.h | 7 +++++-- test/unittests/test_helpers.cpp | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/evmc/helpers.h b/include/evmc/helpers.h index 9273e2b..e43cfd7 100644 --- a/include/evmc/helpers.h +++ b/include/evmc/helpers.h @@ -109,11 +109,14 @@ static inline struct evmc_result evmc_execute(struct evmc_instance* instance, /** * Releases the resources allocated to the execution result. * - * @see evmc_release_result_fn + * @param result The result object to be released. MUST NOT be NULL. + * + * @see evmc_result::release() evmc_release_result_fn */ static inline void evmc_release_result(struct evmc_result* result) { - result->release(result); + if (result->release) + result->release(result); } diff --git a/test/unittests/test_helpers.cpp b/test/unittests/test_helpers.cpp index 82d8195..504025a 100644 --- a/test/unittests/test_helpers.cpp +++ b/test/unittests/test_helpers.cpp @@ -61,3 +61,19 @@ TEST(helpers, is_zero) b.bytes[0] = 1; EXPECT_FALSE(is_zero(b)); } + +TEST(helpers, release_result) +{ + auto r1 = evmc_result{}; + evmc_release_result(&r1); + + static evmc_result r2; + static bool e; + + e = false; + r2 = evmc_result{}; + r2.release = [](const evmc_result* r) { e = r == &r2; }; + EXPECT_FALSE(e); + evmc_release_result(&r2); + EXPECT_TRUE(e); +}