@@ -39,19 +39,21 @@ extern "C"
3939#include "rcutils/testing/fault_injection.h"
4040#include "rcutils/types/rcutils_ret.h"
4141#include "rcutils/visibility_control.h"
42+ #include "rcutils/configuration_flags.h"
4243
43- #ifdef __STDC_LIB_EXT1__
44+ #if defined( __STDC_LIB_EXT1__ ) && !defined( RCUTILS_NO_FILESYSTEM )
4445/// Write the given msg out to stderr, limiting the buffer size in the `fwrite`.
4546/**
4647 * This ensures that there is an upper bound to a buffer overrun if `msg` is
4748 * non-null terminated.
4849 */
4950#define RCUTILS_SAFE_FWRITE_TO_STDERR (msg ) \
5051 do {fwrite(msg, sizeof(char), strnlen_s(msg, 4096), stderr);} while (0)
51- #else
52- /// Write the given msg out to stderr.
52+ #elif !defined(RCUTILS_NO_FILESYSTEM )
5353#define RCUTILS_SAFE_FWRITE_TO_STDERR (msg ) \
5454 do {fwrite(msg, sizeof(char), strlen(msg), stderr);} while (0)
55+ #else
56+ #define RCUTILS_SAFE_FWRITE_TO_STDERR (msg )
5557#endif
5658
5759/// Set the error message to stderr using a format string and format arguments.
@@ -63,6 +65,8 @@ extern "C"
6365 * \param[in] format_string The string to be used as the format of the error message.
6466 * \param[in] ... Arguments for the format string.
6567 */
68+
69+ #if !defined(RCUTILS_AVOID_DYNAMIC_ALLOCATION )
6670#define RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING (format_string , ...) \
6771 do { \
6872 char output_msg[RCUTILS_ERROR_MESSAGE_MAX_LENGTH]; \
@@ -73,7 +77,11 @@ extern "C"
7377 RCUTILS_SAFE_FWRITE_TO_STDERR(output_msg); \
7478 } \
7579 } while (0)
80+ #else
81+ #define RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING (format_string , ...)
82+ #endif
7683
84+ #if !defined(RCUTILS_AVOID_DYNAMIC_ALLOCATION )
7785/// The maximum length a formatted number is allowed to have.
7886#define RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH 20 // "18446744073709551615"
7987
@@ -100,6 +108,13 @@ extern "C"
100108 RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH - \
101109 RCUTILS_ERROR_FORMATTING_CHARACTERS - \
102110 1)
111+ #else
112+ #define RCUTILS_ERROR_STATE_LINE_NUMBER_STR_MAX_LENGTH 1
113+ #define RCUTILS_ERROR_FORMATTING_CHARACTERS 1
114+ #define RCUTILS_ERROR_MESSAGE_MAX_LENGTH 1
115+ #define RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH 1
116+ #define RCUTILS_ERROR_STATE_FILE_MAX_LENGTH 1
117+ #endif // RCUTILS_AVOID_DYNAMIC_ALLOCATION
103118
104119/// Struct wrapping a fixed-size c string used for returning the formatted error string.
105120typedef struct rcutils_error_string_s
@@ -121,7 +136,7 @@ typedef struct rcutils_error_state_s
121136} rcutils_error_state_t ;
122137
123138// make sure our math is right...
124- #if __STDC_VERSION__ >= 201112L
139+ #if __STDC_VERSION__ >= 201112L && !defined( RCUTILS_AVOID_DYNAMIC_ALLOCATION )
125140static_assert (
126141 sizeof (rcutils_error_string_t ) == (
127142 RCUTILS_ERROR_STATE_MESSAGE_MAX_LENGTH +
@@ -233,8 +248,12 @@ rcutils_set_error_state(const char * error_string, const char * file, size_t lin
233248 *
234249 * \param[in] msg The error message to be set.
235250 */
251+ #ifdef RCUTILS_AVOID_DYNAMIC_ALLOCATION
252+ #define RCUTILS_SET_ERROR_MSG (msg )
253+ #else
236254#define RCUTILS_SET_ERROR_MSG (msg ) \
237255 do {rcutils_set_error_state(msg, __FILE__, __LINE__);} while (0)
256+ #endif // RCUTILS_AVOID_DYNAMIC_ALLOCATION
238257
239258/// Set the error message using a format string and format arguments.
240259/**
@@ -245,6 +264,9 @@ rcutils_set_error_state(const char * error_string, const char * file, size_t lin
245264 * \param[in] format_string The string to be used as the format of the error message.
246265 * \param[in] ... Arguments for the format string.
247266 */
267+ #ifdef RCUTILS_AVOID_DYNAMIC_ALLOCATION
268+ #define RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING (format_string , ...)
269+ #else
248270#define RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING (format_string , ...) \
249271 do { \
250272 char output_msg[RCUTILS_ERROR_MESSAGE_MAX_LENGTH]; \
@@ -255,6 +277,8 @@ rcutils_set_error_state(const char * error_string, const char * file, size_t lin
255277 RCUTILS_SET_ERROR_MSG(output_msg); \
256278 } \
257279 } while (0)
280+ #endif // RCUTILS_AVOID_DYNAMIC_ALLOCATION
281+
258282
259283/// Indicate that the function intends to set an error message and return an error value.
260284/**
0 commit comments