diff --git a/rcl/test/mocking_utils/patch.hpp b/rcl/test/mocking_utils/patch.hpp index 8bb67d563..7b0d8d897 100644 --- a/rcl/test/mocking_utils/patch.hpp +++ b/rcl/test/mocking_utils/patch.hpp @@ -473,10 +473,22 @@ auto make_patch(const std::string & target, std::function proxy) /// Patch a `function` to execute normally but always yield a given `return_code` /// in a given `scope`. +/** + * \warning On some Linux distributions (e.g. CentOS), pointers to function + * reference their PLT trampolines. In such cases, it is not possible to + * call `function` from within the mock. + */ #define inject_on_return(scope, function, return_code) \ patch( \ scope, function, ([&, base = function](auto && ... __args) { \ - static_cast(base(std::forward(__args)...)); \ + if (base != function) { \ + static_cast(base(std::forward(__args)...)); \ + } else { \ + RCUTILS_SAFE_FWRITE_TO_STDERR( \ + "[WARNING] mocking_utils::inject_on_return() cannot forward call to " \ + "original '" RCUTILS_STRINGIFY(function) "' function before injection\n" \ + " at " __FILE__ ":" RCUTILS_STRINGIFY(__LINE__) "\n"); \ + } \ return return_code; \ })) diff --git a/rcl/test/rcl/test_logging.cpp b/rcl/test/rcl/test_logging.cpp index 5ffb95eb6..1ce2eda91 100644 --- a/rcl/test/rcl/test_logging.cpp +++ b/rcl/test/rcl/test_logging.cpp @@ -242,14 +242,13 @@ TEST(TestLogging, test_failing_external_logging) { std::stringstream stderr_sstream; auto fwrite_mock = mocking_utils::patch( "lib:rcl", fwrite, - ([&, base = fwrite](const void * ptr, size_t size, - size_t count, FILE * stream) + [&](const void * ptr, size_t size, size_t count, FILE * stream) { if (sizeof(char) == size && stderr == stream) { stderr_sstream << std::string(reinterpret_cast(ptr), count); } - return base(ptr, size, count, stream); - })); + return count; + }); constexpr char stderr_message[] = "internal error"; #ifdef MOCKING_UTILS_SUPPORT_VA_LIST diff --git a/rcl/test/rcl/test_rmw_impl_id_check_func.cpp b/rcl/test/rcl/test_rmw_impl_id_check_func.cpp index 7d576f3d7..62c3f2e7e 100644 --- a/rcl/test/rcl/test_rmw_impl_id_check_func.cpp +++ b/rcl/test/rcl/test_rmw_impl_id_check_func.cpp @@ -128,12 +128,15 @@ TEST(TestRmwCheck, test_mock_rmw_impl_check) { auto mock = mocking_utils::patch( "lib:rcl", rcutils_strdup, - [base = rcutils_strdup](const char * str, auto allocator) + [](const char * str, auto allocator) { static int counter = 1; if (counter == 1) { counter++; - return base(str, allocator); + char * dup = static_cast( + allocator.allocate(strlen(str) + 1, allocator.state)); + memcpy(dup, str, strlen(str) + 1); + return dup; } else { return static_cast(NULL); }