loader: Make error_code in evmc_load_and_create() optional

This commit is contained in:
Paweł Bylica 2019-06-03 14:35:28 +02:00
parent 9510206aa1
commit 81db59cede
No known key found for this signature in database
GPG Key ID: 7A0C037434FE77EF
2 changed files with 25 additions and 7 deletions

View File

@ -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;
}

View File

@ -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);
}