aboutsummaryrefslogtreecommitdiff
path: root/tests/libtest
diff options
context:
space:
mode:
authorJay Satiro <raysatiro@yahoo.com>2016-03-09 02:59:05 -0500
committerDaniel Stenberg <daniel@haxx.se>2016-05-13 00:06:50 +0200
commit68701e51c1f794df59d9f70bcddbf714ee91b868 (patch)
tree5e76101c1e372d42fab764fdbf0b3d8a503baae6 /tests/libtest
parent117a0ffe9fbe2984d2f964a844a8e662586ae9b1 (diff)
mprintf: Fix processing of width and prec args
Prior to this change a width arg could be erroneously output, and also width and precision args could not be used together without crashing. "%0*d%s", 2, 9, "foo" Before: "092" After: "09foo" "%*.*s", 5, 2, "foo" Before: crash After: " fo" Test 557 is updated to verify this and more
Diffstat (limited to 'tests/libtest')
-rw-r--r--tests/libtest/lib557.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/libtest/lib557.c b/tests/libtest/lib557.c
index 2e724897e..5bdb8abe0 100644
--- a/tests/libtest/lib557.c
+++ b/tests/libtest/lib557.c
@@ -1374,6 +1374,53 @@ static int test_curl_off_t_formatting(void)
return failed;
}
+static int string_check(char *buf, const char *buf2)
+{
+ if(strcmp(buf, buf2)) {
+ /* they shouldn't differ */
+ printf("sprintf failed:\nwe '%s'\nsystem: '%s'\n",
+ buf, buf2);
+ return 1;
+ }
+ return 0;
+}
+
+/*
+ * The output strings in this test need to have been verified with a system
+ * sprintf() before used here.
+ */
+static int test_string_formatting(void)
+{
+ int errors = 0;
+ char buf[256];
+ curl_msnprintf(buf, sizeof(buf), "%0*d%s", 2, 9, "foo");
+ errors += string_check(buf, "09foo");
+
+ curl_msnprintf(buf, sizeof(buf), "%*.*s", 5, 2, "foo");
+ errors += string_check(buf, " fo");
+
+ curl_msnprintf(buf, sizeof(buf), "%*.*s", 2, 5, "foo");
+ errors += string_check(buf, "foo");
+
+ curl_msnprintf(buf, sizeof(buf), "%*.*s", 0, 10, "foo");
+ errors += string_check(buf, "foo");
+
+ curl_msnprintf(buf, sizeof(buf), "%-10s", "foo");
+ errors += string_check(buf, "foo ");
+
+ curl_msnprintf(buf, sizeof(buf), "%10s", "foo");
+ errors += string_check(buf, " foo");
+
+ curl_msnprintf(buf, sizeof(buf), "%*.*s", -10, -10, "foo");
+ errors += string_check(buf, "foo ");
+
+ if(!errors)
+ printf("All curl_mprintf() strings tests OK!\n");
+ else
+ printf("Some curl_mprintf() string tests Failed!\n");
+
+ return errors;
+}
int test(char *URL)
{
@@ -1394,6 +1441,8 @@ int test(char *URL)
errors += test_curl_off_t_formatting();
+ errors += test_string_formatting();
+
if(errors)
return TEST_ERR_MAJOR_BAD;
else