If ck_abort() detects a failure, the expression which was
evaluated is passed to _ck_assert_failed to print.
However, the previous behavior was to send the expression
to vsnprintf(), which would expect it to be properly formatted.
If the expression in ck_abort() contained % characters, such as:
ck_abort(bar%foo == 0)
the "%f" portion would be printed as some non-existent floating
point number.
Now, if there is no message passed (and the expression is to
be used) simply report the expression as is. Only format
something with vsnprintf() if there is actual data that
expects formatting.
git-svn-id: svn+ssh://svn.code.sf.net/p/check/code/trunk@1183
64e312b2-a51f-0410-8e61-
82d0ca0eb02a
In Development:
# Mentioning Check 0.9.14 for now, to fix distcheck target until next release
+ * Fix issue with string formatting in ck_assert(), where using
+ the % operator would be interpreted as a string formatter. Bug #96.
Sat July 26, 2014: Released Check 0.9.14
const char *msg;
va_list ap;
char buf[BUFSIZ];
+ const char *to_send;
send_loc_info(file, line);
va_start(ap, expr);
msg = (const char *)va_arg(ap, char *);
- if(msg == NULL)
- msg = expr;
- vsnprintf(buf, BUFSIZ, msg, ap);
+ /*
+ * If a message was passed, format it with vsnprintf.
+ * Otherwise, print the expression as is.
+ */
+ if(msg != NULL)
+ {
+ vsnprintf(buf, BUFSIZ, msg, ap);
+ to_send = buf;
+ }
+ else
+ {
+ to_send = expr;
+ }
+
va_end(ap);
- send_failure_info(buf);
+ send_failure_info(to_send);
if(cur_fork_status() == CK_FORK)
{
#if defined(HAVE_FORK) && HAVE_FORK==1
{ "Simple Tests", CK_FAILURE, "Failed" },
{ "Simple Tests", CK_FAILURE, "Assertion 'x == y' failed" },
{ "Simple Tests", CK_FAILURE, "Assertion '0' failed" },
+ { "Simple Tests", CK_FAILURE, "Assertion '1%f == 1' failed" },
{ "Simple Tests", CK_FAILURE, "Assertion 'x==y' failed: x==3, y==4" },
{ "Simple Tests", CK_FAILURE, "Assertion '3%d==2%f' failed: 3%d==1, 2%f==0" },
{ "Simple Tests", CK_FAILURE, "Assertion 'x!=y' failed: x==3, y==3" },
}
END_TEST
+START_TEST(test_ck_assert_with_mod)
+{
+ int f = 1;
+ record_failure_line_num(__LINE__);
+ ck_assert(1%f == 1);
+}
+END_TEST
+
START_TEST(test_ck_assert_int_eq)
{
int x = 3;
tcase_add_test (tc_simple, test_ck_abort_msg_null);
tcase_add_test (tc_simple, test_ck_assert);
tcase_add_test (tc_simple, test_ck_assert_null);
+ tcase_add_test (tc_simple, test_ck_assert_with_mod);
tcase_add_test (tc_simple, test_ck_assert_int_eq);
tcase_add_test (tc_simple, test_ck_assert_int_eq_with_mod);
tcase_add_test (tc_simple, test_ck_assert_int_ne);