Skip to content

Commit

Permalink
Fix setting allocator to NULL. (#478)
Browse files Browse the repository at this point in the history
* Fix setting allocator to NULL.

That is, previously the code was setting the allocator at
runtime, not during static initialization.  Switch to static
initialization, which should fix a possible race.

* Statically initialize all zero_initialized functions.

Signed-off-by: Chris Lalancette <clalancette@gmail.com>
  • Loading branch information
clalancette authored Sep 17, 2024
1 parent 94ec3d8 commit 20a23ea
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 33 deletions.
8 changes: 1 addition & 7 deletions src/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,7 @@ __default_zero_allocate(size_t number_of_elements, size_t size_of_element, void
rcutils_allocator_t
rcutils_get_zero_initialized_allocator(void)
{
static rcutils_allocator_t zero_allocator = {
.allocate = NULL,
.deallocate = NULL,
.reallocate = NULL,
.zero_allocate = NULL,
.state = NULL,
};
static rcutils_allocator_t zero_allocator = {0};
return zero_allocator;
}

Expand Down
2 changes: 1 addition & 1 deletion src/array_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ typedef struct rcutils_array_list_impl_s
rcutils_array_list_t
rcutils_get_zero_initialized_array_list(void)
{
static rcutils_array_list_t zero_initialized_array_list = {NULL};
static rcutils_array_list_t zero_initialized_array_list = {0};
return zero_initialized_array_list;
}

Expand Down
8 changes: 1 addition & 7 deletions src/char_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@
rcutils_char_array_t
rcutils_get_zero_initialized_char_array(void)
{
static rcutils_char_array_t char_array = {
.buffer = NULL,
.owns_buffer = true,
.buffer_length = 0u,
.buffer_capacity = 0u
};
char_array.allocator = rcutils_get_zero_initialized_allocator();
static rcutils_char_array_t char_array = {0};
return char_array;
}

Expand Down
2 changes: 1 addition & 1 deletion src/hash_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ int rcutils_hash_map_string_cmp_func(const void * val1, const void * val2)
rcutils_hash_map_t
rcutils_get_zero_initialized_hash_map(void)
{
static rcutils_hash_map_t zero_initialized_hash_map = {NULL};
static rcutils_hash_map_t zero_initialized_hash_map = {0};
return zero_initialized_hash_map;
}

Expand Down
5 changes: 1 addition & 4 deletions src/shared_library.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ C_ASSERT(sizeof(char) == sizeof(TCHAR));
rcutils_shared_library_t
rcutils_get_zero_initialized_shared_library(void)
{
rcutils_shared_library_t zero_initialized_shared_library;
zero_initialized_shared_library.library_path = NULL;
zero_initialized_shared_library.lib_pointer = NULL;
zero_initialized_shared_library.allocator = rcutils_get_zero_initialized_allocator();
static rcutils_shared_library_t zero_initialized_shared_library = {0};
return zero_initialized_shared_library;
}

Expand Down
6 changes: 1 addition & 5 deletions src/string_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ extern "C"
rcutils_string_array_t
rcutils_get_zero_initialized_string_array(void)
{
static rcutils_string_array_t array = {
.size = 0,
.data = NULL,
};
array.allocator = rcutils_get_zero_initialized_allocator();
static rcutils_string_array_t array = {0};
return array;
}

Expand Down
3 changes: 1 addition & 2 deletions src/string_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ typedef struct rcutils_string_map_impl_s
rcutils_string_map_t
rcutils_get_zero_initialized_string_map(void)
{
static rcutils_string_map_t zero_initialized_string_map;
zero_initialized_string_map.impl = NULL;
static rcutils_string_map_t zero_initialized_string_map = {0};
return zero_initialized_string_map;
}

Expand Down
7 changes: 1 addition & 6 deletions src/uint8_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@
rcutils_uint8_array_t
rcutils_get_zero_initialized_uint8_array(void)
{
static rcutils_uint8_array_t uint8_array = {
.buffer = NULL,
.buffer_length = 0lu,
.buffer_capacity = 0lu
};
uint8_array.allocator = rcutils_get_zero_initialized_allocator();
static rcutils_uint8_array_t uint8_array = {0};
return uint8_array;
}

Expand Down

0 comments on commit 20a23ea

Please sign in to comment.