From 9c7b4fc9905894bb843e7b53cb40be702a77e407 Mon Sep 17 00:00:00 2001 From: Pablo Garrido Date: Tue, 8 Mar 2022 14:18:58 +0100 Subject: [PATCH 1/2] Reduce error handling static size (#14) Signed-off-by: Pablo Garrido (cherry picked from commit 1176652124c12b23cb58f052077901d2c24dbf39) # Conflicts: # include/rcutils/error_handling.h --- include/rcutils/error_handling.h | 41 ++++++++++++++++++++++++++++++-- src/error_handling_helpers.h | 4 ++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/include/rcutils/error_handling.h b/include/rcutils/error_handling.h index d5f7ea3b..a9a2e365 100644 --- a/include/rcutils/error_handling.h +++ b/include/rcutils/error_handling.h @@ -49,11 +49,41 @@ extern "C" #elif !defined(RCUTILS_NO_FILESYSTEM) #define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) \ do {fwrite(msg, sizeof(char), strlen(msg), stderr);} while (0) -#else +#else #define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) #endif +<<<<<<< HEAD // fixed constraints +======= +/// Set the error message to stderr using a format string and format arguments. +/** + * This function sets the error message to stderr using the given format string. + * The resulting formatted string is silently truncated at + * RCUTILS_ERROR_MESSAGE_MAX_LENGTH. + * + * \param[in] format_string The string to be used as the format of the error message. + * \param[in] ... Arguments for the format string. + */ + +#if !defined(RCUTILS_AVOID_DYNAMIC_ALLOCATION) +#define RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING(format_string, ...) \ + do { \ + char output_msg[RCUTILS_ERROR_MESSAGE_MAX_LENGTH]; \ + int ret = rcutils_snprintf(output_msg, sizeof(output_msg), format_string, __VA_ARGS__); \ + if (ret < 0) { \ + RCUTILS_SAFE_FWRITE_TO_STDERR("Failed to call snprintf for error message formatting\n"); \ + } else { \ + RCUTILS_SAFE_FWRITE_TO_STDERR(output_msg); \ + } \ + } while (0) +#else +#define RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING(format_string, ...) +#endif + +#if !defined(RCUTILS_AVOID_DYNAMIC_ALLOCATION) +/// The maximum length a formatted number is allowed to have. +>>>>>>> 1176652 (Reduce error handling static size (#14)) #define RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH 20 // "18446744073709551615" #define RCUTILS_ERROR_FORMATTING_CHARACTERS 6 // ', at ' + ':' @@ -71,6 +101,13 @@ extern "C" RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH - \ RCUTILS_ERROR_FORMATTING_CHARACTERS - \ 1) +#else +#define RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH 1 +#define RCUTILS_ERROR_FORMATTING_CHARACTERS 1 +#define RCUTILS_ERROR_MESSAGE_MAX_LENGTH 1 +#define RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH 1 +#define RCUTILS_ERROR_STATE_FILE_MAX_LENGTH 1 +#endif // RCUTILS_AVOID_DYNAMIC_ALLOCATION /// Struct wrapping a fixed-size c string used for returning the formatted error string. typedef struct rcutils_error_string_t @@ -91,7 +128,7 @@ typedef struct rcutils_error_state_t } rcutils_error_state_t; // make sure our math is right... -#if __STDC_VERSION__ >= 201112L +#if __STDC_VERSION__ >= 201112L && !defined(RCUTILS_AVOID_DYNAMIC_ALLOCATION) static_assert( sizeof(rcutils_error_string_t) == ( RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH + diff --git a/src/error_handling_helpers.h b/src/error_handling_helpers.h index 70acb4e8..08779ed4 100644 --- a/src/error_handling_helpers.h +++ b/src/error_handling_helpers.h @@ -108,6 +108,7 @@ static void __rcutils_convert_uint64_t_into_c_str(uint64_t number, char * buffer, size_t buffer_size) { +#if !defined(RCUTILS_AVOID_DYNAMIC_ALLOCATION) assert(buffer != NULL); assert(buffer_size >= 21); (void)buffer_size; // prevent warning in release builds where there is no assert(...) @@ -131,6 +132,7 @@ __rcutils_convert_uint64_t_into_c_str(uint64_t number, char * buffer, size_t buf // reverse the string in place __rcutils_reverse_str(buffer, strnlen(buffer, 21)); +#endif } // do not use externally, internal function which is only to be used by error_handling.c @@ -140,6 +142,7 @@ __rcutils_format_error_string( rcutils_error_string_t * error_string, const rcutils_error_state_t * error_state) { +#if !defined(RCUTILS_AVOID_DYNAMIC_ALLOCATION) assert(error_string != NULL); assert(error_state != NULL); static const char format_1[] = ", at "; @@ -173,6 +176,7 @@ __rcutils_format_error_string( written = __rcutils_copy_string(offset, bytes_left, line_number_buffer); offset += written; offset[0] = '\0'; +#endif } #ifdef __cplusplus From eff4e4aa10b2303fabf36221db508f1394cae4ed Mon Sep 17 00:00:00 2001 From: Pablo Garrido Date: Tue, 8 Mar 2022 14:22:01 +0100 Subject: [PATCH 2/2] Fix conflicts Signed-off-by: Pablo Garrido --- include/rcutils/error_handling.h | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/include/rcutils/error_handling.h b/include/rcutils/error_handling.h index a9a2e365..47cfcb14 100644 --- a/include/rcutils/error_handling.h +++ b/include/rcutils/error_handling.h @@ -53,37 +53,9 @@ extern "C" #define RCUTILS_SAFE_FWRITE_TO_STDERR(msg) #endif -<<<<<<< HEAD // fixed constraints -======= -/// Set the error message to stderr using a format string and format arguments. -/** - * This function sets the error message to stderr using the given format string. - * The resulting formatted string is silently truncated at - * RCUTILS_ERROR_MESSAGE_MAX_LENGTH. - * - * \param[in] format_string The string to be used as the format of the error message. - * \param[in] ... Arguments for the format string. - */ - -#if !defined(RCUTILS_AVOID_DYNAMIC_ALLOCATION) -#define RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING(format_string, ...) \ - do { \ - char output_msg[RCUTILS_ERROR_MESSAGE_MAX_LENGTH]; \ - int ret = rcutils_snprintf(output_msg, sizeof(output_msg), format_string, __VA_ARGS__); \ - if (ret < 0) { \ - RCUTILS_SAFE_FWRITE_TO_STDERR("Failed to call snprintf for error message formatting\n"); \ - } else { \ - RCUTILS_SAFE_FWRITE_TO_STDERR(output_msg); \ - } \ - } while (0) -#else -#define RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING(format_string, ...) -#endif - #if !defined(RCUTILS_AVOID_DYNAMIC_ALLOCATION) /// The maximum length a formatted number is allowed to have. ->>>>>>> 1176652 (Reduce error handling static size (#14)) #define RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH 20 // "18446744073709551615" #define RCUTILS_ERROR_FORMATTING_CHARACTERS 6 // ', at ' + ':'