Ensure compliant init options API implementations. (#200)

Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>
This commit is contained in:
Michel Hidalgo 2020-06-24 17:50:46 -03:00 committed by Alejandro Hernández Cordero
parent cab4b81278
commit f7fcf88bb4

View file

@ -448,6 +448,10 @@ extern "C" rmw_ret_t rmw_init_options_copy(const rmw_init_options_t * src, rmw_i
{ {
RMW_CHECK_ARGUMENT_FOR_NULL(src, RMW_RET_INVALID_ARGUMENT); RMW_CHECK_ARGUMENT_FOR_NULL(src, RMW_RET_INVALID_ARGUMENT);
RMW_CHECK_ARGUMENT_FOR_NULL(dst, RMW_RET_INVALID_ARGUMENT); RMW_CHECK_ARGUMENT_FOR_NULL(dst, RMW_RET_INVALID_ARGUMENT);
if (NULL == src->implementation_identifier) {
RMW_SET_ERROR_MSG("expected initialized dst");
return RMW_RET_INVALID_ARGUMENT;
}
RMW_CHECK_TYPE_IDENTIFIERS_MATCH( RMW_CHECK_TYPE_IDENTIFIERS_MATCH(
src, src,
src->implementation_identifier, src->implementation_identifier,
@ -457,40 +461,43 @@ extern "C" rmw_ret_t rmw_init_options_copy(const rmw_init_options_t * src, rmw_i
RMW_SET_ERROR_MSG("expected zero-initialized dst"); RMW_SET_ERROR_MSG("expected zero-initialized dst");
return RMW_RET_INVALID_ARGUMENT; return RMW_RET_INVALID_ARGUMENT;
} }
const rcutils_allocator_t * allocator = &src->allocator; const rcutils_allocator_t * allocator = &src->allocator;
rmw_ret_t ret = RMW_RET_OK;
allocator->deallocate(dst->enclave, allocator->state); rmw_init_options_t tmp = *src;
*dst = *src; tmp.enclave = rcutils_strdup(tmp.enclave, *allocator);
dst->enclave = NULL; if (NULL != src->enclave && NULL == tmp.enclave) {
dst->security_options = rmw_get_zero_initialized_security_options(); return RMW_RET_BAD_ALLOC;
dst->enclave = rcutils_strdup(src->enclave, *allocator);
if (src->enclave && !dst->enclave) {
ret = RMW_RET_BAD_ALLOC;
goto fail;
} }
return rmw_security_options_copy(&src->security_options, allocator, &dst->security_options); tmp.security_options = rmw_get_zero_initialized_security_options();
fail: rmw_ret_t ret =
allocator->deallocate(dst->enclave, allocator->state); rmw_security_options_copy(&src->security_options, allocator, &tmp.security_options);
if (RMW_RET_OK != ret) {
allocator->deallocate(tmp.enclave, allocator->state);
return ret; return ret;
} }
*dst = tmp;
return RMW_RET_OK;
}
extern "C" rmw_ret_t rmw_init_options_fini(rmw_init_options_t * init_options) extern "C" rmw_ret_t rmw_init_options_fini(rmw_init_options_t * init_options)
{ {
RMW_CHECK_ARGUMENT_FOR_NULL(init_options, RMW_RET_INVALID_ARGUMENT); RMW_CHECK_ARGUMENT_FOR_NULL(init_options, RMW_RET_INVALID_ARGUMENT);
rcutils_allocator_t & allocator = init_options->allocator; if (NULL == init_options->implementation_identifier) {
RCUTILS_CHECK_ALLOCATOR(&allocator, return RMW_RET_INVALID_ARGUMENT); RMW_SET_ERROR_MSG("expected initialized init_options");
return RMW_RET_INVALID_ARGUMENT;
}
RMW_CHECK_TYPE_IDENTIFIERS_MATCH( RMW_CHECK_TYPE_IDENTIFIERS_MATCH(
init_options, init_options,
init_options->implementation_identifier, init_options->implementation_identifier,
eclipse_cyclonedds_identifier, eclipse_cyclonedds_identifier,
return RMW_RET_INCORRECT_RMW_IMPLEMENTATION); return RMW_RET_INCORRECT_RMW_IMPLEMENTATION);
allocator.deallocate(init_options->enclave, allocator.state); rcutils_allocator_t * allocator = &init_options->allocator;
rmw_security_options_fini(&init_options->security_options, &allocator); RCUTILS_CHECK_ALLOCATOR(allocator, return RMW_RET_INVALID_ARGUMENT);
allocator->deallocate(init_options->enclave, allocator->state);
rmw_ret_t ret = rmw_security_options_fini(&init_options->security_options, allocator);
*init_options = rmw_get_zero_initialized_init_options(); *init_options = rmw_get_zero_initialized_init_options();
return RMW_RET_OK; return ret;
} }
static void convert_guid_to_gid(const dds_guid_t & guid, rmw_gid_t & gid) static void convert_guid_to_gid(const dds_guid_t & guid, rmw_gid_t & gid)