1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24 #include "curlcheck.h"
25
26 #if defined(__GNUC__) || defined(__clang__)
27 #pragma GCC diagnostic push
28 #pragma GCC diagnostic ignored "-Wformat"
29 #endif
30
unit_setup(void)31 static CURLcode unit_setup(void) {return CURLE_OK;}
unit_stop(void)32 static void unit_stop(void) {}
33
34 UNITTEST_START
35
36 int rc;
37 char buf[3] = {'b', 'u', 'g'};
38 const char *str = "bug";
39 int width = 3;
40 char output[130];
41
42 /*#define curl_msnprintf snprintf */
43
44 /* without a trailing zero */
45 rc = curl_msnprintf(output, 4, "%.*s", width, buf);
46 fail_unless(rc == 3, "return code should be 3");
47 fail_unless(!strcmp(output, "bug"), "wrong output");
48
49 /* with a trailing zero */
50 rc = curl_msnprintf(output, 4, "%.*s", width, str);
51 fail_unless(rc == 3, "return code should be 3");
52 fail_unless(!strcmp(output, "bug"), "wrong output");
53
54 width = 2;
55 /* one byte less */
56 rc = curl_msnprintf(output, 4, "%.*s", width, buf);
57 fail_unless(rc == 2, "return code should be 2");
58 fail_unless(!strcmp(output, "bu"), "wrong output");
59
60 /* string with larger precision */
61 rc = curl_msnprintf(output, 8, "%.8s", str);
62 fail_unless(rc == 3, "return code should be 3");
63 fail_unless(!strcmp(output, "bug"), "wrong output");
64
65 /* longer string with precision */
66 rc = curl_msnprintf(output, 8, "%.3s", "0123456789");
67 fail_unless(rc == 3, "return code should be 3");
68 fail_unless(!strcmp(output, "012"), "wrong output");
69
70 /* negative width */
71 rc = curl_msnprintf(output, 8, "%-8s", str);
72 fail_unless(rc == 7, "return code should be 7");
73 fail_unless(!strcmp(output, "bug "), "wrong output");
74
75 /* larger width that string length */
76 rc = curl_msnprintf(output, 8, "%8s", str);
77 fail_unless(rc == 7, "return code should be 7");
78 fail_unless(!strcmp(output, " bu"), "wrong output");
79
80 /* output a number in a limited output */
81 rc = curl_msnprintf(output, 4, "%d", 10240);
82 fail_unless(rc == 3, "return code should be 3");
83 fail_unless(!strcmp(output, "102"), "wrong output");
84
85 /* padded strings */
86 rc = curl_msnprintf(output, 16, "%8s%8s", str, str);
87 fail_unless(rc == 15, "return code should be 15");
88 fail_unless(!strcmp(output, " bug bu"), "wrong output");
89
90 /* padded numbers */
91 rc = curl_msnprintf(output, 16, "%8d%8d", 1234, 5678);
92 fail_unless(rc == 15, "return code should be 15");
93 fail_unless(!strcmp(output, " 1234 567"), "wrong output");
94
95 /* double precision */
96 rc = curl_msnprintf(output, 24, "%2$.*1$.99d", 3, 5678);
97 fail_unless(rc == 0, "return code should be 0");
98
99 /* 129 input % flags */
100 rc = curl_msnprintf(output, 130,
101 "%s%s%s%s%s%s%s%s%s%s" /* 10 */
102 "%s%s%s%s%s%s%s%s%s%s" /* 20 */
103 "%s%s%s%s%s%s%s%s%s%s" /* 30 */
104 "%s%s%s%s%s%s%s%s%s%s" /* 40 */
105 "%s%s%s%s%s%s%s%s%s%s" /* 50 */
106 "%s%s%s%s%s%s%s%s%s%s" /* 60 */
107 "%s%s%s%s%s%s%s%s%s%s" /* 70 */
108 "%s%s%s%s%s%s%s%s%s%s" /* 80 */
109 "%s%s%s%s%s%s%s%s%s%s" /* 90 */
110 "%s%s%s%s%s%s%s%s%s%s" /* 100 */
111 "%s%s%s%s%s%s%s%s%s%s" /* 110 */
112 "%s%s%s%s%s%s%s%s%s%s" /* 120 */
113 "%s%s%s%s%s%s%s%s%s", /* 129 */
114
115 "a", "", "", "", "", "", "", "", "", "", /* 10 */
116 "b", "", "", "", "", "", "", "", "", "", /* 20 */
117 "c", "", "", "", "", "", "", "", "", "", /* 30 */
118 "d", "", "", "", "", "", "", "", "", "", /* 40 */
119 "e", "", "", "", "", "", "", "", "", "", /* 50 */
120 "f", "", "", "", "", "", "", "", "", "", /* 60 */
121 "g", "", "", "", "", "", "", "", "", "", /* 70 */
122 "h", "", "", "", "", "", "", "", "", "", /* 80 */
123 "i", "", "", "", "", "", "", "", "", "", /* 90 */
124 "j", "", "", "", "", "", "", "", "", "", /* 100 */
125 "k", "", "", "", "", "", "", "", "", "", /* 110 */
126 "l", "", "", "", "", "", "", "", "", "", /* 120 */
127 "m", "", "", "", "", "", "", "", "" /* 129 */
128 );
129 fail_unless(rc == 0, "return code should be 0");
130
131 /* 128 input % flags */
132 rc = curl_msnprintf(output, 130,
133 "%s%s%s%s%s%s%s%s%s%s" /* 10 */
134 "%s%s%s%s%s%s%s%s%s%s" /* 20 */
135 "%s%s%s%s%s%s%s%s%s%s" /* 30 */
136 "%s%s%s%s%s%s%s%s%s%s" /* 40 */
137 "%s%s%s%s%s%s%s%s%s%s" /* 50 */
138 "%s%s%s%s%s%s%s%s%s%s" /* 60 */
139 "%s%s%s%s%s%s%s%s%s%s" /* 70 */
140 "%s%s%s%s%s%s%s%s%s%s" /* 80 */
141 "%s%s%s%s%s%s%s%s%s%s" /* 90 */
142 "%s%s%s%s%s%s%s%s%s%s" /* 100 */
143 "%s%s%s%s%s%s%s%s%s%s" /* 110 */
144 "%s%s%s%s%s%s%s%s%s%s" /* 120 */
145 "%s%s%s%s%s%s%s%s", /* 128 */
146
147 "a", "", "", "", "", "", "", "", "", "", /* 10 */
148 "b", "", "", "", "", "", "", "", "", "", /* 20 */
149 "c", "", "", "", "", "", "", "", "", "", /* 30 */
150 "d", "", "", "", "", "", "", "", "", "", /* 40 */
151 "e", "", "", "", "", "", "", "", "", "", /* 50 */
152 "f", "", "", "", "", "", "", "", "", "", /* 60 */
153 "g", "", "", "", "", "", "", "", "", "", /* 70 */
154 "h", "", "", "", "", "", "", "", "", "", /* 80 */
155 "i", "", "", "", "", "", "", "", "", "", /* 90 */
156 "j", "", "", "", "", "", "", "", "", "", /* 100 */
157 "k", "", "", "", "", "", "", "", "", "", /* 110 */
158 "l", "", "", "", "", "", "", "", "", "", /* 120 */
159 "m", "", "", "", "", "", "", "" /* 128 */
160 );
161 fail_unless(rc == 13, "return code should be 13");
162
163 /* 129 output segments */
164 rc = curl_msnprintf(output, 130,
165 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 20 */
166 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 40 */
167 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 60 */
168 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 80 */
169 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 100 */
170 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 120 */
171 "%%%%%%%%%%%%%%%%%%" /* 129 */
172 );
173 fail_unless(rc == 0, "return code should be 0");
174
175 /* 128 output segments */
176 rc = curl_msnprintf(output, 129,
177 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 20 */
178 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 40 */
179 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 60 */
180 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 80 */
181 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 100 */
182 "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" /* 120 */
183 "%%%%%%%%%%%%%%%%" /* 128 */
184 );
185 fail_unless(rc == 128, "return code should be 128");
186
187 UNITTEST_STOP
188
189 #if defined(__GNUC__) || defined(__clang__)
190 #pragma GCC diagnostic pop
191 #endif
192