From 2ee91d8e4c02ce04899c792c1b28119673b5bf61 Mon Sep 17 00:00:00 2001 From: Nikolai Morin Date: Fri, 18 Dec 2020 17:59:34 +0100 Subject: [PATCH] Add NULL check in remap.c (#879) * Add NULL check in remap.c Signed-off-by: Nikolai Morin --- rcl/src/rcl/remap.c | 7 ++++++- rcl/test/rcl/test_remap.cpp | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/rcl/src/rcl/remap.c b/rcl/src/rcl/remap.c index 7191c48c7..d767ddd55 100644 --- a/rcl/src/rcl/remap.c +++ b/rcl/src/rcl/remap.c @@ -142,7 +142,12 @@ rcl_remap_first_match( } continue; } - matched = (0 == strcmp(expanded_match, name)); + if (NULL != name) { + // this check is to satisfy clang-tidy – name is always not null when type_bitmask is + // RCL_TOPIC_REMAP or RCL_SERVICE_REMAP. That is guaranteed because rcl_remap_first_match + // and rcl_remap_name are not public. + matched = (0 == strcmp(expanded_match, name)); + } allocator.deallocate(expanded_match, allocator.state); } else { // nodename and namespace replacement apply if the type and node name prefix checks passed diff --git a/rcl/test/rcl/test_remap.cpp b/rcl/test/rcl/test_remap.cpp index 72016d867..b8ae663b7 100644 --- a/rcl/test/rcl/test_remap.cpp +++ b/rcl/test/rcl/test_remap.cpp @@ -172,6 +172,27 @@ TEST_F(CLASSNAME(TestRemapFixture, RMW_IMPLEMENTATION), global_topic_name_replac } } +TEST_F(CLASSNAME(TestRemapFixture, RMW_IMPLEMENTATION), topic_and_service_name_not_null) { + rcl_ret_t ret; + rcl_arguments_t global_arguments; + SCOPE_ARGS(global_arguments, "process_name", "--ros-args", "-r", "/bar/foo:=/foo/bar"); + + { + char * output = NULL; + ret = rcl_remap_service_name( + NULL, &global_arguments, NULL, "NodeName", "/", rcl_get_default_allocator(), &output); + EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, ret); + ASSERT_EQ(NULL, output); + } + { + char * output = NULL; + ret = rcl_remap_topic_name( + NULL, &global_arguments, NULL, "NodeName", "/", rcl_get_default_allocator(), &output); + EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, ret); + EXPECT_EQ(NULL, output); + } +} + TEST_F(CLASSNAME(TestRemapFixture, RMW_IMPLEMENTATION), relative_topic_name_remap) { rcl_ret_t ret; rcl_arguments_t global_arguments;