]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/basic/stdio-util.h
stdio-util: give snprintf_ok() some love
[thirdparty/systemd.git] / src / basic / stdio-util.h
index 0ed48b3fd4b155fe6ae777e08e474ffdd85b58e8..69d7062ec6f38eeac130c4a25c51e629053deba5 100644 (file)
@@ -9,8 +9,13 @@
 #include "macro.h"
 #include "memory-util.h"
 
-#define snprintf_ok(buf, len, fmt, ...) \
-        ((size_t) snprintf(buf, len, fmt, __VA_ARGS__) < (len))
+#define snprintf_ok(buf, len, fmt, ...)                                \
+        ({                                                             \
+                char *_buf = (buf);                                    \
+                size_t _len = (len);                                   \
+                int _snpf = snprintf(_buf, _len, (fmt), __VA_ARGS__);  \
+                _snpf >= 0 && (size_t) _snpf < _len ? _buf : NULL;     \
+        })
 
 #define xsprintf(buf, fmt, ...) \
         assert_message_se(snprintf_ok(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__), "xsprintf: " #buf "[] must be big enough")