From 133884beb6d9bd000f82390569e772be9ece3311 Mon Sep 17 00:00:00 2001 From: Scott K Logan Date: Wed, 6 Mar 2024 23:32:56 -0600 Subject: [PATCH] Check allocator validity in some rcl_logging functions (#116) If the allocator is zero-initialized, it may cause a segfault when it is used later in the functions. Signed-off-by: Scott K Logan --- rcl_logging_interface/src/logging_dir.c | 1 + rcl_logging_spdlog/src/rcl_logging_spdlog.cpp | 2 ++ rcl_logging_spdlog/test/test_logging_interface.cpp | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/rcl_logging_interface/src/logging_dir.c b/rcl_logging_interface/src/logging_dir.c index 68eded7..e49e067 100644 --- a/rcl_logging_interface/src/logging_dir.c +++ b/rcl_logging_interface/src/logging_dir.c @@ -28,6 +28,7 @@ rcl_logging_get_logging_directory(rcutils_allocator_t allocator, char ** directo RCUTILS_SET_ERROR_MSG("directory argument must not be null"); return RCL_LOGGING_RET_INVALID_ARGUMENT; } + RCUTILS_CHECK_ALLOCATOR(&allocator, return RCL_LOGGING_RET_INVALID_ARGUMENT); if (NULL != *directory) { RCUTILS_SET_ERROR_MSG("directory argument must point to null"); return RCL_LOGGING_RET_INVALID_ARGUMENT; diff --git a/rcl_logging_spdlog/src/rcl_logging_spdlog.cpp b/rcl_logging_spdlog/src/rcl_logging_spdlog.cpp index af94684..24ab830 100644 --- a/rcl_logging_spdlog/src/rcl_logging_spdlog.cpp +++ b/rcl_logging_spdlog/src/rcl_logging_spdlog.cpp @@ -100,6 +100,8 @@ rcl_logging_ret_t rcl_logging_external_initialize( const char * config_file, rcutils_allocator_t allocator) { + RCUTILS_CHECK_ALLOCATOR(&allocator, return RCL_LOGGING_RET_INVALID_ARGUMENT); + std::lock_guard lk(g_logger_mutex); // It is possible for this to get called more than once in a process (some of // the tests do this implicitly by calling rclcpp::init more than once). diff --git a/rcl_logging_spdlog/test/test_logging_interface.cpp b/rcl_logging_spdlog/test/test_logging_interface.cpp index a3486c3..2f911a0 100644 --- a/rcl_logging_spdlog/test/test_logging_interface.cpp +++ b/rcl_logging_spdlog/test/test_logging_interface.cpp @@ -192,7 +192,7 @@ TEST_F(AllocatorTest, init_invalid) rcl_logging_external_initialize("anything", nullptr, bad_allocator)); rcutils_reset_error(); EXPECT_EQ( - RCL_LOGGING_RET_ERROR, + RCL_LOGGING_RET_INVALID_ARGUMENT, rcl_logging_external_initialize(nullptr, nullptr, invalid_allocator)); rcutils_reset_error(); }