diff --git a/rcl/include/rcl/error_handling.h b/rcl/include/rcl/error_handling.h index f8b09d0392..471a527833 100644 --- a/rcl/include/rcl/error_handling.h +++ b/rcl/include/rcl/error_handling.h @@ -45,6 +45,9 @@ typedef rcutils_error_state_t rcl_error_state_t; #define RCL_SET_ERROR_MSG(msg, allocator) RCUTILS_SET_ERROR_MSG(msg, allocator) +#define RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(allocator, fmt_str, ...) \ + RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(allocator, fmt_str, __VA_ARGS__) + #define rcl_error_is_set rcutils_error_is_set #define rcl_get_error_state rcutils_get_error_state diff --git a/rcl/src/rcl/expand_topic_name.c b/rcl/src/rcl/expand_topic_name.c index 22b0628506..bd59ae0ceb 100644 --- a/rcl/src/rcl/expand_topic_name.c +++ b/rcl/src/rcl/expand_topic_name.c @@ -166,20 +166,14 @@ rcl_expand_topic_name( *output_topic_name = NULL; char * unmatched_substitution = rcutils_strndup(next_opening_brace, substitution_substr_len, allocator); - char * allocated_msg = NULL; - char * msg = NULL; if (unmatched_substitution) { - allocated_msg = rcutils_format_string( + RCL_SET_ERROR_MSG_WITH_FORMAT_STRING( allocator, "unknown substitution: %s", unmatched_substitution); - msg = allocated_msg; } else { - RCUTILS_SAFE_FWRITE_TO_STDERR("failed to allocate memory for error message\n"); - msg = "unknown substitution: allocation failed when reporting error"; + RCUTILS_SAFE_FWRITE_TO_STDERR("failed to allocate memory for unmatched substitution\n"); } - RCL_SET_ERROR_MSG(msg, allocator) allocator.deallocate(unmatched_substitution, allocator.state); - allocator.deallocate(allocated_msg, allocator.state); allocator.deallocate(local_output, allocator.state); return RCL_RET_UNKNOWN_SUBSTITUTION; } diff --git a/rcl_lifecycle/src/transition_map.c b/rcl_lifecycle/src/transition_map.c index 20cb8b5970..99d9f3d975 100644 --- a/rcl_lifecycle/src/transition_map.c +++ b/rcl_lifecycle/src/transition_map.c @@ -77,9 +77,8 @@ rcl_lifecycle_register_state( const rcutils_allocator_t * allocator) { if (rcl_lifecycle_get_state(transition_map, state.id) != NULL) { - char * error_msg = rcutils_format_string(rcutils_get_default_allocator(), - "state %u is already registered\n", state.id); - RCL_SET_ERROR_MSG(error_msg, rcutils_get_default_allocator()); + RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(rcutils_get_default_allocator(), + "state %u is already registered\n", state.id); return RCL_RET_ERROR; } @@ -115,9 +114,8 @@ rcl_lifecycle_register_transition( rcl_lifecycle_state_t * state = rcl_lifecycle_get_state(transition_map, transition.start->id); if (!state) { - char * error_msg = rcutils_format_string(rcl_get_default_allocator(), - "state %u is not registered\n", transition.start->id); - RCL_SET_ERROR_MSG(error_msg, rcl_get_default_allocator()); + RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(rcl_get_default_allocator(), + "state %u is not registered\n", transition.start->id); return RCL_RET_ERROR; }