From 81db59cede352b88583df3edd52ec4ee8ca462b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Mon, 3 Jun 2019 14:35:28 +0200 Subject: [PATCH] loader: Make error_code in evmc_load_and_create() optional --- lib/loader/loader.c | 21 ++++++++++++++------- test/unittests/test_loader.cpp | 11 +++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/loader/loader.c b/lib/loader/loader.c index 72b2afa..51d7a85 100644 --- a/lib/loader/loader.c +++ b/lib/loader/loader.c @@ -212,22 +212,29 @@ struct evmc_instance* evmc_load_and_create(const char* filename, if (!create_fn) return NULL; + enum evmc_loader_error_code ec = EVMC_LOADER_SUCCESS; + struct evmc_instance* instance = create_fn(); if (!instance) { - *error_code = set_error(EVMC_LOADER_INSTANCE_CREATION_FAILURE, - "creating EVMC instance of %s has failed", filename); - return NULL; + ec = set_error(EVMC_LOADER_INSTANCE_CREATION_FAILURE, + "creating EVMC instance of %s has failed", filename); + goto exit; } if (!evmc_is_abi_compatible(instance)) { + ec = set_error(EVMC_LOADER_ABI_VERSION_MISMATCH, + "EVMC ABI version %d of %s mismatches the expected version %d", + instance->abi_version, filename, EVMC_ABI_VERSION); evmc_destroy(instance); - *error_code = set_error(EVMC_LOADER_ABI_VERSION_MISMATCH, - "EVMC ABI version %d of %s mismatches the expected version %d", - instance->abi_version, filename, EVMC_ABI_VERSION); - return NULL; + instance = NULL; + goto exit; } +exit: + if (error_code) + *error_code = ec; + return instance; } diff --git a/test/unittests/test_loader.cpp b/test/unittests/test_loader.cpp index 010cbd6..903fc35 100644 --- a/test/unittests/test_loader.cpp +++ b/test/unittests/test_loader.cpp @@ -301,6 +301,11 @@ TEST_F(loader, load_and_create_failure) EXPECT_TRUE(vm == nullptr); EXPECT_EQ(ec, EVMC_LOADER_INSTANCE_CREATION_FAILURE); EXPECT_STREQ(evmc_last_error_msg(), "creating EVMC instance of failure.vm has failed"); + EXPECT_TRUE(evmc_last_error_msg() == nullptr); + + vm = evmc_load_and_create(evmc_test_library_path, nullptr); + EXPECT_TRUE(vm == nullptr); + EXPECT_STREQ(evmc_last_error_msg(), "creating EVMC instance of failure.vm has failed"); } TEST_F(loader, load_and_create_abi_mismatch) @@ -315,5 +320,11 @@ TEST_F(loader, load_and_create_abi_mismatch) "EVMC ABI version 1985 of abi1985.vm mismatches the expected version " + std::to_string(EVMC_ABI_VERSION); EXPECT_EQ(evmc_last_error_msg(), expected_error_msg); + EXPECT_TRUE(evmc_last_error_msg() == nullptr); + EXPECT_EQ(destroy_count, create_count); + + vm = evmc_load_and_create(evmc_test_library_path, nullptr); + EXPECT_TRUE(vm == nullptr); + EXPECT_EQ(evmc_last_error_msg(), expected_error_msg); EXPECT_EQ(destroy_count, create_count); }