From 3bc0f2b57c5a75ec84a300baa1c5d70c3e2e6d32 Mon Sep 17 00:00:00 2001 From: Felix Endres Date: Fri, 11 Sep 2020 17:23:20 +0200 Subject: [PATCH] Update logging.h Fix issue #285: Remove the temporary variable in a macro, because the name of the variable may shadow a variable of the macro user. As suggested by @clalancette, also wrapped it into a do-while wrapping. --- include/rcutils/logging.h | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/include/rcutils/logging.h b/include/rcutils/logging.h index afd08319..3d16aa02 100644 --- a/include/rcutils/logging.h +++ b/include/rcutils/logging.h @@ -516,17 +516,18 @@ void rcutils_logging_console_output_handler( * All logging macros ensure that this has been called once. */ #define RCUTILS_LOGGING_AUTOINIT \ - if (RCUTILS_UNLIKELY(!g_rcutils_logging_initialized)) { \ - rcutils_ret_t ret = rcutils_logging_initialize(); \ - if (ret != RCUTILS_RET_OK) { \ - RCUTILS_SAFE_FWRITE_TO_STDERR( \ - "[rcutils|" __FILE__ ":" RCUTILS_STRINGIFY(__LINE__) \ - "] error initializing logging: "); \ - RCUTILS_SAFE_FWRITE_TO_STDERR(rcutils_get_error_string().str); \ - RCUTILS_SAFE_FWRITE_TO_STDERR("\n"); \ - rcutils_reset_error(); \ + do { \ + if (RCUTILS_UNLIKELY(!g_rcutils_logging_initialized)) { \ + if (rcutils_logging_initialize() != RCUTILS_RET_OK) { \ + RCUTILS_SAFE_FWRITE_TO_STDERR( \ + "[rcutils|" __FILE__ ":" RCUTILS_STRINGIFY(__LINE__) \ + "] error initializing logging: "); \ + RCUTILS_SAFE_FWRITE_TO_STDERR(rcutils_get_error_string().str); \ + RCUTILS_SAFE_FWRITE_TO_STDERR("\n"); \ + rcutils_reset_error(); \ + } \ } \ - } + } while (0); #ifdef __cplusplus }