From f4105b162277cdec8ad62e47cbf52aead073d277 Mon Sep 17 00:00:00 2001 From: Tomoya Fujita Date: Mon, 16 Sep 2024 08:56:53 -0700 Subject: [PATCH] Initialize the NULL strucutre with static value. (#378) * Initialize the NULL strucutre with static value. Signed-off-by: Tomoya Fujita * use static const for xxx_get_zero_initialized_yyy functions. Signed-off-by: Tomoya Fujita * address review comment. Signed-off-by: Tomoya Fujita --------- Signed-off-by: Tomoya Fujita --- rmw/src/discovery_options.c | 6 ++---- rmw/src/event.c | 6 ++++-- rmw/src/init_options.c | 21 +++++++++++-------- rmw/src/message_sequence.c | 18 ++++------------ rmw/src/names_and_types.c | 7 ++----- rmw/src/network_flow_endpoint.c | 3 ++- rmw/src/network_flow_endpoint_array.c | 3 ++- rmw/src/security_options.c | 5 +++-- rmw/src/subscription_content_filter_options.c | 7 +++---- rmw/src/topic_endpoint_info.c | 10 ++------- rmw/src/topic_endpoint_info_array.c | 10 ++------- rmw/src/types.c | 3 ++- 12 files changed, 40 insertions(+), 59 deletions(-) diff --git a/rmw/src/discovery_options.c b/rmw/src/discovery_options.c index f4f92b82..23b5d0b2 100644 --- a/rmw/src/discovery_options.c +++ b/rmw/src/discovery_options.c @@ -21,10 +21,8 @@ rmw_discovery_options_t rmw_get_zero_initialized_discovery_options(void) { - rmw_discovery_options_t result = (rmw_discovery_options_t) { - .automatic_discovery_range = RMW_AUTOMATIC_DISCOVERY_RANGE_NOT_SET, - .static_peers_count = 0, - }; // NOLINT(readability/braces): false positive + // All members are initialized to 0 or NULL by C99 6.7.8/10. + static const rmw_discovery_options_t result; return result; } diff --git a/rmw/src/event.c b/rmw/src/event.c index 8005fe47..9a9ed235 100644 --- a/rmw/src/event.c +++ b/rmw/src/event.c @@ -24,11 +24,13 @@ extern "C" { rmw_event_t rmw_get_zero_initialized_event(void) { - const rmw_event_t event = { + // TODO(@fujitatomoya): This is not exatly zero initialized structure. + /// We should introduce xxx_get_default_event to return the default values. + static const rmw_event_t event = { .implementation_identifier = NULL, .data = NULL, .event_type = RMW_EVENT_INVALID - }; // NOLINT(readability/braces): false positive + }; return event; } diff --git a/rmw/src/init_options.c b/rmw/src/init_options.c index f9c78482..0c45388a 100644 --- a/rmw/src/init_options.c +++ b/rmw/src/init_options.c @@ -25,15 +25,18 @@ extern "C" rmw_init_options_t rmw_get_zero_initialized_init_options(void) { - return (const rmw_init_options_t) { - .domain_id = RMW_DEFAULT_DOMAIN_ID, - .discovery_options = rmw_get_zero_initialized_discovery_options(), - .implementation_identifier = NULL, - .impl = NULL, - .instance_id = 0, - .enclave = NULL, - .security_options = rmw_get_default_security_options(), - }; // NOLINT(readability/braces): false positive + // TODO(@fujitatomoya): This is not exatly zero initialized structure. + /// We should introduce xxx_get_default_init_optionst to return the default values. + static const rmw_init_options_t init_option = { + .domain_id = RMW_DEFAULT_DOMAIN_ID, + .discovery_options = {RMW_AUTOMATIC_DISCOVERY_RANGE_NOT_SET, 0}, + .implementation_identifier = NULL, + .impl = NULL, + .instance_id = 0, + .enclave = NULL, + .security_options = {RMW_SECURITY_ENFORCEMENT_PERMISSIVE, NULL}, + }; + return init_option; } #ifdef __cplusplus diff --git a/rmw/src/message_sequence.c b/rmw/src/message_sequence.c index 373feb02..49897bd9 100644 --- a/rmw/src/message_sequence.c +++ b/rmw/src/message_sequence.c @@ -18,13 +18,8 @@ rmw_message_sequence_t rmw_get_zero_initialized_message_sequence(void) { - static rmw_message_sequence_t message_sequence = { - .data = NULL, - .size = 0u, - .capacity = 0u, - .allocator = NULL - }; - + // All members are initialized to 0 or NULL by C99 6.7.8/10. + static const rmw_message_sequence_t message_sequence; return message_sequence; } @@ -75,13 +70,8 @@ rmw_message_sequence_fini(rmw_message_sequence_t * sequence) rmw_message_info_sequence_t rmw_get_zero_initialized_message_info_sequence(void) { - static rmw_message_info_sequence_t message_info_sequence = { - .data = NULL, - .size = 0u, - .capacity = 0u, - .allocator = NULL - }; - + // All members are initialized to 0 or NULL by C99 6.7.8/10. + static const rmw_message_info_sequence_t message_info_sequence; return message_info_sequence; } diff --git a/rmw/src/names_and_types.c b/rmw/src/names_and_types.c index 9baf512d..a164f957 100644 --- a/rmw/src/names_and_types.c +++ b/rmw/src/names_and_types.c @@ -24,11 +24,8 @@ rmw_names_and_types_t rmw_get_zero_initialized_names_and_types(void) { - static rmw_names_and_types_t zero = { - .names = {0, NULL, {NULL, NULL, NULL, NULL, NULL}}, - .types = NULL, - }; - zero.names = rcutils_get_zero_initialized_string_array(); + // All members are initialized to 0 or NULL by C99 6.7.8/10. + static const rmw_names_and_types_t zero; return zero; } diff --git a/rmw/src/network_flow_endpoint.c b/rmw/src/network_flow_endpoint.c index 41d921cb..7f781fdc 100644 --- a/rmw/src/network_flow_endpoint.c +++ b/rmw/src/network_flow_endpoint.c @@ -22,7 +22,8 @@ rmw_network_flow_endpoint_t rmw_get_zero_initialized_network_flow_endpoint(void) { - rmw_network_flow_endpoint_t network_flow_endpoint = {0}; + // All members are initialized to 0 or NULL by C99 6.7.8/10. + static const rmw_network_flow_endpoint_t network_flow_endpoint; return network_flow_endpoint; } diff --git a/rmw/src/network_flow_endpoint_array.c b/rmw/src/network_flow_endpoint_array.c index b9401901..1d2ac22c 100644 --- a/rmw/src/network_flow_endpoint_array.c +++ b/rmw/src/network_flow_endpoint_array.c @@ -18,7 +18,8 @@ rmw_network_flow_endpoint_array_t rmw_get_zero_initialized_network_flow_endpoint_array(void) { - rmw_network_flow_endpoint_array_t network_flow_endpoint_array = {0}; + // All members are initialized to 0 or NULL by C99 6.7.8/10. + static const rmw_network_flow_endpoint_array_t network_flow_endpoint_array; return network_flow_endpoint_array; } diff --git a/rmw/src/security_options.c b/rmw/src/security_options.c index ac06b9c3..2e60bd43 100644 --- a/rmw/src/security_options.c +++ b/rmw/src/security_options.c @@ -22,14 +22,15 @@ rmw_security_options_t rmw_get_zero_initialized_security_options(void) { - rmw_security_options_t zero_initialized_options = {0, NULL}; + // All members are initialized to 0 or NULL by C99 6.7.8/10. + static const rmw_security_options_t zero_initialized_options; return zero_initialized_options; } rmw_security_options_t rmw_get_default_security_options(void) { - rmw_security_options_t default_options = { + static const rmw_security_options_t default_options = { RMW_SECURITY_ENFORCEMENT_PERMISSIVE, NULL, }; diff --git a/rmw/src/subscription_content_filter_options.c b/rmw/src/subscription_content_filter_options.c index aaf63f28..3a47911e 100644 --- a/rmw/src/subscription_content_filter_options.c +++ b/rmw/src/subscription_content_filter_options.c @@ -22,10 +22,9 @@ rmw_subscription_content_filter_options_t rmw_get_zero_initialized_content_filter_options(void) { - return (const rmw_subscription_content_filter_options_t) { - .filter_expression = NULL, - .expression_parameters = rcutils_get_zero_initialized_string_array() - }; // NOLINT(readability/braces): false positive + // All members are initialized to 0 or NULL by C99 6.7.8/10. + static const rmw_subscription_content_filter_options_t zero; + return zero; } rmw_ret_t diff --git a/rmw/src/topic_endpoint_info.c b/rmw/src/topic_endpoint_info.c index b8d94eb9..10d819ae 100644 --- a/rmw/src/topic_endpoint_info.c +++ b/rmw/src/topic_endpoint_info.c @@ -22,14 +22,8 @@ rmw_topic_endpoint_info_t rmw_get_zero_initialized_topic_endpoint_info(void) { -#ifdef __clang__ -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wmissing-field-initializers" -#endif - rmw_topic_endpoint_info_t zero = {0}; -#ifdef __clang__ -# pragma clang diagnostic pop -#endif + // All members are initialized to 0 or NULL by C99 6.7.8/10. + static const rmw_topic_endpoint_info_t zero; return zero; } diff --git a/rmw/src/topic_endpoint_info_array.c b/rmw/src/topic_endpoint_info_array.c index 1cd1f47d..4c649437 100644 --- a/rmw/src/topic_endpoint_info_array.c +++ b/rmw/src/topic_endpoint_info_array.c @@ -19,14 +19,8 @@ rmw_topic_endpoint_info_array_t rmw_get_zero_initialized_topic_endpoint_info_array(void) { -#ifdef __clang__ -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wmissing-field-initializers" -#endif - rmw_topic_endpoint_info_array_t zero = {0}; -#ifdef __clang__ -# pragma clang diagnostic pop -#endif + // All members are initialized to 0 or NULL by C99 6.7.8/10. + static const rmw_topic_endpoint_info_array_t zero; return zero; } diff --git a/rmw/src/types.c b/rmw/src/types.c index dfb79dcf..b8a83e12 100644 --- a/rmw/src/types.c +++ b/rmw/src/types.c @@ -19,6 +19,7 @@ RMW_WARN_UNUSED rmw_message_info_t rmw_get_zero_initialized_message_info(void) { - rmw_message_info_t zero_initialized_message_info = {0}; + // All members are initialized to 0 or NULL by C99 6.7.8/10. + static const rmw_message_info_t zero_initialized_message_info; return zero_initialized_message_info; }