Skip to content

Commit

Permalink
Fix NULL allocator and racy condition. (#1188)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomoya Fujita <[email protected]>
  • Loading branch information
fujitatomoya committed Sep 21, 2024
1 parent bfe00f7 commit 3762d86
Show file tree
Hide file tree
Showing 19 changed files with 50 additions and 60 deletions.
7 changes: 3 additions & 4 deletions rcl/src/rcl/arguments.c
Original file line number Diff line number Diff line change
Expand Up @@ -797,10 +797,9 @@ rcl_arguments_get_unparsed_ros(
rcl_arguments_t
rcl_get_zero_initialized_arguments(void)
{
static rcl_arguments_t default_arguments = {
.impl = NULL
};
return default_arguments;
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_arguments_t zero_arguments;
return zero_arguments;
}

rcl_ret_t
Expand Down
5 changes: 3 additions & 2 deletions rcl/src/rcl/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ struct rcl_client_impl_s
rcl_client_t
rcl_get_zero_initialized_client(void)
{
static rcl_client_t null_client = {0};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_client_t null_client;
return null_client;
}

Expand Down Expand Up @@ -279,7 +280,7 @@ rcl_client_options_t
rcl_client_get_default_options(void)
{
// !!! MAKE SURE THAT CHANGES TO THESE DEFAULTS ARE REFLECTED IN THE HEADER DOC STRING
static rcl_client_options_t default_options;
rcl_client_options_t default_options;
// Must set the allocator and qos after because they are not a compile time constant.
default_options.qos = rmw_qos_profile_services_default;
default_options.allocator = rcl_get_default_allocator();
Expand Down
2 changes: 1 addition & 1 deletion rcl/src/rcl/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extern "C"
rcl_context_t
rcl_get_zero_initialized_context(void)
{
static rcl_context_t context = {
rcl_context_t context = {
.impl = NULL,
.instance_id_storage = {0},
};
Expand Down
3 changes: 2 additions & 1 deletion rcl/src/rcl/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ extern "C"
rcl_event_t
rcl_get_zero_initialized_event(void)
{
static rcl_event_t null_event = {0};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_event_t null_event;
return null_event;
}

Expand Down
8 changes: 3 additions & 5 deletions rcl/src/rcl/guard_condition.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ struct rcl_guard_condition_impl_s
rcl_guard_condition_t
rcl_get_zero_initialized_guard_condition(void)
{
static rcl_guard_condition_t null_guard_condition = {
.context = 0,
.impl = 0
};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_guard_condition_t null_guard_condition;
return null_guard_condition;
}

Expand Down Expand Up @@ -144,7 +142,7 @@ rcl_guard_condition_options_t
rcl_guard_condition_get_default_options(void)
{
// !!! MAKE SURE THAT CHANGES TO THESE DEFAULTS ARE REFLECTED IN THE HEADER DOC STRING
static rcl_guard_condition_options_t default_options;
rcl_guard_condition_options_t default_options;
default_options.allocator = rcl_get_default_allocator();
return default_options;
}
Expand Down
5 changes: 2 additions & 3 deletions rcl/src/rcl/lexer_lookahead.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ struct rcl_lexer_lookahead2_impl_s
rcl_lexer_lookahead2_t
rcl_get_zero_initialized_lexer_lookahead2(void)
{
static rcl_lexer_lookahead2_t zero_initialized = {
.impl = NULL,
};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_lexer_lookahead2_t zero_initialized;
return zero_initialized;
}

Expand Down
6 changes: 2 additions & 4 deletions rcl/src/rcl/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@ const char * rcl_create_node_logger_name(
rcl_node_t
rcl_get_zero_initialized_node(void)
{
static rcl_node_t null_node = {
.context = 0,
.impl = 0
};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_node_t null_node;
return null_node;
}

Expand Down
5 changes: 3 additions & 2 deletions rcl/src/rcl/publisher.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ extern "C"
rcl_publisher_t
rcl_get_zero_initialized_publisher(void)
{
static rcl_publisher_t null_publisher = {0};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_publisher_t null_publisher;
return null_publisher;
}

Expand Down Expand Up @@ -219,7 +220,7 @@ rcl_publisher_options_t
rcl_publisher_get_default_options(void)
{
// !!! MAKE SURE THAT CHANGES TO THESE DEFAULTS ARE REFLECTED IN THE HEADER DOC STRING
static rcl_publisher_options_t default_options;
rcl_publisher_options_t default_options;
// Must set the allocator and qos after because they are not a compile time constant.
default_options.qos = rmw_qos_profile_default;
default_options.allocator = rcl_get_default_allocator();
Expand Down
5 changes: 2 additions & 3 deletions rcl/src/rcl/remap.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ extern "C"
rcl_remap_t
rcl_get_zero_initialized_remap(void)
{
static rcl_remap_t default_rule = {
.impl = NULL
};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_remap_t default_rule;
return default_rule;
}

Expand Down
5 changes: 3 additions & 2 deletions rcl/src/rcl/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ struct rcl_service_impl_s
rcl_service_t
rcl_get_zero_initialized_service(void)
{
static rcl_service_t null_service = {0};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_service_t null_service;
return null_service;
}

Expand Down Expand Up @@ -285,7 +286,7 @@ rcl_service_options_t
rcl_service_get_default_options(void)
{
// !!! MAKE SURE THAT CHANGES TO THESE DEFAULTS ARE REFLECTED IN THE HEADER DOC STRING
static rcl_service_options_t default_options;
rcl_service_options_t default_options;
// Must set the allocator and qos after because they are not a compile time constant.
default_options.qos = rmw_qos_profile_services_default;
default_options.allocator = rcl_get_default_allocator();
Expand Down
3 changes: 2 additions & 1 deletion rcl/src/rcl/service_event_publisher.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@

rcl_service_event_publisher_t rcl_get_zero_initialized_service_event_publisher(void)
{
static rcl_service_event_publisher_t zero_service_event_publisher = {0};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_service_event_publisher_t zero_service_event_publisher;
return zero_service_event_publisher;
}

Expand Down
5 changes: 3 additions & 2 deletions rcl/src/rcl/subscription.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ extern "C"
rcl_subscription_t
rcl_get_zero_initialized_subscription(void)
{
static rcl_subscription_t null_subscription = {0};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_subscription_t null_subscription;
return null_subscription;
}

Expand Down Expand Up @@ -227,7 +228,7 @@ rcl_subscription_options_t
rcl_subscription_get_default_options(void)
{
// !!! MAKE SURE THAT CHANGES TO THESE DEFAULTS ARE REFLECTED IN THE HEADER DOC STRING
static rcl_subscription_options_t default_options;
rcl_subscription_options_t default_options;
// Must set these after declaration because they are not a compile time constants.
default_options.qos = rmw_qos_profile_default;
default_options.allocator = rcl_get_default_allocator();
Expand Down
3 changes: 2 additions & 1 deletion rcl/src/rcl/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ struct rcl_timer_impl_s
rcl_timer_t
rcl_get_zero_initialized_timer(void)
{
static rcl_timer_t null_timer = {0};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_timer_t null_timer;
return null_timer;
}

Expand Down
17 changes: 2 additions & 15 deletions rcl/src/rcl/wait.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,8 @@ struct rcl_wait_set_impl_s
rcl_wait_set_t
rcl_get_zero_initialized_wait_set(void)
{
static rcl_wait_set_t null_wait_set = {
.subscriptions = NULL,
.size_of_subscriptions = 0,
.guard_conditions = NULL,
.size_of_guard_conditions = 0,
.clients = NULL,
.size_of_clients = 0,
.services = NULL,
.size_of_services = 0,
.timers = NULL,
.size_of_timers = 0,
.events = NULL,
.size_of_events = 0,
.impl = NULL,
};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_wait_set_t null_wait_set;
return null_wait_set;
}

Expand Down
5 changes: 3 additions & 2 deletions rcl_action/src/rcl_action/action_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ extern "C"
rcl_action_client_t
rcl_action_get_zero_initialized_client(void)
{
static rcl_action_client_t null_action_client = {0};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_action_client_t null_action_client;
return null_action_client;
}

Expand Down Expand Up @@ -269,7 +270,7 @@ rcl_action_client_fini(rcl_action_client_t * action_client, rcl_node_t * node)
rcl_action_client_options_t
rcl_action_client_get_default_options(void)
{
static rcl_action_client_options_t default_options;
rcl_action_client_options_t default_options;
default_options.goal_service_qos = rmw_qos_profile_services_default;
default_options.cancel_service_qos = rmw_qos_profile_services_default;
default_options.result_service_qos = rmw_qos_profile_services_default;
Expand Down
5 changes: 3 additions & 2 deletions rcl_action/src/rcl_action/action_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ rcl_action_goal_handle_set_goal_terminal_timestamp(
rcl_action_server_t
rcl_action_get_zero_initialized_server(void)
{
static rcl_action_server_t null_action_server = {0};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_action_server_t null_action_server;
return null_action_server;
}

Expand Down Expand Up @@ -285,7 +286,7 @@ rcl_action_server_options_t
rcl_action_server_get_default_options(void)
{
// !!! MAKE SURE THAT CHANGES TO THESE DEFAULTS ARE REFLECTED IN THE HEADER DOC STRING
static rcl_action_server_options_t default_options;
rcl_action_server_options_t default_options;
default_options.goal_service_qos = rmw_qos_profile_services_default;
default_options.cancel_service_qos = rmw_qos_profile_services_default;
default_options.result_service_qos = rmw_qos_profile_services_default;
Expand Down
3 changes: 2 additions & 1 deletion rcl_action/src/rcl_action/goal_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ typedef struct rcl_action_goal_handle_impl_s
rcl_action_goal_handle_t
rcl_action_get_zero_initialized_goal_handle(void)
{
static rcl_action_goal_handle_t null_handle = {0};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_action_goal_handle_t null_handle;
return null_handle;
}

Expand Down
12 changes: 8 additions & 4 deletions rcl_action/src/rcl_action/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,32 @@ extern "C"
rcl_action_goal_info_t
rcl_action_get_zero_initialized_goal_info(void)
{
static rcl_action_goal_info_t goal_info = {{{0}}, {0, 0}};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_action_goal_info_t goal_info;
return goal_info;
}

rcl_action_goal_status_array_t
rcl_action_get_zero_initialized_goal_status_array(void)
{
static rcl_action_goal_status_array_t status_array = {{{0, 0, 0}}, {0, 0, 0, 0, 0}};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_action_goal_status_array_t status_array;
return status_array;
}

rcl_action_cancel_request_t
rcl_action_get_zero_initialized_cancel_request(void)
{
static rcl_action_cancel_request_t request = {{{{0}}, {0, 0}}};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_action_cancel_request_t request;
return request;
}

rcl_action_cancel_response_t
rcl_action_get_zero_initialized_cancel_response(void)
{
static rcl_action_cancel_response_t response = {{0, {0, 0, 0}}, {0, 0, 0, 0, 0}};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_action_cancel_response_t response;
return response;
}

Expand Down
6 changes: 1 addition & 5 deletions rcl_lifecycle/src/transition_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@ extern "C"
rcl_lifecycle_transition_map_t
rcl_lifecycle_get_zero_initialized_transition_map(void)
{
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static rcl_lifecycle_transition_map_t transition_map;
transition_map.states = NULL;
transition_map.states_size = 0;
transition_map.transitions = NULL;
transition_map.transitions_size = 0;

return transition_map;
}

Expand Down

0 comments on commit 3762d86

Please sign in to comment.