]> git.kaiwu.me - klib.git/commitdiff
kstring: Add printf format attribute to kvsprintf() and ksprintf()
authorYorhel <git@yorhel.nl>
Tue, 7 May 2013 10:03:48 +0000 (12:03 +0200)
committerYorhel <git@yorhel.nl>
Tue, 7 May 2013 10:05:25 +0000 (12:05 +0200)
This allows compile-time detection of format-string related bugs.
Although the macros only check for the GCC version, clang (and perhaps
other compilers too) have these macros defined and can recognize
the formatting attributes as well.

kstring.h

index ef69a6b1ebbb623f131744e42f618d28bd644111..13a555972029820d3a503402a8700b0266b42112 100644 (file)
--- a/kstring.h
+++ b/kstring.h
 #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
 #endif
 
+#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
+#define KS_ATTR_PRINTF(fmt, arg) __attribute__((__format__ (__printf__, fmt, arg)))
+#else
+#define KS_ATTR_PRINTF(fmt, arg)
+#endif
+
+
 #ifndef KSTRING_T
 #define KSTRING_T kstring_t
 typedef struct __kstring_t {
@@ -53,8 +60,8 @@ typedef struct {
 extern "C" {
 #endif
 
-       int kvsprintf(kstring_t *s, const char *fmt, va_list ap);
-       int ksprintf(kstring_t *s, const char *fmt, ...);
+       int kvsprintf(kstring_t *s, const char *fmt, va_list ap) KS_ATTR_PRINTF(2,0);
+       int ksprintf(kstring_t *s, const char *fmt, ...) KS_ATTR_PRINTF(2,3);
        int ksplit_core(char *s, int delimiter, int *_max, int **_offsets);
        char *kstrstr(const char *str, const char *pat, int **_prep);
        char *kstrnstr(const char *str, const char *pat, int n, int **_prep);