summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@who-t.net>2020-06-16 10:34:07 +1000
committerPeter Hutterer <peter.hutterer@who-t.net>2020-07-06 15:15:20 +1000
commit9b1b0c574322c0e27ab281a1aeff5499df637048 (patch)
treeae7e5f27e3b6a1c0ffd9b431b7f64511cee1d87d /src
parentd075c3e6971a10fd7e0bd44bbf5a5c9d610159d7 (diff)
downloadxorg-lib-libxkbcommon-9b1b0c574322c0e27ab281a1aeff5499df637048.tar.gz
Add a snprintf_safe() helper function
Returns true on success or false on error _or_ truncation. Since truncation is almost always an error anyway, we might as well make this easier to check. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Diffstat (limited to 'src')
-rw-r--r--src/utils.h15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/utils.h b/src/utils.h
index 2c35a87..abeaabd 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -26,6 +26,7 @@
#include <errno.h>
#include <inttypes.h>
+#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
@@ -290,4 +291,18 @@ int vasprintf(char **strp, const char *fmt, va_list ap);
# endif /* !HAVE_VASPRINTF */
#endif /* !HAVE_ASPRINTF */
+static inline bool
+ATTR_PRINTF(3, 4)
+snprintf_safe(char *buf, size_t sz, const char *format, ...)
+{
+ va_list ap;
+ int rc;
+
+ va_start(ap, format);
+ rc = vsnprintf(buf, sz, format, ap);
+ va_end(ap);
+
+ return rc >= 0 && (size_t)rc < sz;
+}
+
#endif /* UTILS_H */