From b5a55ac523ae04d32aa257d415856b6216b64a79 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Sun, 13 Sep 2015 20:24:29 +0200 Subject: [PATCH] Fixed non-literal strings not being accepted as format string in assert macros --- include/criterion/assert.h | 8 ++++---- include/criterion/preprocess.h | 16 ++++++++++++++++ src/asprintf.c | 3 +++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/include/criterion/assert.h b/include/criterion/assert.h index f88842b7..73418e7b 100644 --- a/include/criterion/assert.h +++ b/include/criterion/assert.h @@ -89,12 +89,12 @@ CR_END_C_API "" CR_TRANSLATE_DEF_MSG__(CR_VA_HEAD(CR_VA_TAIL(__VA_ARGS__))) \ )) -# define CR_INIT_STATS_(BufSize, MsgVar, ...) CR_EXPAND( \ +# define CR_INIT_STATS_(BufSize, MsgVar, ...) \ do { \ char *def_msg = CR_EXPAND(CR_TRANSLATE_DEF_MSG_(__VA_ARGS__)); \ char *formatted_msg = NULL; \ int msglen = cr_asprintf(&formatted_msg, \ - "" CR_VA_TAIL(CR_VA_TAIL(__VA_ARGS__))); \ + CR_EXPAND(CR_VA_REPL_TAIL2("", __VA_ARGS__))); \ if (formatted_msg && *formatted_msg) { \ MsgVar = formatted_msg; \ CR_STDN free(def_msg); \ @@ -115,7 +115,7 @@ CR_END_C_API buf += sizeof (size_t); \ CR_STDN strcpy(buf, MsgVar); \ CR_STDN free(MsgVar); \ - } while (0)) + } while (0) # define CR_FAIL_ABORT_ criterion_abort_test # define CR_FAIL_CONTINUES_ criterion_continue_test @@ -134,7 +134,7 @@ CR_END_C_API size_t bufsize; \ \ struct criterion_assert_stats *stat; \ - CR_EXPAND(CR_INIT_STATS_(bufsize, msg, CR_VA_TAIL(__VA_ARGS__))); \ + CR_INIT_STATS_(bufsize, msg, CR_EXPAND(CR_VA_TAIL(__VA_ARGS__))); \ stat->passed = passed; \ stat->file = __FILE__; \ stat->line = __LINE__; \ diff --git a/include/criterion/preprocess.h b/include/criterion/preprocess.h index aee370a3..b2c671b5 100644 --- a/include/criterion/preprocess.h +++ b/include/criterion/preprocess.h @@ -38,6 +38,13 @@ # define CR_STR(x) CR_EXPAND(CR_STR_(x)) # define CR_STR_(x) #x +# define CR_VA_REPL_TAIL2(Repl, ...) CR_EXPAND(CR_VA_REPL_TAIL2_HELPER(Repl, CR_VA_REPL_TAIL2_SELECT(__VA_ARGS__), __VA_ARGS__)) + +# define CR_VA_REPL_TAIL2_HELPER(Repl, N, ...) CR_EXPAND(CR_VA_REPL_TAIL2_HELPER_(Repl, N, __VA_ARGS__)) +# define CR_VA_REPL_TAIL2_HELPER_(Repl, N, ...) CR_EXPAND(CR_VA_REPL_TAIL2_HELPER_##N(Repl, __VA_ARGS__)) +# define CR_VA_REPL_TAIL2_HELPER_1(Repl, ...) Repl +# define CR_VA_REPL_TAIL2_HELPER_2(Repl, ...) CR_IDENTITY(CR_VA_TAIL(CR_VA_TAIL(__VA_ARGS__))) + # define CR_VA_TAIL(...) CR_EXPAND(CR_VA_TAIL_HELPER(CR_VA_TAIL_SELECT(__VA_ARGS__), __VA_ARGS__)) # define CR_VA_TAIL_HELPER(N, ...) CR_EXPAND(CR_VA_TAIL_HELPER_(N, __VA_ARGS__)) @@ -61,6 +68,15 @@ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, \ 2, 2, 1, _)) +# define CR_VA_REPL_TAIL2_SELECT(...) CR_EXPAND(CR_VA_TAIL_SELECT64(__VA_ARGS__, \ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, \ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, \ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, \ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, \ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, \ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, \ + 1, 1, 1, _)) + # define CR_VA_TAIL_SELECT64( \ _01, _02, _03, _04, _05, _06, _07, _08, _09, _10, \ _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \ diff --git a/src/asprintf.c b/src/asprintf.c index c2a4a126..f7753cc0 100644 --- a/src/asprintf.c +++ b/src/asprintf.c @@ -36,6 +36,9 @@ int cr_asprintf(char **strp, const char *fmt, ...) { } int cr_vasprintf(char **strp, const char *fmt, va_list ap) { + if (!fmt) + return 0; + va_list vl; va_copy(vl, ap);