File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -42,7 +42,6 @@ rcutils_split(
4242 * string_array = rcutils_get_zero_initialized_string_array ();
4343 return RCUTILS_RET_OK ;
4444 }
45- string_array -> allocator = allocator ;
4645
4746 size_t string_size = strlen (str );
4847
@@ -58,15 +57,13 @@ rcutils_split(
5857 rhs_offset = 1 ;
5958 }
6059
61- string_array -> size = 1 ;
60+ size_t array_size = 1 ;
6261 for (size_t i = lhs_offset ; i < string_size - rhs_offset ; ++ i ) {
6362 if (str [i ] == delimiter ) {
64- ++ string_array -> size ;
63+ ++ array_size ;
6564 }
6665 }
67- // TODO(wjwwood): refactor this function so it can use rcutils_string_array_init() instead
68- string_array -> data = allocator .allocate (string_array -> size * sizeof (char * ), allocator .state );
69- if (NULL == string_array -> data ) {
66+ if (rcutils_string_array_init (string_array , array_size , & allocator ) != RCUTILS_RET_OK ) {
7067 goto fail ;
7168 }
7269
@@ -104,6 +101,9 @@ rcutils_split(
104101 } else {
105102 string_array -> data [token_counter ] =
106103 allocator .allocate ((rhs - lhs + 2 ) * sizeof (char ), allocator .state );
104+ if (NULL == string_array -> data [token_counter ]) {
105+ goto fail ;
106+ }
107107 snprintf (string_array -> data [token_counter ], (rhs - lhs + 1 ), "%s" , str + lhs );
108108 }
109109
Original file line number Diff line number Diff line change @@ -64,14 +64,22 @@ TEST(test_split, split) {
6464 RCUTILS_RET_INVALID_ARGUMENT ,
6565 rcutils_split (" Test" , ' /' , rcutils_get_default_allocator (), NULL ));
6666
67- // Allocating string_array->data fails
67+ // Allocating initial string_array fails
6868 rcutils_allocator_t time_bomb_allocator = get_time_bomb_allocator ();
69+ set_time_bomb_allocator_calloc_count (time_bomb_allocator, 0 );
70+ EXPECT_EQ (
71+ RCUTILS_RET_BAD_ALLOC ,
72+ rcutils_split (" Test" , ' /' , time_bomb_allocator, &tokens_fail));
73+
74+ // Allocating string_array->data fails
75+ set_time_bomb_allocator_calloc_count (time_bomb_allocator, 1 );
6976 set_time_bomb_allocator_malloc_count (time_bomb_allocator, 0 );
7077 EXPECT_EQ (
7178 RCUTILS_RET_BAD_ALLOC ,
7279 rcutils_split (" Test" , ' /' , time_bomb_allocator, &tokens_fail));
7380
7481 // Allocating string_array->data[0] fails
82+ set_time_bomb_allocator_calloc_count (time_bomb_allocator, 1 );
7583 set_time_bomb_allocator_malloc_count (time_bomb_allocator, 1 );
7684 EXPECT_EQ (
7785 RCUTILS_RET_BAD_ALLOC ,
You can’t perform that action at this time.
0 commit comments