]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
string_utils: add wrapper for snprintf()
authorChristian Brauner <christian.brauner@ubuntu.com>
Thu, 11 Feb 2021 09:14:34 +0000 (10:14 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Thu, 11 Feb 2021 09:14:34 +0000 (10:14 +0100)
This let's us avoid the tedious

if (ret < 0 || (size_t)ret >= sizeof(buf))

style of error checking.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/string_utils.h

index a489cac445988104e5ffad180383307a9c0eb172..dd086cee70e69140f4c70be0433ff823063838e9 100644 (file)
@@ -140,4 +140,13 @@ static inline bool strequal(const char *str, const char *eq)
        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 */