Skip to content

Commit

Permalink
Improve __rmw_create_wait_set() implementation. (#427)
Browse files Browse the repository at this point in the history
Handle errors consistently.

Signed-off-by: Michel Hidalgo <[email protected]>
  • Loading branch information
hidmic authored and ahcorde committed Oct 15, 2020
1 parent f13bbc8 commit 06f449d
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions rmw_fastrtps_shared_cpp/src/rmw_wait_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,34 @@ __rmw_create_wait_set(const char * identifier, rmw_context_t * context, size_t m
return nullptr);

(void)max_conditions;
rmw_wait_set_t * wait_set = rmw_wait_set_allocate();
CustomWaitsetInfo * wait_set_info = nullptr;

// From here onward, error results in unrolling in the goto fail block.
CustomWaitsetInfo * wait_set_info = nullptr;
rmw_wait_set_t * wait_set = rmw_wait_set_allocate();
if (!wait_set) {
RMW_SET_ERROR_MSG("failed to allocate wait set");
goto fail;
}
wait_set->implementation_identifier = identifier;
wait_set->data = rmw_allocate(sizeof(CustomWaitsetInfo));
// This should default-construct the fields of CustomWaitsetInfo
wait_set_info = static_cast<CustomWaitsetInfo *>(wait_set->data);
// cppcheck-suppress syntaxError
RMW_TRY_PLACEMENT_NEW(wait_set_info, wait_set_info, goto fail, CustomWaitsetInfo, )
if (!wait_set_info) {
RMW_SET_ERROR_MSG("failed to construct wait set info struct");
if (!wait_set->data) {
RMW_SET_ERROR_MSG("failed to allocate wait set info");
goto fail;
}
// This should default-construct the fields of CustomWaitsetInfo
RMW_TRY_PLACEMENT_NEW(
wait_set_info,
wait_set->data,
goto fail,
// cppcheck-suppress syntaxError
CustomWaitsetInfo, );
(void) wait_set_info;

return wait_set;

fail:
if (wait_set) {
if (wait_set->data) {
RMW_TRY_DESTRUCTOR_FROM_WITHIN_FAILURE(
wait_set_info->~CustomWaitsetInfo(), wait_set_info)
rmw_free(wait_set->data);
}
rmw_wait_set_free(wait_set);
Expand Down

0 comments on commit 06f449d

Please sign in to comment.