Skip to content

Commit d76ab0e

Browse files
Make rcutils_split() return RCUTILS_RET_BAD_ALLOC if alloc fails (ros2#444)
Also, mention RCUTILS_RET_BAD_ALLOC in rcutils_split_last() doc. Signed-off-by: Christophe Bedard <christophe.bedard@apex.ai>
1 parent 0b8c101 commit d76ab0e

3 files changed

Lines changed: 4 additions & 3 deletions

File tree

‎include/rcutils/split.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ rcutils_split(
5252
* \param[in] allocator for allocating new memory for the output array
5353
* \param[out] string_array with the split tokens
5454
* \return #RCUTILS_RET_OK if successful, or
55+
* \return #RCUTILS_RET_BAD_ALLOC if memory allocation fails, or
5556
* \return #RCUTILS_RET_ERROR if an unknown error occurs
5657
*/
5758
RCUTILS_PUBLIC

‎src/split.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ rcutils_split(
118118
}
119119

120120
RCUTILS_SET_ERROR_MSG("unable to allocate memory for string array data");
121-
return RCUTILS_RET_ERROR;
121+
return RCUTILS_RET_BAD_ALLOC;
122122
}
123123

124124
rcutils_ret_t

‎test/test_split.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ TEST(test_split, split) {
6868
rcutils_allocator_t time_bomb_allocator = get_time_bomb_allocator();
6969
set_time_bomb_allocator_malloc_count(time_bomb_allocator, 0);
7070
EXPECT_EQ(
71-
RCUTILS_RET_ERROR,
71+
RCUTILS_RET_BAD_ALLOC,
7272
rcutils_split("Test", '/', time_bomb_allocator, &tokens_fail));
7373

7474
// Allocating string_array->data[0] fails
7575
set_time_bomb_allocator_malloc_count(time_bomb_allocator, 1);
7676
EXPECT_EQ(
77-
RCUTILS_RET_ERROR,
77+
RCUTILS_RET_BAD_ALLOC,
7878
rcutils_split("hello/world", '/', time_bomb_allocator, &tokens_fail));
7979

8080
rcutils_string_array_t tokens0 = test_split("", '/', 0);

0 commit comments

Comments
 (0)