Skip to content

Commit

Permalink
Make rcutils_split() return RCUTILS_RET_BAD_ALLOC if alloc fails (ros…
Browse files Browse the repository at this point in the history
…2#444)

Also, mention RCUTILS_RET_BAD_ALLOC in rcutils_split_last() doc.

Signed-off-by: Christophe Bedard <christophe.bedard@apex.ai>
  • Loading branch information
christophebedard committed Jan 23, 2024
1 parent 0b8c101 commit d76ab0e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/rcutils/split.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ rcutils_split(
* \param[in] allocator for allocating new memory for the output array
* \param[out] string_array with the split tokens
* \return #RCUTILS_RET_OK if successful, or
* \return #RCUTILS_RET_BAD_ALLOC if memory allocation fails, or
* \return #RCUTILS_RET_ERROR if an unknown error occurs
*/
RCUTILS_PUBLIC
Expand Down
2 changes: 1 addition & 1 deletion src/split.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ rcutils_split(
}

RCUTILS_SET_ERROR_MSG("unable to allocate memory for string array data");
return RCUTILS_RET_ERROR;
return RCUTILS_RET_BAD_ALLOC;
}

rcutils_ret_t
Expand Down
4 changes: 2 additions & 2 deletions test/test_split.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ TEST(test_split, split) {
rcutils_allocator_t time_bomb_allocator = get_time_bomb_allocator();
set_time_bomb_allocator_malloc_count(time_bomb_allocator, 0);
EXPECT_EQ(
RCUTILS_RET_ERROR,
RCUTILS_RET_BAD_ALLOC,
rcutils_split("Test", '/', time_bomb_allocator, &tokens_fail));

// Allocating string_array->data[0] fails
set_time_bomb_allocator_malloc_count(time_bomb_allocator, 1);
EXPECT_EQ(
RCUTILS_RET_ERROR,
RCUTILS_RET_BAD_ALLOC,
rcutils_split("hello/world", '/', time_bomb_allocator, &tokens_fail));

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

0 comments on commit d76ab0e

Please sign in to comment.