1 #ifndef STRACE_XSTRING_H
2 #define STRACE_XSTRING_H
7 #include "error_prints.h"
8 #include "gcc_compat.h"
11 * Print to static buffer and die on (really unexpected) errors and overflows.
12 * Shouldn't be used directly; please refer to helper macros xsnprintf and
15 * @param str String buffer to print into.
16 * @param size Size of the string buffer in bytes.
17 * @param func Function name from which this function is called.
18 * @param argstr Stringified arguments (including format argument).
19 * @param format Format string.
20 * @param ... Format arguments.
21 * @return Number of characters printed, excluding terminating null byte
22 * (the same as s(n)printf).
24 static inline int ATTRIBUTE_FORMAT((printf, 5, 6))
25 xsnprintf_(char *str, size_t size, const char *func, const char *argstr,
26 const char *format, ...)
32 ret = vsnprintf(str, size, format, ap);
35 if (ret < 0 || (unsigned int) ret >= size)
36 error_msg_and_die("%s: got unexpected return value %d for "
37 "snprintf(buf, %zu, %s)",
38 func, ret, size, argstr);
44 * snprintf that dies on (really unexpected) errors and overflows.
46 * @param str_ String buffer to print into.
47 * @param size_ Size of the string buffer in bytes.
48 * @param fmt_ Format string.
49 * @param ... Format arguments.
51 #define xsnprintf(str_, size_, fmt_, ...) \
52 xsnprintf_((str_), (size_), __func__, #fmt_ ", " #__VA_ARGS__, \
56 * Print to a character array buffer and die on (really unexpected) errors and
57 * overflows. Buffer size is obtained with sizeof().
59 * @param str_ Character array buffer to print into.
60 * @param fmt_ Format string.
61 * @param ... Format arguments.
63 #define xsprintf(str_, fmt_, ...) \
64 xsnprintf((str_), sizeof(str_) + MUST_BE_ARRAY(str_), (fmt_), \
68 get_pos_diff_(char *str, size_t size, char *pos, const char *func,
71 if ((str + size) < str)
72 error_msg_and_die("%s: string size overflow (%p+%zu) in %s",
73 func, str, size, call);
75 if (pos > (str + size))
76 error_msg_and_die("%s: got position (%p) beyond string "
78 func, pos, str, size, call);
81 error_msg_and_die("%s: got position %p before string %p in %s",
82 func, pos, str, call);
88 * Helper function for constructing string in a character array by appending
89 * new formatted parts. Returns new position. Fails on error or buffer
90 * overflow, in line with the rest of x* functions. Obtains buffer size via
93 * @param str_ Character array buffer to print into.
94 * @param pos_ Current position.
95 * @param fmt_ Format string.
96 * @param ... Format arguments.
97 * @return New position.
99 #define xappendstr(str_, pos_, fmt_, ...) \
100 (xsnprintf((pos_), sizeof(str_) + MUST_BE_ARRAY(str_) - \
101 get_pos_diff_((str_), sizeof(str_), (pos_), __func__, \
102 "xappendstr(" #str_ ", " #pos_ ", " #fmt_ ", " \
104 (fmt_), ##__VA_ARGS__) + (pos_))
106 #endif /* !STRACE_XSTRING_H */