Skip to content

Commit

Permalink
Fixed non-literal strings not being accepted as format string in asse…
Browse files Browse the repository at this point in the history
…rt macros
  • Loading branch information
Snaipe committed Sep 13, 2015
1 parent 98eb5e4 commit b5a55ac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
8 changes: 4 additions & 4 deletions include/criterion/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); \
Expand All @@ -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
Expand All @@ -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__; \
Expand Down
16 changes: 16 additions & 0 deletions include/criterion/preprocess.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__))
Expand All @@ -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, \
Expand Down
3 changes: 3 additions & 0 deletions src/asprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit b5a55ac

Please sign in to comment.