string_utils: add wrapper for snprintf()

This let's us avoid the tedious if (ret < 0 || (size_t)ret >= sizeof(buf)) style of error checking. Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent accd6291
...@@ -138,4 +138,13 @@ static inline bool strequal(const char *str, const char *eq) ...@@ -138,4 +138,13 @@ static inline bool strequal(const char *str, const char *eq)
return strcmp(str, eq) == 0; return strcmp(str, eq) == 0;
} }
#define strnprintf(buf, buf_size, ...) \
({ \
int __ret_strnprintf; \
__ret_strnprintf = snprintf(buf, buf_size, ##__VA_ARGS__); \
if (__ret_strnprintf < 0 || (size_t)__ret_strnprintf >= buf_size) \
__ret_strnprintf = ret_errno(EIO); \
__ret_strnprintf; \
})
#endif /* __LXC_STRING_UTILS_H */ #endif /* __LXC_STRING_UTILS_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment