@@ -37,14 +37,19 @@ extern "C"
3737#include "rcutils/testing/fault_injection.h"
3838#include "rcutils/types/rcutils_ret.h"
3939#include "rcutils/visibility_control.h"
40+ #include "rcutils/configuration_flags.h"
4041
42+ #if !defined(RCUTILS_NO_FILESYSTEM )
4143/// Write the given msg out to stderr, limiting the buffer size in the `fwrite`.
4244/**
4345 * This ensures that there is an upper bound to a buffer overrun if `msg` is
4446 * non-null terminated.
4547 */
4648#define RCUTILS_SAFE_FWRITE_TO_STDERR (msg ) \
4749 do {fwrite(msg, sizeof(char), rcutils_strnlen(msg, 4096), stderr);} while (0)
50+ #else
51+ #define RCUTILS_SAFE_FWRITE_TO_STDERR (msg )
52+ #endif
4853
4954/// Set the error message to stderr using a format string and format arguments.
5055/**
@@ -55,6 +60,8 @@ extern "C"
5560 * \param[in] format_string The string to be used as the format of the error message.
5661 * \param[in] ... Arguments for the format string.
5762 */
63+
64+ #if !defined(RCUTILS_AVOID_DYNAMIC_ALLOCATION )
5865#define RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING (format_string , ...) \
5966 do { \
6067 char output_msg[RCUTILS_ERROR_MESSAGE_MAX_LENGTH]; \
@@ -66,7 +73,11 @@ extern "C"
6673 RCUTILS_SAFE_FWRITE_TO_STDERR(output_msg); \
6774 } \
6875 } while (0)
76+ #else
77+ #define RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING (format_string , ...)
78+ #endif
6979
80+ #if !defined(RCUTILS_AVOID_DYNAMIC_ALLOCATION )
7081/// The maximum length a formatted number is allowed to have.
7182#define RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH 20 // "18446744073709551615"
7283
@@ -93,6 +104,13 @@ extern "C"
93104 RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH - \
94105 RCUTILS_ERROR_FORMATTING_CHARACTERS - \
95106 1)
107+ #else
108+ #define RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH 1
109+ #define RCUTILS_ERROR_FORMATTING_CHARACTERS 1
110+ #define RCUTILS_ERROR_MESSAGE_MAX_LENGTH 1
111+ #define RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH 1
112+ #define RCUTILS_ERROR_STATE_FILE_MAX_LENGTH 1
113+ #endif // RCUTILS_AVOID_DYNAMIC_ALLOCATION
96114
97115/// Struct wrapping a fixed-size c string used for returning the formatted error string.
98116typedef struct rcutils_error_string_s
@@ -114,7 +132,7 @@ typedef struct rcutils_error_state_s
114132} rcutils_error_state_t ;
115133
116134// make sure our math is right...
117- #if __STDC_VERSION__ >= 201112L
135+ #if __STDC_VERSION__ >= 201112L && !defined( RCUTILS_AVOID_DYNAMIC_ALLOCATION )
118136static_assert (
119137 sizeof (rcutils_error_string_t ) == (
120138 RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH +
@@ -226,8 +244,12 @@ rcutils_set_error_state(const char * error_string, const char * file, size_t lin
226244 *
227245 * \param[in] msg The error message to be set.
228246 */
247+ #ifdef RCUTILS_AVOID_DYNAMIC_ALLOCATION
248+ #define RCUTILS_SET_ERROR_MSG (msg )
249+ #else
229250#define RCUTILS_SET_ERROR_MSG (msg ) \
230251 do {rcutils_set_error_state(msg, __FILE__, __LINE__);} while (0)
252+ #endif // RCUTILS_AVOID_DYNAMIC_ALLOCATION
231253
232254/// Set the error message using a format string and format arguments.
233255/**
@@ -238,6 +260,9 @@ rcutils_set_error_state(const char * error_string, const char * file, size_t lin
238260 * \param[in] format_string The string to be used as the format of the error message.
239261 * \param[in] ... Arguments for the format string.
240262 */
263+ #ifdef RCUTILS_AVOID_DYNAMIC_ALLOCATION
264+ #define RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING (format_string , ...)
265+ #else
241266#define RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING (format_string , ...) \
242267 do { \
243268 char output_msg[RCUTILS_ERROR_MESSAGE_MAX_LENGTH]; \
@@ -249,6 +274,8 @@ rcutils_set_error_state(const char * error_string, const char * file, size_t lin
249274 RCUTILS_SET_ERROR_MSG(output_msg); \
250275 } \
251276 } while (0)
277+ #endif // RCUTILS_AVOID_DYNAMIC_ALLOCATION
278+
252279
253280/// Indicate that the function intends to set an error message and return an error value.
254281/**
0 commit comments