Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wait allocation cleanup #770

Merged
merged 3 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions rcl/src/rcl/wait.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ rcl_wait_set_is_valid(const rcl_wait_set_t * wait_set)
}

static void
__wait_set_clean_up(rcl_wait_set_t * wait_set, rcl_allocator_t allocator)
__wait_set_clean_up(rcl_wait_set_t * wait_set)
{
rcl_ret_t ret = rcl_wait_set_resize(wait_set, 0, 0, 0, 0, 0, 0);
(void)ret; // NO LINT
assert(RCL_RET_OK == ret); // Defensive, shouldn't fail with size 0.
if (wait_set->impl) {
allocator.deallocate(wait_set->impl, allocator.state);
wait_set->impl->allocator.deallocate(wait_set->impl, wait_set->impl->allocator.state);
wait_set->impl = NULL;
}
}
Expand Down Expand Up @@ -146,6 +146,10 @@ rcl_wait_set_init(
wait_set->impl->rmw_services.service_count = 0;
wait_set->impl->rmw_events.events = NULL;
wait_set->impl->rmw_events.event_count = 0;
// Set context.
wait_set->impl->context = context;
// Set allocator.
wait_set->impl->allocator = allocator;

size_t num_conditions =
(2 * number_of_subscriptions) +
Expand All @@ -159,10 +163,6 @@ rcl_wait_set_init(
goto fail;
}

// Set context.
wait_set->impl->context = context;
// Set allocator.
wait_set->impl->allocator = allocator;
// Initialize subscription space.
rcl_ret_t ret = rcl_wait_set_resize(
wait_set, number_of_subscriptions, number_of_guard_conditions, number_of_timers,
Expand All @@ -179,7 +179,7 @@ rcl_wait_set_init(
fail_ret = RCL_RET_WAIT_SET_INVALID;
}
}
__wait_set_clean_up(wait_set, allocator);
__wait_set_clean_up(wait_set);
return fail_ret;
}

Expand All @@ -195,7 +195,7 @@ rcl_wait_set_fini(rcl_wait_set_t * wait_set)
RCL_SET_ERROR_MSG(rmw_get_error_string().str);
result = RCL_RET_WAIT_SET_INVALID;
}
__wait_set_clean_up(wait_set, wait_set->impl->allocator);
__wait_set_clean_up(wait_set);
}
return result;
}
Expand Down Expand Up @@ -300,6 +300,7 @@ rcl_wait_set_get_allocator(const rcl_wait_set_t * wait_set, rcl_allocator_t * al
wait_set->impl->RMWStorage, sizeof(void *) * Type ## s_size, allocator.state); \
if (!wait_set->impl->RMWStorage) { \
allocator.deallocate((void *)wait_set->Type ## s, allocator.state); \
wait_set->Type ## s = NULL; \
wait_set->size_of_ ## Type ## s = 0; \
RCL_SET_ERROR_MSG("allocating memory failed"); \
return RCL_RET_BAD_ALLOC; \
Expand Down
29 changes: 29 additions & 0 deletions rcl/test/rcl/test_wait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,3 +684,32 @@ TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), wait_set_get_allocator
ret = rcl_wait_set_fini(&wait_set);
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
}

// Test proper error handling with a fault injection test
TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), wait_set_test_maybe_init_fail) {
rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set();
rcl_ret_t ret = RCL_RET_OK;
rcl_allocator_t alloc = rcl_get_default_allocator();

RCUTILS_FAULT_INJECTION_TEST(
{
ret = rcl_wait_set_init(&wait_set, 1, 0, 0, 1, 1, 0, context_ptr, alloc);
if (RCL_RET_OK == ret) {
EXPECT_EQ(RCL_RET_OK, rcl_wait_set_fini(&wait_set));
} else {
EXPECT_TRUE(RCL_RET_WAIT_SET_INVALID == ret || RCL_RET_BAD_ALLOC == ret);
rcl_reset_error();
}
});

RCUTILS_FAULT_INJECTION_TEST(
{
ret = rcl_wait_set_init(&wait_set, 1, 1, 1, 1, 1, 1, context_ptr, alloc);
if (RCL_RET_OK == ret) {
EXPECT_EQ(RCL_RET_OK, rcl_wait_set_fini(&wait_set));
} else {
EXPECT_TRUE(RCL_RET_WAIT_SET_INVALID == ret || RCL_RET_BAD_ALLOC == ret);
rcl_reset_error();
}
});
}