Debian Codegraph @

gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-1.c

raw
1 /* { dg-do compile } */
2 /* { dg-options "-Wformat -Wformat-overflow=1 -ftrack-macro-expansion=0" } */
3 /* { dg-require-effective-target int32plus } */
4
5 /* When debugging, define LINE to the line number of the test case to exercise
6 and avoid exercising any of the others. The buffer and objsize macros
7 below make use of LINE to avoid warnings for other lines. */
8 #ifndef LINE
9 # define LINE 0
10 #endif
11
12 #define INT_MAX __INT_MAX__
13
14 typedef __builtin_va_list va_list;
15
16
17 char buffer [256];
18 extern char *ptr;
19
20 /* Evaluate to an array of SIZE characters when non-negative, or to
21 a pointer to an unknown object otherwise. */
22 #define buffer(size) \
23 ((0 <= size) ? buffer + sizeof buffer - (size) : ptr)
24
25 /* Evaluate to SIZE when non-negative, or to SIZE_MAX otherise. */
26 #define objsize(size) ((0 <= size) ? (size) : __SIZE_MAX__)
27
28 typedef __SIZE_TYPE__ size_t;
29
30 #if !__cplusplus
31 typedef __WCHAR_TYPE__ wchar_t;
32 #endif
33
34 typedef __WINT_TYPE__ wint_t;
35
36 typedef unsigned char UChar;
37
38 /* Constants used to verify the pass can determine their values even
39 without optimization. */
40 const int cst0 = 0;
41 const int cst1 = 1;
42 const int cst10 = 10;
43
44 /* Initialized global variables used to verify that the pass doesn't
45 use their initial values (they could be modified by calls to other
46 functions). */
47 int var0 = 0;
48 int var10 = 10;
49
50 const char s0[] = "";
51 const char s1[] = "1";
52 const char s2[] = "12";
53 const char s3[] = "123";
54 const char s4[] = "1234";
55 const char s5[] = "12345";
56 const char s6[] = "123456";
57 const char s7[] = "1234567";
58 const char s8[] = "12345678";
59
60 void sink (void*, ...);
61
62 int dummy_sprintf (char*, const char*, ...);
63 int dummy_snprintf (char*, size_t, const char*, ...);
64 int dummy_vsprintf (char*, const char*, va_list);
65 int dummy_vsnprintf (char*, size_t, const char*, va_list);
66 int dummy___sprintf_chk (char*, int, size_t, const char*, ...);
67 int dummy___snprintf_chk (char*, size_t, int, size_t, const char*, ...);
68 int dummy___vsprintf_chk (char*, int, size_t, const char*, va_list);
69 int dummy___vsnprintf_chk (char*, size_t, int, size_t, const char*, va_list);
70
71 /* Helper to expand function to either __builtin_f or dummy_f to
72 make debugging GCC easy. */
73 #define FUNC(f) \
74 ((!LINE || LINE == __LINE__) ? __builtin_ ## f : dummy_ ## f)
75
76 /* Macro to verify that calls to __builtin_sprintf (i.e., with no size
77 argument) issue diagnostics by correctly determining the size of
78 the destination buffer. */
79 #define T(size, ...) \
80 (FUNC (sprintf) (buffer (size), __VA_ARGS__), \
81 sink (buffer, ptr))
82
83 /* Exercise the "%%" directive. */
84
85 void test_sprintf_percent (void)
86 {
87 T (-1, "%%");
88 T ( 0, "%%"); /* { dg-warning ".%%. directive writing 1 byte into a region of size 0" } */
89 T ( 1, "%%"); /* { dg-warning "writing a terminating nul past the end" } */
90 T ( 2, "%%");
91 T ( 2, "%%%%"); /* { dg-warning "writing a terminating nul past the end" } */
92 T ( 3, "%%%%");
93 T ( 3, "%%X%%"); /* { dg-warning "writing a terminating nul past the end" } */
94 T ( 4, "%%X%%");
95 }
96
97 /* Exercise the "%c" and "%lc" directive with constant arguments. */
98
99 void test_sprintf_c_const (void)
100 {
101 T (-1, "%c", 0); /* No warning for unknown destination size. */
102 T ( 0, "%c", 0); /* { dg-warning ".%c. directive writing 1 byte into a region of size 0" } */
103 T ( 1, "%c", 0); /* { dg-warning "writing a terminating nul past the end" } */
104 T ( 1, "%c", '1'); /* { dg-warning "nul past the end" } */
105 T ( 2, "%c", '1');
106 T ( 2, "%2c", '1'); /* { dg-warning "nul past the end" } */
107 T ( 2, "%3c", '1'); /* { dg-warning "into a region" } */
108 T ( 2, "%c%c", '1', '2'); /* { dg-warning "nul past the end" } */
109 T ( 3, "%c%c", '1', '2');
110
111 T ( 2, "%1$c%2$c", '1', '2'); /* { dg-warning "does not support %n.|nul past the end" } */
112 T ( 3, "%1$c%2$c", '1', '2');
113
114 /* Verify that a warning is issued for exceeding INT_MAX bytes and
115 not otherwise. In ILP32 the maximum object size is INT_MAX - 1
116 bytes so the calls are diagnosed due to the overflow. */
117 T (-1, "%*c", INT_MAX - 1, '1');
118 T (-1, "%*c", INT_MAX, '1'); /* { dg-warning "writing a terminating nul past the end " "ilp32" { target ilp32 } } */
119 T (-1, "X%*c", INT_MAX - 1, '1'); /* { dg-warning "writing a terminating nul past the end " "ilp32" { target ilp32 } } */
120 T (-1, "X%*c", INT_MAX, '1'); /* { dg-warning "directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." "lp64" { target lp64 } } */
121 /* { dg-warning "directive writing 2147483647 bytes into a region of size 2147483646" "ilp32" { target ilp32 } .-1 } */
122
123 T (-1, "%*c%*c", INT_MAX - 1, '1', INT_MAX - 1, '2'); /* { dg-warning "directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." "lp64" { target lp64 } } */
124 /* { dg-warning "directive writing 2147483646 bytes into a region of size 1" "ilp32" { target ilp32 } .-1 } */
125
126 T (-1, "%*cX", INT_MAX - 2, '1');
127 T (-1, "%*cX", INT_MAX - 1, '1'); /* { dg-warning "writing a terminating nul past the end of the destination" "ilp32" { target ilp32 } } */
128 T (-1, "%*cX", INT_MAX, '1'); /* { dg-warning "output of \[0-9\]+ bytes causes result to exceed .INT_MAX." "lp64" { target lp64 } } */
129 /* { dg-warning "directive writing 1 byte into a region of size 0" "ilp32" { target ilp32 } .-1 } */
130 }
131
132 /* Verify that no warning is issued for calls that write into a flexible
133 array member whose size isn't known. Also verify that calls that use
134 a flexible array member as an argument to the "%s" directive do not
135 cause a warning. */
136
137 void test_sprintf_flexarray (void *p, int i)
138 {
139 struct S
140 {
141 int n;
142 char a[];
143 } *s = p;
144
145 FUNC (sprintf)(s->a, "%c", 'x');
146
147 FUNC (sprintf)(s->a, "%-s", "");
148 FUNC (sprintf)(s->a, "%-s", "abc");
149 FUNC (sprintf)(s->a, "abc%sghi", "def");
150
151 FUNC (sprintf)(s->a, "%i", 1234);
152
153 FUNC (sprintf)(buffer (1), "%-s", s->a);
154 FUNC (sprintf)(buffer (1), "%-s", s [i].a);
155 FUNC (sprintf)(buffer (2), "%-s", s->a);
156 FUNC (sprintf)(buffer (2), "%-s", s [i].a);
157 }
158
159 /* Same as above but for zero-length arrays. */
160
161 void test_sprintf_zero_length_array (void *p, int i)
162 {
163 struct S
164 {
165 int n;
166 char a [0];
167 } *s = p;
168
169 FUNC (sprintf)(s->a, "%c", 'x');
170
171 FUNC (sprintf)(s->a, "%s", "");
172 FUNC (sprintf)(s->a, "%s", "abc");
173 FUNC (sprintf)(s->a, "abc%sghi", "def");
174
175 FUNC (sprintf)(s->a, "%i", 1234);
176
177 FUNC (sprintf)(buffer (1), "%s", s->a);
178 FUNC (sprintf)(buffer (1), "%s", s [i].a);
179 }
180
181 #undef T
182 #define T(size, fmt, ...) \
183 (FUNC (__sprintf_chk) (buffer (size), 0, objsize (size), fmt, __VA_ARGS__), \
184 sink (buffer, ptr))
185
186 /* Exercise the "%c" and "%lc" directive with constant arguments. */
187
188 void test_sprintf_chk_c_const (void)
189 {
190 T (-1, "%c", 0); /* No warning for unknown destination size. */
191 /* Verify the full text of the diagnostic for just the distinct messages
192 and use abbreviations in subsequent test cases. */
193 T (0, "%c", 0); /* { dg-warning ".%c. directive writing 1 byte into a region of size 0" } */
194 T (1, "%c", 0); /* { dg-warning "writing a terminating nul past the end" } */
195 T (1, "%c", '1'); /* { dg-warning "nul past the end" } */
196 T (2, "%c", '1');
197 T (2, "%2c", '1'); /* { dg-warning "nul past the end" } */
198 T (2, "%3c", '1'); /* { dg-warning "into a region" } */
199 T (2, "%c%c", '1', '2'); /* { dg-warning "nul past the end" } */
200 T (3, "%c%c", '1', '2');
201
202 /* Wide characters. */
203 T (0, "%lc", (wint_t)0); /* { dg-warning ".%lc. directive writing up to 1 bytes into a region of size 0" } */
204 T (1, "%lc", (wint_t)0); /* { dg-warning "nul past the end" } */
205 T (1, "%lc%lc", (wint_t)0, (wint_t)0); /* { dg-warning ".%lc. directive writing up to 1 bytes into a region of size between 0 and 1" } */
206 T (2, "%lc", (wint_t)0);
207 T (2, "%lc%lc", (wint_t)0, (wint_t)0); /* { dg-warning "nul past the end" } */
208
209 /* The following could result in as few as no bytes and in as many as
210 MB_CUR_MAX, but since the MB_CUR_MAX value is a runtime property
211 the write cannot be reliably diagnosed. */
212 T (2, "%lc", (wint_t)L'1');
213 T (2, "%1lc", (wint_t)L'1');
214 /* Writing some unknown number of bytes into a field two characters wide. */
215 T (2, "%2lc", (wint_t)L'1'); /* { dg-warning "nul past the end" } */
216
217 T (3, "%lc%c", (wint_t)L'1', '2');
218 /* Here in the best case each argument will format as single character,
219 causing the terminating NUL to be written past the end. */
220 T (3, "%lc%c%c", (wint_t)L'1', '2', '3'); /* { dg-warning "nul past the end" } */
221 T (3, "%lc%lc%c", (wint_t)L'1', (wint_t)L'2', '3'); /* { dg-warning "nul past the end" } */
222 }
223
224 /* Exercise the "%s" and "%ls" directive with constant arguments. */
225
226 void test_sprintf_chk_s_const (void)
227 {
228 T (-1, "%*s", 0, ""); /* No warning for unknown destination size. */
229 T ( 0, "%*s", 0, ""); /* { dg-warning "nul past the end" } */
230 T ( 0, "%-s", ""); /* { dg-warning "nul past the end" } */
231 T ( 0, "%*s", 0, s0); /* { dg-warning "nul past the end" } */
232 T ( 1, "%*s", 0, "");
233 T ( 1, "%*s", 0, s0);
234 T ( 1, "%*s", 0, "\0");
235 T ( 1, "%*s", 0, "1"); /* { dg-warning "nul past the end" } */
236 T ( 1, "%*s", 0, s1); /* { dg-warning "nul past the end" } */
237 T ( 1, "%1s", ""); /* { dg-warning "nul past the end" } */
238 T ( 1, "%1s", s0); /* { dg-warning "nul past the end" } */
239 T (-1, "%1s", "1"); /* No warning for unknown destination size. */
240 T ( 1, "%*s", 1, ""); /* { dg-warning "nul past the end" } */
241 T ( 1, "%*s", 1, s0); /* { dg-warning "nul past the end" } */
242 T (-1, "%*s", 1, s0); /* No warning for unknown destination size. */
243
244 T (1, "%.s", "");
245 T (1, "%.s", "123");
246 T (1, "%.0s", "123");
247 T (1, "%.0s", s3);
248 T (1, "%.*s", 0, "123");
249 T (1, "%.*s", 0, s3);
250 T (1, "%.1s", "123"); /* { dg-warning "nul past the end" } */
251 T (1, "%.1s", s3); /* { dg-warning "nul past the end" } */
252 T (1, "%.*s", 1, "123"); /* { dg-warning "nul past the end" } */
253 T (1, "%.*s", 1, s3); /* { dg-warning "nul past the end" } */
254
255 T (2, "%.*s", 0, "");
256 T (2, "%.*s", 0, "1");
257 T (2, "%.*s", 0, s1);
258 T (2, "%.*s", 0, "1\0");
259 T (2, "%.*s", 0, "12");
260 T (2, "%.*s", 0, s2);
261
262 T (2, "%.*s", 1, "");
263 T (2, "%.*s", 1, "1");
264 T (2, "%.*s", 1, s1);
265 T (2, "%.*s", 1, "1\0");
266 T (2, "%.*s", 1, "12");
267 T (2, "%.*s", 1, s2);
268
269 T (2, "%.*s", 2, "");
270 T (2, "%.*s", 2, "1");
271 T (2, "%.*s", 2, s1);
272 T (2, "%.*s", 2, "1\0");
273 T (2, "%.*s", 2, "12"); /* { dg-warning "nul past the end" } */
274 T (2, "%.*s", 2, s2); /* { dg-warning "nul past the end" } */
275
276 T (2, "%.*s", 3, "");
277 T (2, "%.*s", 3, "1");
278 T (2, "%.*s", 3, s1);
279 T (2, "%.*s", 3, "1\0");
280 T (2, "%.*s", 3, "12"); /* { dg-warning "nul past the end" } */
281 T (2, "%.*s", 3, "123"); /* { dg-warning "into a region" } */
282 T (2, "%.*s", 3, s2); /* { dg-warning "nul past the end" } */
283 T (2, "%.*s", 3, s3); /* { dg-warning "into a region" } */
284
285 T (2, "%*s", 0, "");
286 T (2, "%*s", 0, "1");
287 T (2, "%*s", 0, s1);
288 T (2, "%*s", 0, "1\0");
289 T (2, "%*s", 0, "12"); /* { dg-warning "nul past the end" } */
290 T (2, "%*s", 0, s2); /* { dg-warning "nul past the end" } */
291
292 /* Verify that output in excess of INT_MAX bytes is diagnosed even
293 when the size of the destination object is unknown. */
294 T (-1, "%*s", INT_MAX - 1, "");
295 T (-1, "%*s", INT_MAX, ""); /* { dg-warning "writing a terminating nul past the end" "ilp32" { target ilp32 } } */
296 T (-1, "X%*s", INT_MAX, ""); /* { dg-warning "directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." "lp64" { target lp64 } } */
297 /* { dg-warning "directive writing 2147483647 bytes into a region of size 2147483646" "ilp32" { target ilp32 } .-1 } */
298
299 /* Multiple directives. */
300
301 T (1, "%s%s", "", "");
302 T (1, "%s%s", s0, s0);
303 T (1, "%s%s", "", "1"); /* { dg-warning "nul past the end" } */
304 T (1, "%s%s", s0, s1); /* { dg-warning "nul past the end" } */
305 T (1, "%s%s", "1", ""); /* { dg-warning "nul past the end" } */
306 T (1, "%s%s", s1, s0); /* { dg-warning "nul past the end" } */
307 T (1, "%s%s", "1", "2"); /* { dg-warning "into a region" } */
308 T (1, "%s%s", s1, s1); /* { dg-warning "into a region" } */
309
310 T (2, "%s%s", "", "");
311 T (2, "%s%s", "", "1");
312 T (2, "%s%s", "1", "");
313 T (2, "%s%s", "", "12"); /* { dg-warning "nul past the end" } */
314 T (2, "%s%s", "1", "2"); /* { dg-warning "nul past the end" } */
315 T (2, "%s%s", "12", "2"); /* { dg-warning "into a region" } */
316 T (2, "%s%s", "1", "23"); /* { dg-warning "into a region" } */
317 T (2, "%s%s", "12", "3"); /* { dg-warning "into a region" } */
318 T (2, "%s%s", "12", "34"); /* { dg-warning "into a region" } */
319
320 T (2, "_%s", "");
321 T (2, "%%%s", "");
322 T (2, "%%%%%s", ""); /* { dg-warning "nul past the end" } */
323 T (2, "%s%%", "");
324 T (2, "_%s", "1"); /* { dg-warning "nul past the end" } */
325 T (2, "%%%s", "1"); /* { dg-warning "nul past the end" } */
326 T (2, "%s%%", "1"); /* { dg-warning "nul past the end" } */
327 T (2, "_%s", "12"); /* { dg-warning "into a region" } */
328 T (2, "__%s", "1"); /* { dg-warning "into a region" } */
329
330 T (2, "%1$s%2$s", "12", "3"); /* { dg-warning ".%2.s. directive writing 1 byte into a region of size 0" } */
331 T (2, "%1$s%1$s", "12"); /* { dg-warning "does not support|.%1.s. directive writing 2 bytes into a region of size 0" } */
332 T (2, "%2$s%1$s", "1", "23"); /* { dg-warning ".%1.s. directive writing 1 byte into a region of size 0" } */
333 T (2, "%2$s%2$s", "1", "23"); /* { dg-warning "unused|%2.s. directive writing 2 bytes into a region of size 0" } */
334
335 T (3, "__%s", "");
336 T (3, "__%s", "1"); /* { dg-warning "nul past the end" } */
337 T (3, "%s_%s", "", "");
338 T (3, "%s_%s", "1", "");
339 T (3, "%s_%s", "", "1");
340 T (3, "%s_%s", "1", "2"); /* { dg-warning "nul past the end" } */
341
342 /* Wide strings. */
343 T (-1, "%ls", L"");
344 T ( 0, "%ls", L""); /* { dg-warning "nul past the end" } */
345 T ( 1, "%ls", L"");
346 T ( 1, "%ls", L"\0");
347 T ( 1, "%1ls", L""); /* { dg-warning "nul past the end" } */
348
349 T (0, "%*ls", 0, L""); /* { dg-warning "nul past the end" } */
350 T (1, "%*ls", 0, L"");
351 T (1, "%*ls", 0, L"\0");
352 T (1, "%*ls", 1, L""); /* { dg-warning "nul past the end" } */
353
354 /* A wide character string need not convert into any bytes (although
355 individual ASCII characters are assumed to convert into 1 bt %lc
356 so this could be made smarter. */
357 T (1, "%ls", L"1"); /* { dg-warning "directive writing up to 6 bytes into a region of size 1" } */
358 T (1, "%.0ls", L"1");
359 T (2, "%.0ls", L"1");
360 T (2, "%.1ls", L"1");
361 T (2, "%.*ls", 1, L"1");
362
363 /* The "%.2ls" directive below will write at a minimum 1 byte (because
364 L"1" is known and can be assumed to convert to at least one multibyte
365 character), and at most 2 bytes because of the precision. Since its
366 output is explicitly bounded it is diagnosed. */
367 T (2, "%.2ls", L"1"); /* { dg-warning "nul past the end" } */
368 T (2, "%.*ls", 2, L"1"); /* { dg-warning "nul past the end" } */
369
370 T (3, "%.0ls", L"1");
371 T (3, "%.1ls", L"1");
372 T (3, "%.2ls", L"1");
373 T (3, "%ls", L"12"); /* { dg-warning "directive writing up to 12 bytes" } */
374
375 T (3, "%ls", L"123"); /* { dg-warning "directive writing up to 18 bytes" } */
376 T (3, "%.0ls", L"123");
377 T (3, "%.1ls", L"123");
378 T (3, "%.2ls", L"123");
379 T (3, "%.3ls", L"123"); /* { dg-warning "nul past the end" } */
380 }
381
382 /* Exercise the "%hhd", "%hhi", "%hho", "%hhu", and "%hhx" directives
383 with constant arguments. */
384
385 void test_sprintf_chk_hh_const (void)
386 {
387 T (-1, "%hhd", 0);
388
389 T (1, "%hhd", 0); /* { dg-warning "nul past the end" } */
390 T (1, "%hhd", cst0); /* { dg-warning "nul past the end" } */
391 T (1, "%hhd", 1); /* { dg-warning "nul past the end" } */
392 T (1, "%hhd", cst1); /* { dg-warning "nul past the end" } */
393 T (1, "%hhd", -1); /* { dg-warning "into a region" } */
394 T (1, "%+hhd", 0); /* { dg-warning "into a region" } */
395 T (1, "%+hhd", 1); /* { dg-warning "into a region" } */
396 T (1, "%-hhd", 0); /* { dg-warning "nul past the end" } */
397
398 T (1, "%hhi", 0); /* { dg-warning "nul past the end" } */
399 T (1, "%hhi", 1); /* { dg-warning "nul past the end" } */
400 T (1, "%hhi", -1); /* { dg-warning "into a region" } */
401 T (1, "%+hhi", 0); /* { dg-warning "into a region" } */
402 T (1, "%+hhi", 1); /* { dg-warning "into a region" } */
403 T (1, "%-hhi", 0); /* { dg-warning "nul past the end" } */
404
405 T (2, "%hhi", 0);
406 T (2, "%hhi", 1);
407 T (2, "%hhi", 9);
408 T (2, "% hhi", 9); /* { dg-warning "nul past the end" } */
409 T (2, "%+hhi", 9); /* { dg-warning "nul past the end" } */
410 T (2, "%-hhi", 9);
411 T (2, "%hhi", 10); /* { dg-warning "nul past the end" } */
412 T (2, "%hhi", cst10); /* { dg-warning "nul past the end" } */
413 T (2, "%hhi", -1); /* { dg-warning "nul past the end" } */
414 T (2, "% hhi", -1); /* { dg-warning "nul past the end" } */
415 T (2, "%+hhi", -1); /* { dg-warning "nul past the end" } */
416 T (2, "%-hhi", -1); /* { dg-warning "nul past the end" } */
417
418 T (2, "%hho", 0);
419 T (2, "%hho", 1);
420 T (2, "%hho", 7);
421 T (2, "%hho", 010); /* { dg-warning "nul past the end" } */
422 T (2, "%hho", 077); /* { dg-warning "nul past the end" } */
423 T (2, "%hho", -1); /* { dg-warning "into a region" } */
424
425 T (2, "%hhx", 0);
426 T (2, "%hhX", 1);
427 T (2, "%hhx", 7);
428 T (2, "%hhX", 8);
429 T (2, "%hhx", -1); /* { dg-warning "nul past the end" } */
430 T (2, "%hhX", 0xf);
431 T (2, "%hhx", 0x10); /* { dg-warning "nul past the end" } */
432 T (2, "%hhX", 0xff); /* { dg-warning "nul past the end" } */
433
434 T (1, "%#hhx", 0); /* { dg-warning "nul past the end" } */
435 T (2, "%#hhx", 0);
436 T (3, "%#hhx", 1); /* { dg-warning "nul past the end" } */
437
438 T (4, "%hhd", 255);
439 T (4, "%hhd", 256);
440 T (4, "%hhd", 0xfff);
441 T (4, "%hhd", 0xffff);
442
443 T (4, "%hhi", 255);
444 T (4, "%hhi", 256);
445 T (4, "%hhi", 0xfff);
446 T (4, "%hhi", 0xffff);
447
448 T (4, "%hhu", -1);
449 T (4, "%hhu", 255);
450 T (4, "%hhu", 256);
451 T (4, "%hhu", 0xfff);
452 T (4, "%hhu", 0xffff);
453
454 T (4, "%#hhx", 0);
455 T (4, "%#hhx", 1);
456 T (4, "%#hhx", -1); /* { dg-warning "nul past the end" } */
457 T (4, "%#hhx", 0xf);
458 T (4, "%#hhx", 0x10); /* { dg-warning "nul past the end" } */
459 T (4, "%#hhx", 0xff); /* { dg-warning "nul past the end" } */
460 T (4, "%#hhx", 0xfff); /* { dg-warning "nul past the end" } */
461
462 T (4, "%hhi %hhi", 0, 0);
463 T (4, "%hhi %hhi", 9, 9);
464 T (4, "%hhi %hhi", 1, 10); /* { dg-warning "nul past the end" } */
465 T (4, "%hhi %hhi", 10, 1); /* { dg-warning "nul past the end" } */
466 T (4, "%hhi %hhi", 11, 12); /* { dg-warning "into a region" } */
467
468 /* As a special case, a precision of zero with an argument of zero
469 results in zero bytes (unless modified by flags and/or width). */
470 T (1, "%.0hhd", 0);
471 T (1, "%+.0hhd", 0); /* { dg-warning "nul past the end" } */
472 T (1, "%-.0hhd", 0);
473 T (1, "% .0hhd", 0); /* { dg-warning "nul past the end" } */
474 T (1, "%0.0hhd", 0); /* { dg-warning ".0. flag ignored with precision" } */
475 T (1, "%00.0hhd", 0); /* { dg-warning "repeated .0. flag in format" } */
476 /* { dg-warning ".0. flag ignored with precision" "" { target *-*-* } .-1 } */
477 T (1, "%-0.0hhd", 0); /* { dg-warning ".0. flag ignored with .-. flag" } */
478 /* { dg-warning ".0. flag ignored with precision" "" { target *-*-* } .-1 } */
479 T (1, "%.0hhi", 0);
480 T (1, "%.0hho", 0);
481 /* As a special case, '#' in base 8 results in 1 byte (the zero). */
482 T (1, "%#.0hho", 0); /* { dg-warning "nul past the end" } */
483 T (1, "%.0hhx", 0);
484 T (1, "%.0hhX", 0);
485 T (1, "%#.0hhX", 0);
486
487 T (5, "%0*hhd %0*hhi", 0, 7, 0, 9);
488 T (5, "%0*hhd %0*hhi", 1, 7, 1, 9);
489 T (5, "%0*hhd %0*hhi", 1, 7, 2, 9);
490 T (5, "%0*hhd %0*hhi", 2, 7, 1, 9);
491 T (5, "%0*hhd %0*hhi", 2, 7, 2, 9); /* { dg-warning "nul past the end" } */
492 T (5, "%0*hhd %0*hhi", 0, 12, 0, 123); /* { dg-warning ".%0\\*hhi. directive writing 3 bytes into a region of size 2" } */
493 T (5, "%0*hhd %0*hhi", 1, 12, 1, 123); /* { dg-warning ".%0\\*hhi. directive writing 3 bytes into a region of size 2" } */
494 T (5, "%0*hhd %0*hhi", 2, 12, 3, 123); /* { dg-warning ".%0\\*hhi. directive writing 3 bytes into a region of size 2" } */
495
496 /* FIXME: Move the boundary test cases into a file of their own that's
497 exercised only on targets with the matching type limits (otherwise
498 they'll fail). */
499 #undef MAX
500 #define MAX 127
501
502 #undef MIN
503 #define MIN (-MAX -1)
504
505 T (1, "%hhi", MAX); /* { dg-warning "into a region" } */
506 T (1, "%hhi", MIN); /* { dg-warning "into a region" } */
507 T (1, "%hhi", MAX + 1); /* { dg-warning "into a region" } */
508
509 T (2, "%hhi", MAX + 1); /* { dg-warning "into a region" } */
510 T (2, "%hhi", MAX + 10); /* { dg-warning "into a region" } */
511 T (2, "%hhi", MAX + 100); /* { dg-warning "into a region" } */
512 }
513
514 /* Exercise the "%hhd", "%hi", "%ho", "%hu", and "%hx" directives
515 with constant arguments. */
516
517 void test_sprintf_chk_h_const (void)
518 {
519 T (1, "%hu", 0); /* { dg-warning "nul past the end" } */
520 T (1, "%hu", 1); /* { dg-warning "nul past the end" } */
521 T (1, "%hu", -1); /* { dg-warning "into a region" } */
522
523 T (2, "%hi", 0);
524 T (2, "%hi", 1);
525 T (2, "%hi", 9);
526 T (2, "% hi", 9); /* { dg-warning "nul past the end" } */
527 T (2, "%+hi", 9); /* { dg-warning "nul past the end" } */
528 T (2, "%-hi", 9);
529 T (2, "%hi", 10); /* { dg-warning "nul past the end" } */
530 T (2, "%hi", -1); /* { dg-warning "nul past the end" } */
531 T (2, "% hi", -2); /* { dg-warning "nul past the end" } */
532 T (2, "%+hi", -3); /* { dg-warning "nul past the end" } */
533 T (2, "%-hi", -4); /* { dg-warning "nul past the end" } */
534
535 T (2, "%hu", 0);
536 T (2, "%hu", 1);
537 T (2, "%hu", 9);
538 T (2, "%hu", 10); /* { dg-warning "nul past the end" } */
539 T (2, "%hu", -1); /* { dg-warning "into a region" } */
540
541 T (2, "%ho", 0);
542 T (2, "%ho", 1);
543 T (2, "%ho", 7);
544 T (2, "%ho", 010); /* { dg-warning "nul past the end" } */
545 T (2, "%ho", 077); /* { dg-warning "nul past the end" } */
546 T (2, "%ho", 0100); /* { dg-warning "into a region" } */
547 T (2, "%ho", -1); /* { dg-warning "into a region" } */
548
549 T (2, "%hx", 0);
550 T (2, "%hx", 1);
551 T (2, "%hx", 7);
552 T (2, "%hx", 0xf);
553 T (2, "%hx", 0x10); /* { dg-warning "nul past the end" } */
554 T (2, "%hx", 0xff); /* { dg-warning "nul past the end" } */
555 T (2, "%hx", 0x100); /* { dg-warning "into a region" } */
556 T (2, "%hx", -1); /* { dg-warning "into a region" } */
557
558 T (3, "% hi", 7);
559 T (3, "%+hi", 8);
560 T (3, "%-hi", 9);
561 T (3, "%hi", 10);
562 T (3, "%hi", -1);
563 T (3, "% hi", -2);
564 T (3, "%+hi", -3);
565 T (3, "%-hi", -4);
566
567 T (5, "%hu", 9999);
568 T (5, "%hu", 10000); /* { dg-warning "nul past the end" } */
569 T (5, "%hu", 65535); /* { dg-warning "nul past the end" } */
570
571 T (1, "%#hx", 0); /* { dg-warning "nul past the end" } */
572 T (2, "%#hx", 0);
573 T (3, "%#hx", 1); /* { dg-warning "nul past the end" } */
574
575 T (4, "%#hx", 0);
576 T (4, "%#hx", 1);
577 T (4, "%#hx", 0xf);
578 T (4, "%#hx", 0x10); /* { dg-warning "nul past the end" } */
579 T (4, "%#hx", 0xff); /* { dg-warning "nul past the end" } */
580 T (4, "%#hx", 0x100); /* { dg-warning "into a region" } */
581 T (4, "%#hx", -1); /* { dg-warning "into a region" } */
582
583 /* As a special case, a precision of zero with an argument of zero
584 results in zero bytes (unless modified by width). */
585 T (1, "%.0hd", 0);
586 T (1, "%+.0hd", 0); /* { dg-warning "nul past the end" } */
587 T (1, "%-.0hd", 0);
588 T (1, "% .0hd", 0); /* { dg-warning "nul past the end" } */
589 T (1, "%0.0hd", 0); /* { dg-warning ".0. flag ignored with precision" } */
590 T (1, "%00.0hd", 0); /* { dg-warning "repeated .0. flag in format" } */
591 /* { dg-warning ".0. flag ignored with precision" "" { target *-*-* } .-1 } */
592 T (1, "%-0.0hd", 0); /* { dg-warning ".0. flag ignored with .-. flag" } */
593 /* { dg-warning ".0. flag ignored with precision" "" { target *-*-* } .-1 } */
594 T (1, "%.0hi", 0);
595 T (1, "%.0ho", 0);
596 T (1, "%#.0ho", 0); /* { dg-warning "nul past the end" } */
597 T (1, "%.0hx", 0);
598 T (1, "%.0hX", 0);
599 T (1, "%#.0hX", 0);
600
601 #undef MAX
602 #define MAX 65535
603
604 T (1, "%hu", 0); /* { dg-warning "nul past the end" } */
605 T (1, "%hu", 1); /* { dg-warning "nul past the end" } */
606 T (1, "%hu", -1); /* { dg-warning "into a region" } */
607 T (1, "%hu", MAX); /* { dg-warning "into a region" } */
608 T (1, "%hu", MAX + 1); /* { dg-warning "nul past the end" } */
609 }
610
611 /* Exercise the "%d", "%i", "%o", "%u", and "%x" directives with
612 constant arguments. */
613
614 void test_sprintf_chk_integer_const (void)
615 {
616 T ( 1, "%i", 0); /* { dg-warning "nul past the end" } */
617 T ( 1, "%i", 1); /* { dg-warning "nul past the end" } */
618 T ( 1, "%i", -1); /* { dg-warning "into a region" } */
619 T ( 1, "%i_", 1); /* { dg-warning " 1 byte into a region of size 0" } */
620 T ( 1, "_%i", 1); /* { dg-warning "into a region" } */
621 T ( 1, "_%i_", 1); /* { dg-warning "into a region" } */
622 T ( 1, "%o", 0); /* { dg-warning "nul past the end" } */
623 T ( 1, "%u", 0); /* { dg-warning "nul past the end" } */
624 T ( 1, "%x", 0); /* { dg-warning "nul past the end" } */
625 T ( 1, "%#x", 0); /* { dg-warning "nul past the end" } */
626 T ( 1, "%x", 1); /* { dg-warning "nul past the end" } */
627 T ( 1, "%#x", 1); /* { dg-warning "into a region" } */
628
629 T ( 2, "%i", 0);
630 T ( 2, "%i", 1);
631 T ( 2, "%i", 9);
632 T ( 2, "%i", -1); /* { dg-warning "nul past the end" } */
633 T ( 2, "%i", 10); /* { dg-warning "nul past the end" } */
634 T ( 2, "%i_", 0); /* { dg-warning "nul past the end" } */
635 T ( 2, "_%i", 0); /* { dg-warning "nul past the end" } */
636 T ( 2, "_%i_", 0); /* { dg-warning " 1 byte into a region of size 0" } */
637 T ( 2, "%o", 1);
638 T ( 2, "%o", 7);
639 T ( 2, "%o", 010); /* { dg-warning "nul past the end" } */
640 T ( 2, "%o", 0100); /* { dg-warning "into a region" } */
641 T ( 2, "%x", 1);
642 T ( 2, "%#x", 1); /* { dg-warning "into a region" } */
643 T ( 2, "%x", 0xa);
644 T ( 2, "%x", 0xf);
645 T ( 2, "%x", 0x10); /* { dg-warning "nul past the end" } */
646 T ( 2, "%x", 0xff); /* { dg-warning "nul past the end" } */
647 T ( 2, "%x", 0x1ff); /* { dg-warning "into a region" } */
648
649 T ( 3, "%i", 0);
650 T ( 3, "%i", 1);
651 T ( 3, "%i", 9);
652 T ( 3, "%i", -9);
653 T ( 3, "%i", 10);
654 T ( 3, "%i", 99);
655 T ( 3, "%i", -99); /* { dg-warning "nul past the end" } */
656
657 /* ~0U is formatted into exactly three bytes as "-1" followed by
658 the terminating NUL character. */
659 T ( 3, "%+i", ~0U);
660 T ( 3, "%-i", ~0U);
661 T ( 3, "% i", ~0U);
662
663 T ( 8, "%8u", 1); /* { dg-warning "nul past the end" } */
664 T ( 9, "%8u", 1);
665
666 /* As a special case, a precision of zero with an argument of zero
667 results in zero bytes (unless modified by width). */
668 T (1, "%.0d", 0);
669 T (1, "%+.0d", 0); /* { dg-warning "nul past the end" } */
670 T (1, "%-.0d", 0);
671 T (1, "% .0d", 0); /* { dg-warning "nul past the end" } */
672 T (1, "%0.0d", 0); /* { dg-warning ".0. flag ignored with precision" } */
673 T (1, "%00.0d", 0); /* { dg-warning "repeated .0. flag in format" } */
674 /* { dg-warning ".0. flag ignored with precision" "" { target *-*-* } .-1 } */
675 T (1, "%-0.0d", 0); /* { dg-warning ".0. flag ignored with .-. flag" } */
676 /* { dg-warning ".0. flag ignored with precision" "" { target *-*-* } .-1 } */
677 T (1, "%.0i", 0);
678 T (1, "%.0o", 0);
679 T (1, "%#.0o", 0); /* { dg-warning "nul past the end" } */
680 T (1, "%.0x", 0);
681 T (1, "%.0X", 0);
682 T (1, "%#.0X", 0);
683
684 T ( 7, "%1$i%2$i%3$i", 1, 23, 456);
685 T ( 8, "%1$i%2$i%3$i%1$i", 1, 23, 456);
686 T ( 8, "%1$i%2$i%3$i%2$i", 1, 23, 456); /* { dg-warning "nul past the end" } */
687 T ( 8, "%1$i%2$i%3$i%3$i", 1, 23, 456); /* { dg-warning "into a region" } */
688
689 #undef MAX
690 #define MAX 2147483647 /* 10 digits. */
691 #undef MIN
692 #define MIN (-MAX -1) /* Sign plus 10 digits. */
693
694 T ( 1, "%i", MAX); /* { dg-warning "into a region" } */
695 T ( 1, "%i", MIN); /* { dg-warning "into a region" } */
696 T ( 2, "%i", MAX); /* { dg-warning "into a region" } */
697 T ( 2, "%i", MIN); /* { dg-warning "into a region" } */
698 T (10, "%i", 123456789);
699 T (10, "%i", -123456789); /* { dg-warning "nul past the end" } */
700 T (10, "%i", MAX); /* { dg-warning "nul past the end" } */
701 T (10, "%i", MIN); /* { dg-warning "into a region" } */
702
703 T (11, "%i", MAX);
704 T (11, "%i", MIN); /* { dg-warning "nul past the end" } */
705 }
706
707 /* Exercise the "%jd", "%ji", "%jo", "%ju", and "%jx" directives
708 for the formatting of intmax_t and uintmax_t values with constant
709 arguments. */
710
711 void test_sprintf_chk_j_const (void)
712 {
713 #define I(x) ((__INTMAX_TYPE__)x)
714
715 T ( 1, "%ji", I ( 0)); /* { dg-warning "nul past the end" } */
716 T ( 1, "%ji", I ( 1)); /* { dg-warning "nul past the end" } */
717 T ( 1, "%ji", I ( -1)); /* { dg-warning "into a region" } */
718 T ( 1, "%ji_", I ( 1)); /* { dg-warning " 1 byte into a region of size 0" } */
719 T ( 1, "_%ji", I ( 1)); /* { dg-warning "into a region" } */
720 T ( 1, "_%ji_",I ( 1)); /* { dg-warning "into a region" } */
721 T ( 1, "%jo", I ( 0)); /* { dg-warning "nul past the end" } */
722 T ( 1, "%ju", I ( 0)); /* { dg-warning "nul past the end" } */
723 T ( 1, "%jx", I ( 0)); /* { dg-warning "nul past the end" } */
724 T ( 1, "%#jx", I ( 0)); /* { dg-warning "nul past the end" } */
725 T ( 1, "%jx", I ( 1)); /* { dg-warning "nul past the end" } */
726 T ( 1, "%#jx", I ( 1)); /* { dg-warning "into a region" } */
727
728 T ( 2, "%ji", I ( 0));
729 T ( 2, "%ji", I ( 1));
730 T ( 2, "%ji", I ( 9));
731 T ( 2, "%ji", I ( -1)); /* { dg-warning "nul past the end" } */
732 T ( 2, "%ji", I ( 10)); /* { dg-warning "nul past the end" } */
733 T ( 2, "%ji_", I ( 0)); /* { dg-warning "nul past the end" } */
734 T ( 2, "_%ji", I ( 0)); /* { dg-warning "nul past the end" } */
735 T ( 2, "_%ji_",I ( 0)); /* { dg-warning " 1 byte into a region of size 0" } */
736 T ( 2, "%jo", I ( 1));
737 T ( 2, "%jo", I ( 7));
738 T ( 2, "%jo", I ( 010)); /* { dg-warning "nul past the end" } */
739 T ( 2, "%jo", I ( 0100)); /* { dg-warning "into a region" } */
740 T ( 2, "%jx", I ( 1));
741 T ( 2, "%#jx", I ( 1)); /* { dg-warning "into a region" } */
742 T ( 2, "%jx", I ( 0xa));
743 T ( 2, "%jx", I ( 0xf));
744 T ( 2, "%jx", I ( 0x10)); /* { dg-warning "nul past the end" } */
745 T ( 2, "%jx", I ( 0xff)); /* { dg-warning "nul past the end" } */
746 T ( 2, "%jx", I (0x1ff)); /* { dg-warning "into a region" } */
747
748 T ( 3, "%ji", I ( 0));
749 T ( 3, "%ji", I ( 1));
750 T ( 3, "%ji", I ( 9));
751 T ( 3, "%ji", I ( -9));
752 T ( 3, "%ji", I ( 10));
753 T ( 3, "%ji", I ( 99));
754 T ( 3, "%ji", I ( -99)); /* { dg-warning "nul past the end" } */
755
756 /* ~0 is formatted into exactly three bytes as "-1" followed by
757 the terminating NUL character. */
758 T ( 3, "%+ji", ~I (0));
759 T ( 3, "%-ji", ~I (0));
760 T ( 3, "% ji", ~I (0));
761
762 T ( 8, "%8ju", I (1)); /* { dg-warning "nul past the end" } */
763 T ( 9, "%8ju", I (1));
764
765 /* As a special case, a precision of zero with an argument of zero
766 results in zero bytes (unless modified by width). */
767 T (1, "%.0jd", I (0));
768 T (1, "%+.0jd", I (0)); /* { dg-warning "nul past the end" } */
769 T (1, "%+.0ju", I (0)); /* { dg-warning ".\\+. flag used" } */
770 T (1, "%-.0jd", I (0));
771 T (1, "% .0jd", I (0)); /* { dg-warning "nul past the end" } */
772 T (1, "% .0ju", I (0)); /* { dg-warning ". . flag used" } */
773 T (1, "%0.0jd", I (0)); /* { dg-warning ".0. flag ignored with precision" } */
774 T (1, "%00.0jd", I (0)); /* { dg-warning "repeated .0. flag in format" } */
775 /* { dg-warning ".0. flag ignored with precision" "" { target *-*-* } .-1 } */
776 T (1, "%-0.0jd", I (0)); /* { dg-warning ".0. flag ignored with .-. flag" } */
777 /* { dg-warning ".0. flag ignored with precision" "" { target *-*-* } .-1 } */
778 T (1, "%.0ji", I (0));
779 T (1, "%.0jo", I (0));
780 T (1, "%#.0jo", I (0)); /* { dg-warning "nul past the end" } */
781 T (1, "%.0jx", I (0));
782 T (1, "%.0jX", I (0));
783 T (1, "%#.0jX", I (0));
784 }
785
786 /* Exercise the "%ld", "%li", "%lo", "%lu", and "%lx" directives
787 with constant arguments. */
788
789 void test_sprintf_chk_l_const (void)
790 {
791 T ( 1, "%li", 0L); /* { dg-warning "nul past the end" } */
792 T ( 1, "%li", 1L); /* { dg-warning "nul past the end" } */
793 T ( 1, "%li", -1L); /* { dg-warning "into a region" } */
794 T ( 1, "%li_", 1L); /* { dg-warning " 1 byte into a region of size 0" } */
795 T ( 1, "_%li", 1L); /* { dg-warning "into a region" } */
796 T ( 1, "_%li_", 1L); /* { dg-warning "into a region" } */
797 T ( 1, "%lo", 0L); /* { dg-warning "nul past the end" } */
798 T ( 1, "%lu", 0L); /* { dg-warning "nul past the end" } */
799 T ( 1, "%lx", 0L); /* { dg-warning "nul past the end" } */
800 T ( 1, "%#lx", 0L); /* { dg-warning "nul past the end" } */
801 T ( 1, "%lx", 1L); /* { dg-warning "nul past the end" } */
802 T ( 1, "%#lx", 1L); /* { dg-warning "into a region" } */
803
804 T ( 2, "%li", 0L);
805 T ( 2, "%li", 1L);
806 T ( 2, "%li", 9L);
807 T ( 2, "%li", -1L); /* { dg-warning "nul past the end" } */
808 T ( 2, "%li", 10L); /* { dg-warning "nul past the end" } */
809 T ( 2, "%li_", 0L); /* { dg-warning "nul past the end" } */
810 T ( 2, "_%li", 0L); /* { dg-warning "nul past the end" } */
811 T ( 2, "_%li_", 0L); /* { dg-warning " 1 byte into a region of size 0" } */
812 T ( 2, "%lo", 1L);
813 T ( 2, "%lo", 7L);
814 T ( 2, "%lo", 010L); /* { dg-warning "nul past the end" } */
815 T ( 2, "%lo", 0100L); /* { dg-warning "into a region" } */
816 T ( 2, "%lx", 1L);
817 T ( 2, "%#lx", 1L); /* { dg-warning "into a region" } */
818 T ( 2, "%lx", 0xaL);
819 T ( 2, "%lx", 0xfL);
820 T ( 2, "%lx", 0x10L); /* { dg-warning "nul past the end" } */
821 T ( 2, "%lx", 0xffL); /* { dg-warning "nul past the end" } */
822 T ( 2, "%lx", 0x1ffL); /* { dg-warning "into a region" } */
823
824 T ( 3, "%li", 0L);
825 T ( 3, "%li", 1L);
826 T ( 3, "%li", 9L);
827 T ( 3, "%li", -9L);
828 T ( 3, "%li", 10L);
829 T ( 3, "%li", 99L);
830 T ( 3, "%li", -99L); /* { dg-warning "nul past the end" } */
831
832 /* ~0U is formatted into exactly three bytes as "-1" followed by
833 the terminating NUL character. */
834 T ( 3, "%+li", ~0LU);
835 T ( 3, "%-li", ~0LU);
836 T ( 3, "% li", ~0LU);
837
838 T ( 8, "%8lu", 1L); /* { dg-warning "nul past the end" } */
839 T ( 9, "%8lu", 1L);
840
841 /* As a special case, a precision of zero with an argument of zero
842 results in zero bytes (unless modified by width). */
843 T (1, "%.0ld", 0L);
844 T (1, "%+.0ld", 0L); /* { dg-warning "nul past the end" } */
845 T (1, "%+.0lu", 0L); /* { dg-warning ".\\+. flag used" } */
846 T (1, "%-.0ld", 0L);
847 T (1, "% .0ld", 0L); /* { dg-warning "nul past the end" } */
848 T (1, "% .0lu", 0L); /* { dg-warning ". . flag used" } */
849 T (1, "%0.0ld", 0L); /* { dg-warning ".0. flag ignored with precision" } */
850 T (1, "%00.0ld", 0L); /* { dg-warning "repeated .0. flag in format" } */
851 /* { dg-warning ".0. flag ignored with precision" "" { target *-*-* } .-1 } */
852 T (1, "%-0.0ld", 0L); /* { dg-warning ".0. flag ignored with .-. flag" } */
853 /* { dg-warning ".0. flag ignored with precision" "" { target *-*-* } .-1 } */
854 T (1, "%.0li", 0L);
855 T (1, "%.0lo", 0L);
856 T (1, "%#.0lo", 0L); /* { dg-warning "nul past the end" } */
857 T (1, "%.0lx", 0L);
858 T (1, "%.0lX", 0L);
859 T (1, "%#.0lX", 0L);
860 }
861
862 /* Exercise the "%lld", "%lli", "%llo", "%llu", and "%llx" directives
863 with constant arguments. */
864
865 void test_sprintf_chk_ll_const (void)
866 {
867 T ( 1, "%lli", 0LL); /* { dg-warning "nul past the end" } */
868 T ( 1, "%lli", 1LL); /* { dg-warning "nul past the end" } */
869 T ( 1, "%lli", -1LL); /* { dg-warning "into a region" } */
870 T ( 1, "%lli_", 1LL); /* { dg-warning " 1 byte into a region of size 0 " } */
871 T ( 1, "_%lli", 1LL); /* { dg-warning "into a region" } */
872 T ( 1, "_%lli_", 1LL); /* { dg-warning "into a region" } */
873 T ( 1, "%llo", 0LL); /* { dg-warning "nul past the end" } */
874 T ( 1, "%llu", 0LL); /* { dg-warning "nul past the end" } */
875 T ( 1, "%llx", 0LL); /* { dg-warning "nul past the end" } */
876 T ( 1, "%#llx", 0LL); /* { dg-warning "nul past the end" } */
877 T ( 1, "%llx", 1LL); /* { dg-warning "nul past the end" } */
878 T ( 1, "%#llx", 1LL); /* { dg-warning "into a region" } */
879
880 T ( 2, "%lli", 0LL);
881 T ( 2, "%lli", 1LL);
882 T ( 2, "%lli", 9LL);
883 T ( 2, "%lli", -1LL); /* { dg-warning "nul past the end" } */
884 T ( 2, "%lli", 10LL); /* { dg-warning "nul past the end" } */
885 T ( 2, "%lli_", 0LL); /* { dg-warning "nul past the end" } */
886 T ( 2, "_%lli", 0LL); /* { dg-warning "nul past the end" } */
887 T ( 2, "_%lli_", 0LL); /* { dg-warning " 1 byte into a region of size 0" } */
888 T ( 2, "%llo", 1LL);
889 T ( 2, "%llo", 7LL);
890 T ( 2, "%llo", 010LL); /* { dg-warning "nul past the end" } */
891 T ( 2, "%llo", 0100LL); /* { dg-warning "into a region" } */
892 T ( 2, "%llx", 1LL);
893 T ( 2, "%#llx", 1LL); /* { dg-warning "into a region" } */
894 T ( 2, "%llx", 0xaLL);
895 T ( 2, "%llx", 0xfLL);
896 T ( 2, "%llx", 0x10LL); /* { dg-warning "nul past the end" } */
897 T ( 2, "%llx", 0xffLL); /* { dg-warning "nul past the end" } */
898 T ( 2, "%llx", 0x1ffLL); /* { dg-warning "into a region" } */
899
900 T ( 3, "%lli", 0LL);
901 T ( 3, "%lli", 1LL);
902 T ( 3, "%lli", 9LL);
903 T ( 3, "%lli", -9LL);
904 T ( 3, "%lli", 10LL);
905 T ( 3, "%lli", 99LL);
906 T ( 3, "%lli", -99LL); /* { dg-warning "nul past the end" } */
907
908 /* ~0U is formatted into exactly three bytes as "-1" followed by
909 the terminating NUL character. */
910 T ( 3, "%+lli", ~0LLU);
911 T ( 3, "%-lli", ~0LLU);
912 T ( 3, "% lli", ~0LLU);
913
914 T ( 8, "%8llu", 1LL); /* { dg-warning "nul past the end" } */
915 T ( 9, "%8llu", 1LL);
916
917 /* assume 64-bit long long. */
918 #define LLONG_MAX 9223372036854775807LL /* 19 bytes */
919 #define LLONG_MIN (-LLONG_MAX - 1) /* 20 bytes */
920
921 T (18, "%lli", LLONG_MIN); /* { dg-warning "into a region" } */
922 T (19, "%lli", LLONG_MIN); /* { dg-warning "into a region" } */
923 T (20, "%lli", LLONG_MIN); /* { dg-warning "nul past the end" } */
924 T (21, "%lli", LLONG_MIN);
925
926 T (18, "%lli", LLONG_MAX); /* { dg-warning "into a region" } */
927 T (19, "%lli", LLONG_MAX); /* { dg-warning "nul past the end" } */
928 T (20, "%lli", LLONG_MAX);
929
930 T (21, "%llo", -1LL); /* { dg-warning "into a region" } */
931 T (22, "%llo", -1LL); /* { dg-warning "nul past the end" } */
932 T (23, "%llo", -1LL);
933
934 T (19, "%llu", -1LL); /* { dg-warning "into a region" } */
935 T (20, "%llu", -1LL); /* { dg-warning "nul past the end" } */
936 T (21, "%llu", -1LL);
937
938 T (15, "%llx", -1LL); /* { dg-warning "into a region" } */
939 T (16, "%llx", -1LL); /* { dg-warning "nul past the end" } */
940 T (17, "%llx", -1LL);
941 }
942
943 void test_sprintf_chk_L_const (void)
944 {
945 T (-1, "%Li", 0LL);
946 T ( 1, "%Li", 0LL); /* { dg-warning "nul past the end" } */
947 T ( 1, "%Li", 1LL); /* { dg-warning "nul past the end" } */
948 T ( 1, "%Li", -1LL); /* { dg-warning "into a region" } */
949 T ( 1, "%Li_", 1LL); /* { dg-warning " 1 byte into a region of size 0" } */
950 T ( 1, "_%Li", 1LL); /* { dg-warning "into a region" } */
951 T ( 1, "_%Li_", 1LL); /* { dg-warning "into a region" } */
952 }
953
954 void test_sprintf_chk_z_const (void)
955 {
956 T (-1, "%zi", (size_t)0);
957 T ( 1, "%zi", (size_t)0); /* { dg-warning "nul past the end" } */
958 T ( 1, "%zi", (size_t)1); /* { dg-warning "nul past the end" } */
959 T ( 1, "%zi", (size_t)-1L);/* { dg-warning "into a region" } */
960 T ( 1, "%zi_", (size_t)1); /* { dg-warning " 1 byte into a region of size 0" } */
961 T ( 1, "_%zi", (size_t)1); /* { dg-warning "into a region" } */
962 T ( 1, "_%zi_", (size_t)1); /* { dg-warning "into a region" } */
963
964 T ( 2, "%zu", (size_t)1);
965 T ( 2, "%zu", (size_t)9);
966 T ( 2, "%zu", (size_t)10); /* { dg-warning "nul past the end" } */
967 }
968
969 void test_sprintf_chk_a_const (void)
970 {
971 T (-1, "%a", 0.0);
972 T (-1, "%la", 0.0);
973 T (-1, "%.a", 0.0);
974 T (-1, "%.la", 0.0);
975 T (-1, "%123.a", 0.0);
976 T (-1, "%234.la", 0.0);
977 T (-1, "%.345a", 0.0);
978 T (-1, "%456.567la", 0.0);
979
980 /* The least number of bytes on output is 6 for "0x0p+0". When precision
981 is missing the number of digits after the decimal point isn't fully
982 specified by C (a defect). Two sets of implementations are known to
983 exist: those that trim trailing zeros (e.g., Glibc) and those that
984 pad output with trailing zeros so that all floating point numbers
985 result in the same number of bytes on output (e.g., Solaris). */
986 T ( 0, "%a", 0.0); /* { dg-warning "writing between 6 and 20 bytes" } */
987 T ( 0, "%la", 0.0); /* { dg-warning "between 6 and 20 bytes" } */
988 T ( 1, "%a", 0.0); /* { dg-warning "between 6 and 20 bytes" } */
989 T ( 2, "%a", 0.0); /* { dg-warning "between 6 and 20 bytes" } */
990 T ( 3, "%a", 0.0); /* { dg-warning "between 6 and 20 bytes" } */
991 T ( 4, "%a", 0.0); /* { dg-warning "between 6 and 20 bytes" } */
992 T ( 5, "%a", 0.0); /* { dg-warning "between 6 and 20 bytes" } */
993 T ( 6, "%a", 0.0); /* { dg-warning "between 6 and 20 bytes" } */
994 T (19, "%a", 0.0); /* { dg-warning "between 6 and 20 bytes" } */
995 T (20, "%a", 0.0); /* { dg-warning "may write a terminating nul" } */
996
997 T (0, "%.a", 0.0); /* { dg-warning "into a region" } */
998 T (0, "%.0a", 0.0); /* { dg-warning "into a region" } */
999 T (0, "%.0la", 0.0); /* { dg-warning "into a region" } */
1000 T (1, "%.0a", 0.0); /* { dg-warning "into a region" } */
1001 T (2, "%.0a", 0.0); /* { dg-warning "into a region" } */
1002 T (3, "%.0a", 0.0); /* { dg-warning "into a region" } */
1003 T (4, "%.0a", 0.0); /* { dg-warning "into a region" } */
1004 T (5, "%.0a", 0.0); /* { dg-warning "into a region" } */
1005 T (6, "%.0a", 0.0); /* { dg-warning "writing a terminating nul" } */
1006
1007 T (7, "%6.a", 0.0);
1008 T (7, "%7.a", 0.0); /* { dg-warning "writing a terminating nul" } */
1009 T (7, "%7.1a", 0.0); /* { dg-warning "writing 8 bytes into a region of size 7" } */
1010
1011 T (7, "%.a", 0.0);
1012 T (7, "%.0a", 0.0);
1013 }
1014
1015 void test_sprintf_chk_e_const (void)
1016 {
1017 T (-1, "%E", 0.0);
1018 T (-1, "%lE", 0.0);
1019 T (-1, "%.E", 0.0);
1020 T (-1, "%.lE", 0.0);
1021 T (-1, "%123.E", 0.0);
1022 T (-1, "%234.lE", 0.0);
1023 T (-1, "%.345E", 0.0);
1024 T (-1, "%.456lE", 0.0);
1025
1026 T ( 0, "%E", 0.0); /* { dg-warning "into a region" } */
1027 T ( 0, "%e", 0.0); /* { dg-warning "into a region" } */
1028 T ( 1, "%E", 1.0); /* { dg-warning "into a region" } */
1029 T ( 1, "%e", 1.0); /* { dg-warning "into a region" } */
1030 T ( 2, "%e", 2.0); /* { dg-warning "into a region" } */
1031 T ( 3, "%e", 3.0); /* { dg-warning "into a region" } */
1032 T (12, "%e", 1.2); /* { dg-warning "nul past the end" } */
1033 T (12, "%e", 12.0); /* { dg-warning "nul past the end" } */
1034 T (13, "%e", 1.3); /* 1.300000e+00 */
1035 T (13, "%E", 13.0); /* 1.300000e+01 */
1036 T (13, "%e", 13.0);
1037 T (13, "%E", 1.4e+99); /* 1.400000e+99 */
1038 T (13, "%e", 1.5e+100); /* { dg-warning "nul past the end" } */
1039 T (14, "%E", 1.6e+101); /* 1.600000E+101 */
1040 T (14, "%e", -1.7e+102); /* { dg-warning "nul past the end" } */
1041 T (15, "%E", -1.8e+103); /* -1.800000E+103 */
1042
1043 T (16, "%.8e", -1.9e+104); /* { dg-warning "nul past the end" } */
1044 T (17, "%.8e", -2.0e+105); /* -2.00000000e+105 */
1045
1046 T ( 5, "%.e", 0.0); /* { dg-warning "nul past the end" } */
1047 T ( 5, "%.0e", 0.0); /* { dg-warning "nul past the end" } */
1048 T ( 5, "%.0e", 1.0); /* { dg-warning "nul past the end" } */
1049 T ( 6, "%.e", 1.0);
1050 T ( 6, "%.0e", 1.0);
1051
1052 /* The output of the following directives depends on the rounding
1053 mode. */
1054 T (12, "%e", 9.999999e+99); /* { dg-warning "between 12 and 13" } */
1055 T (12, "%e", 9.9999994e+99); /* { dg-warning "between 12 and 13" } */
1056 T (12, "%e", 9.9999995e+99); /* { dg-warning "between 12 and 13" } */
1057 T (12, "%e", 9.9999996e+99); /* { dg-warning "between 12 and 13" } */
1058 T (12, "%e", 9.9999997e+99); /* { dg-warning "between 12 and 13" } */
1059 T (12, "%e", 9.9999998e+99); /* { dg-warning "between 12 and 13" } */
1060
1061 T (12, "%Le", 9.9999994e+99L);/* { dg-warning "between 12 and 13" } */
1062 T (12, "%Le", 9.9999995e+99L);/* { dg-warning "between 12 and 13" } */
1063 T (12, "%Le", 9.9999996e+99L);/* { dg-warning "between 12 and 13" } */
1064 T (12, "%Le", 9.9999997e+99L);/* { dg-warning "between 12 and 13" } */
1065 T (12, "%Le", 9.9999998e+99L);/* { dg-warning "between 12 and 13" } */
1066 T (12, "%Le", 9.9999999e+99L);/* { dg-warning "between 12 and 13" } */
1067 }
1068
1069 /* At -Wformat-overflow level 1 unknown numbers are assumed to have
1070 the value one, and unknown strings are assumed to have a zero
1071 length. */
1072
1073 void test_sprintf_chk_s_nonconst (int w, int p, const char *s)
1074 {
1075 T (-1, "%s", s);
1076 T ( 0, "%-s", s); /* { dg-warning "writing a terminating nul" } */
1077 T ( 1, "%s", s);
1078 T (-1, "%.0s", s);
1079 T ( 1, "%.0s", s);
1080 T (-1, "%.1s", s);
1081 T ( 1, "%.1s", s); /* { dg-warning "may write a terminating nul past the end" } */
1082 T (-1, "%.2s", s);
1083 T ( 1, "%.2s", s); /* { dg-warning "directive writing up to 2 bytes" } */
1084 T ( 2, "%.2s", s); /* { dg-warning "may write a terminating nul" } */
1085 T ( 3, "%.2s", s);
1086
1087 /* The string argument is constant but the width and/or precision
1088 is not. */
1089 T (-1, "%*s", w, "");
1090 T ( 1, "%*s", w, ""); /* { dg-warning "may write a terminating nul past the end" } */
1091 T (-1, "%*s", w, "1");
1092 T ( 1, "%*s", w, "1"); /* { dg-warning "writing a terminating nul past the end" } */
1093 T (-1, "%.*s", p, "");
1094 T ( 1, "%.*s", p, "");
1095 T (-1, "%.*s", p, "1");
1096 T ( 1, "%.*s", p, "1"); /* { dg-warning "may write a terminating nul" } */
1097 T ( 1, "%.*s", w, "123"); /* { dg-warning "writing up to 3 bytes into a region of size 1" } */
1098
1099 /* Either of the messages below is acceptable. */
1100 T ( 1, "%*s", w, "123"); /* { dg-warning "writing 3 or more bytes into a region of size 1|writing between 3 and 2147483648 bytes" } */
1101 T ( 2, "%*s", w, "123"); /* { dg-warning "writing 3 or more bytes into a region of size 2|writing between 3 and 2147483648 bytes" } */
1102 T ( 3, "%*s", w, "123"); /* { dg-warning "writing a terminating nul past the end" } */
1103 T ( 4, "%*s", w, "123");
1104
1105 /* The following will definitely write past the end of the buffer,
1106 but since at level 1 the length of an unknown string argument
1107 is assumed to be zero, it will write the terminating nul past
1108 the end (we don't print "past the end" when we're not
1109 sure which we can't be with an unknown string. */
1110 T (1, "%1s", s); /* { dg-warning "writing a terminating nul past the end" } */
1111
1112 /* Multiple directives. */
1113 T (1, "%s%s", s, s);
1114 T (1, "%s%s%s", s, s, s);
1115 }
1116
1117 /* Exercise the hh length modifier with all integer specifiers and
1118 a non-constant argument. */
1119
1120 void test_sprintf_chk_hh_nonconst (int w, int p, int a)
1121 {
1122 T (-1, "%hhd", a);
1123
1124 T (0, "%hhd", a); /* { dg-warning ".%hhd. directive writing between 1 and . bytes into a region of size 0" } */
1125 T (0, "%hhi", var0); /* { dg-warning "into a region" } */
1126 T (0, "%hhi", a); /* { dg-warning "into a region" } */
1127 T (0, "%hhu", a); /* { dg-warning "into a region" } */
1128 T (0, "%hhx", a); /* { dg-warning "into a region" } */
1129
1130 T (1, "%hhd", a); /* { dg-warning "nul past the end" } */
1131 T (1, "%hhd", var0); /* { dg-warning "nul past the end" } */
1132 T (1, "%hhi", a); /* { dg-warning "nul past the end" } */
1133 T (1, "%hhu", a); /* { dg-warning "nul past the end" } */
1134 T (1, "%hhx", a); /* { dg-warning "nul past the end" } */
1135
1136 T (1, "% hhd", a); /* { dg-warning "into a region" } */
1137 T (1, "% hhi", a); /* { dg-warning "into a region" } */
1138 T (1, "%+hhd", a); /* { dg-warning "into a region" } */
1139 T (1, "%+hhi", a); /* { dg-warning "into a region" } */
1140 T (1, "%-hhd", a); /* { dg-warning "nul past the end" } */
1141 T (1, "%-hhi", a); /* { dg-warning "nul past the end" } */
1142
1143 T (2, "%hhd", a);
1144 T (2, "%hhd", var0);
1145 T (2, "%hhd", var10);
1146 T (2, "%hhi", a);
1147 T (2, "%hho", a);
1148 T (2, "%hhu", a);
1149 T (2, "%hhx", a);
1150
1151 T (2, "% hhd", a); /* { dg-warning "nul past the end" } */
1152 T (2, "% hhd", var0); /* { dg-warning "nul past the end" } */
1153 T (2, "% hhd", var10); /* { dg-warning "nul past the end" } */
1154 T (2, "% hhi", a); /* { dg-warning "nul past the end" } */
1155 T (2, "% hho", a); /* { dg-warning ". . flag used with .%o." } */
1156 T (2, "% hhu", a); /* { dg-warning ". . flag used with .%u." } */
1157 T (2, "% hhx", a); /* { dg-warning ". . flag used with .%x." } */
1158
1159 /* The following results in between "0" and "0377" for -1. Although
1160 the minimum output would fit, given the '#' flag the likely output
1161 (i.e., for any non-zero argument) includes a leading zero followed
1162 by one or more octal digits, which results in the terminating nul
1163 being written past the end. Thus the "may write" warning. */
1164 T (2, "%#hho", a); /* { dg-warning "may write a terminating nul" } */
1165 /* Similar to the above, but the likely output of the directive for
1166 a non-zero argument overflows. Thus the "writing X bytes" (as
1167 opposed to "may write") warning. */
1168 T (2, "%#hhx", a); /* { dg-warning "writing between 1 and 4 bytes" } */
1169
1170 T (3, "%0hhd", a);
1171 T (3, "%1hhd", a);
1172 T (3, "%2hhd", a);
1173 T (3, "%2hhi", a);
1174 T (3, "%2hho", a);
1175 T (3, "%2hhu", a);
1176 T (3, "%2hhx", a);
1177 T (3, "%2.hhx", a);
1178
1179 T (3, "%3hhd", a); /* { dg-warning "nul past the end" } */
1180 T (3, "%3hhi", a); /* { dg-warning "nul past the end" } */
1181 T (3, "%3hho", a); /* { dg-warning "nul past the end" } */
1182 T (3, "%3hhu", a); /* { dg-warning "nul past the end" } */
1183 T (3, "%3hhx", a); /* { dg-warning "nul past the end" } */
1184 T (3, "%3.hhx", a); /* { dg-warning "nul past the end" } */
1185
1186 T (4, "%5hhd", a); /* { dg-warning "into a region" } */
1187 T (4, "%6hhi", a); /* { dg-warning "into a region" } */
1188 T (4, "%7hho", a); /* { dg-warning "into a region" } */
1189 T (4, "%8hhu", a); /* { dg-warning "into a region" } */
1190 T (4, "%9hhx", a); /* { dg-warning "into a region" } */
1191
1192 T (3, "%.hhd", a);
1193 T (3, "%.0hhd", a);
1194 T (3, "%.1hhd", a);
1195 T (3, "%.2hhd", a);
1196 T (3, "%.2hhi", a);
1197 T (3, "%.2hho", a);
1198 T (3, "%.2hhu", a);
1199 T (3, "%.2hhx", a);
1200
1201 T (3, "%.3hhd", a); /* { dg-warning "nul past the end" } */
1202 T (3, "%.3hhi", a); /* { dg-warning "nul past the end" } */
1203 T (3, "%.3hho", a); /* { dg-warning "nul past the end" } */
1204 T (3, "%.3hhu", a); /* { dg-warning "nul past the end" } */
1205 T (3, "%.3hhx", a); /* { dg-warning "nul past the end" } */
1206
1207 T (4, "%.5hhd", a); /* { dg-warning "into a region" } */
1208 T (4, "%.6hhi", a); /* { dg-warning "into a region" } */
1209 T (4, "%.7hho", a); /* { dg-warning "into a region" } */
1210 T (4, "%.8hhu", a); /* { dg-warning "into a region" } */
1211 T (4, "%.9hhx", a); /* { dg-warning "into a region" } */
1212
1213 /* Exercise cases where the type of the actual argument (whose value
1214 and range are unknown) constrain the size of the output and so
1215 can be used to avoid what would otherwise be false positives. */
1216
1217 T (2, "%hhd", (UChar)a);
1218 T (2, "%hhi", (UChar)a);
1219 T (2, "%-hhi", (UChar)a);
1220
1221 /* Exercise cases where the argument is known but width isn't. */
1222 T (0, "%*hhi", w, 0); /* { dg-warning "into a region" } */
1223 T (1, "%*hhi", w, 0); /* { dg-warning "nul past the end" } */
1224 T (2, "%*hhi", w, 0);
1225 T (2, "%*hhi", w, 12); /* { dg-warning "nul past the end" } */
1226 T (2, "%*hhi", w, 123); /* { dg-warning "into a region" } */
1227
1228 /* The argument is known but precision isn't. When the argument
1229 is zero only the first call can be diagnosed since a zero
1230 precision would result in no bytes on output. */
1231 T (0, "%.*hhi", p, 0); /* { dg-warning " writing up to \[0-9\]+ bytes" } */
1232 T (1, "%.*hhi", p, 0); /* { dg-warning "may write a terminating nul" }*/
1233 T (2, "%.*hhi", p, 0);
1234 T (2, "%.*hhi", p, 12); /* { dg-warning "nul past the end" } */
1235 T (2, "%.*hhi", p, 123); /* { dg-warning "into a region" } */
1236
1237 /* The argument is known but neither width nor precision is. */
1238 T (0, "%*.*hhi", w, p, 0); /* { dg-warning "writing up to \[0-9\]+ bytes" } */
1239 T (1, "%*.*hhi", w, p, 0); /* { dg-warning "may write a terminating nul" } */
1240 T (2, "%*.*hhi", w, p, 0);
1241 T (2, "%*.*hhi", w, p, 12); /* { dg-warning "nul past the end" } */
1242 T (2, "%*.*hhi", w, p, 123); /* { dg-warning "into a region" } */
1243
1244 /* The argument and width are known but precision isn't. */
1245 T (0, "%1.*hhi", p, 0); /* { dg-warning "into a region" } */
1246 T (0, "%-1.*hhi", p, 0); /* { dg-warning "into a region" } */
1247 T (1, "%1.*hhi", p, 0); /* { dg-warning "nul past the end" } */
1248 T (2, "%1.*hhi", p, 0);
1249 T (2, "%2.*hhi", p, 0); /* { dg-warning "nul past the end" } */
1250 T (2, "%1.*hhi", p, 12); /* { dg-warning "nul past the end" } */
1251 T (2, "%2.*hhi", p, 12); /* { dg-warning "nul past the end" } */
1252
1253 T (2, "%1.*hhi", p, 123); /* { dg-warning "into a region" } */
1254 T (2, "%2.*hhi", p, 123); /* { dg-warning "into a region" } */
1255 T (2, "%3.*hhi", p, 123); /* { dg-warning "into a region" } */
1256
1257 /* The argument and precision are known but width isn't. */
1258 T (0, "%*.1hhi", w, 0); /* { dg-warning "into a region" } */
1259 T (1, "%*.1hhi", w, 0); /* { dg-warning "nul past the end" } */
1260 T (2, "%*.1hhi", w, 0);
1261 T (2, "%*.2hhi", w, 0); /* { dg-warning "nul past the end" } */
1262 T (2, "%*.1hhi", w, 12); /* { dg-warning "nul past the end" } */
1263 T (2, "%*.2hhi", w, 12); /* { dg-warning "nul past the end" } */
1264 T (2, "%*.3hhi", w, 12); /* { dg-warning "into a region" } */
1265
1266 T (2, "%*.1hhi", w, 123); /* { dg-warning "into a region" } */
1267 T (2, "%*.2hhi", w, 123); /* { dg-warning "into a region" } */
1268 T (2, "%*.3hhi", w, 123); /* { dg-warning "into a region" } */
1269 }
1270
1271 /* Exercise the h length modifier with all integer specifiers and
1272 a non-constant argument. */
1273
1274 void test_sprintf_chk_h_nonconst (int a)
1275 {
1276 T (-1, "%hd", a);
1277
1278 T (0, "%hd", a); /* { dg-warning "into a region" } */
1279 T (0, "%hi", a); /* { dg-warning "into a region" } */
1280 T (0, "%hu", a); /* { dg-warning "into a region" } */
1281 T (0, "%hx", a); /* { dg-warning "into a region" } */
1282
1283 T (1, "%hd", a); /* { dg-warning "nul past the end" } */
1284 T (1, "%hi", a); /* { dg-warning "nul past the end" } */
1285 T (1, "%hu", a); /* { dg-warning "nul past the end" } */
1286 T (1, "%hx", a); /* { dg-warning "nul past the end" } */
1287
1288 T (1, "% hd", a); /* { dg-warning "into a region" } */
1289 T (1, "% hi", a); /* { dg-warning "into a region" } */
1290 T (1, "%+hd", a); /* { dg-warning "into a region" } */
1291 T (1, "%+hi", a); /* { dg-warning "into a region" } */
1292 T (1, "%-hd", a); /* { dg-warning "nul past the end" } */
1293 T (1, "%-hi", a); /* { dg-warning "nul past the end" } */
1294
1295 T (2, "%hd", a);
1296 T (2, "%hi", a);
1297 T (2, "%ho", a);
1298 T (2, "%hu", a);
1299 T (2, "%hx", a);
1300
1301 T (2, "% hd", a); /* { dg-warning "nul past the end" } */
1302 T (2, "% hi", a); /* { dg-warning "nul past the end" } */
1303 T (2, "% ho", a); /* { dg-warning ". . flag used with .%o." } */
1304 T (2, "% hu", a); /* { dg-warning ". . flag used with .%u." } */
1305 T (2, "% hx", a); /* { dg-warning ". . flag used with .%x." } */
1306
1307 T (2, "#%ho", a); /* { dg-warning "nul past the end" } */
1308 T (2, "#%hx", a); /* { dg-warning "nul past the end" } */
1309
1310 T (3, "%2hd", a);
1311 T (3, "%2hi", a);
1312 T (3, "%2ho", a);
1313 T (3, "%2hu", a);
1314 T (3, "%2hx", a);
1315 }
1316
1317 /* Exercise all integer specifiers with no modifier and a non-constant
1318 argument. */
1319
1320 void test_sprintf_chk_int_nonconst (int w, int p, int a)
1321 {
1322 T (-1, "%d", a);
1323
1324 T (0, "%d", a); /* { dg-warning "into a region" } */
1325 T (0, "%i", a); /* { dg-warning "into a region" } */
1326 T (0, "%u", a); /* { dg-warning "into a region" } */
1327 T (0, "%x", a); /* { dg-warning "into a region" } */
1328
1329 T (1, "%d", a); /* { dg-warning "nul past the end" } */
1330 T (1, "%i", a); /* { dg-warning "nul past the end" } */
1331 T (1, "%u", a); /* { dg-warning "nul past the end" } */
1332 T (1, "%x", a); /* { dg-warning "nul past the end" } */
1333
1334 T (1, "% d", a); /* { dg-warning "into a region" } */
1335 T (1, "% i", a); /* { dg-warning "into a region" } */
1336 T (1, "%+d", a); /* { dg-warning "into a region" } */
1337 T (1, "%+i", a); /* { dg-warning "into a region" } */
1338 T (1, "%-d", a); /* { dg-warning "nul past the end" } */
1339 T (1, "%-i", a); /* { dg-warning "nul past the end" } */
1340
1341 T (2, "%d", a);
1342 T (2, "%i", a);
1343 T (2, "%o", a);
1344 T (2, "%u", a);
1345 T (2, "%x", a);
1346
1347 T (2, "% d", a); /* { dg-warning "nul past the end" } */
1348 T (2, "% i", a); /* { dg-warning "nul past the end" } */
1349 T (2, "% o", a); /* { dg-warning ". . flag used with .%o." } */
1350 T (2, "% u", a); /* { dg-warning ". . flag used with .%u." } */
1351 T (2, "% x", a); /* { dg-warning ". . flag used with .%x." } */
1352
1353 T (2, "#%o", a); /* { dg-warning "nul past the end" } */
1354 T (2, "#%x", a); /* { dg-warning "nul past the end" } */
1355
1356 T (3, "%2d", a);
1357 T (3, "%2i", a);
1358 T (3, "%2o", a);
1359 T (3, "%2u", a);
1360 T (3, "%2x", a);
1361
1362 T (1, "%.*d", p, a); /* { dg-warning "nul past the end" } */
1363 T (2, "%.*d", p, a);
1364
1365 T (4, "%i %i", a, a);
1366 /* The following will definitely be "writing a terminating nul past the end"
1367 (i.e., not "may write".) */
1368 T (4, "%i %i ", a, a); /* { dg-warning "nul past the end" } */
1369 T (4, "%i %i %i", a, a, a); /* { dg-warning "into a region" }*/
1370 }
1371
1372 void test_sprintf_chk_e_nonconst (int w, int p, double d)
1373 {
1374 T (-1, "%E", d);
1375 T (-1, "%lE", d);
1376 T (-1, "%.E", d);
1377 T (-1, "%.lE", d);
1378 T (-1, "%*E", w, d);
1379 T (-1, "%*lE", w, d);
1380 T (-1, "%.*E", p, d);
1381 T (-1, "%.*lE", p, d);
1382 T (-1, "%*.*E", w, p, d);
1383 T (-1, "%*.*lE", w, p, d);
1384
1385 /* The least number of bytes %E can produce is 3 for "inf" and "nan". */
1386 T ( 0, "%E", d); /* { dg-warning "writing between 3 and 14 bytes into a region of size 0" } */
1387 T ( 0, "%e", d); /* { dg-warning "into a region" } */
1388 T ( 1, "%E", d); /* { dg-warning "into a region" } */
1389 T ( 1, "%e", d); /* { dg-warning "into a region" } */
1390 T ( 2, "%e", d); /* { dg-warning "into a region" } */
1391 T ( 3, "%e", d); /* { dg-warning "into a region" } */
1392 T (12, "%e", d); /* { dg-warning "nul past the end" } */
1393 T (13, "%E", d); /* 1.000000E+00 */
1394 T (13, "%e", d);
1395 T (14, "%E", d);
1396 T (14, "%e", d);
1397
1398 T ( 0, "%+E", d); /* { dg-warning "writing between 4 and 14 bytes into a region of size 0" } */
1399 T ( 0, "%-e", d); /* { dg-warning "writing between 3 and 14 bytes into a region of size 0" } */
1400 T ( 0, "% E", d); /* { dg-warning "writing between 4 and 14 bytes into a region of size 0" } */
1401
1402 /* The range of output of "%.0e" is between 3 and 7 bytes (not counting
1403 the terminating NUL. */
1404 T ( 5, "%.0e", d); /* { dg-warning "may write a terminating nul past the end" } */
1405 T ( 6, "%.0e", d); /* 1e+00 */
1406
1407 /* The range of output of "%.1e" is between 3 and 9 bytes (not counting
1408 the terminating NUL. */
1409 T ( 7, "%.1e", d); /* { dg-warning "may write a terminating nul past the end" } */
1410 T ( 8, "%.1e", d);
1411
1412 T ( 0, "%*e", 0, d); /* { dg-warning "writing between 3 and 14 bytes into a region of size 0" } */
1413 T ( 0, "%*e", w, d); /* { dg-warning "writing 3 or more bytes into a region of size 0|writing between 3 and \[0-9\]+ bytes into a region of size 0" } */
1414 }
1415
1416 void test_sprintf_chk_f_nonconst (double d)
1417 {
1418 T (-1, "%F", d);
1419 T (-1, "%lF", d);
1420
1421 T ( 0, "%F", d); /* { dg-warning "into a region" } */
1422 T ( 0, "%f", d); /* { dg-warning "into a region" } */
1423 T ( 1, "%F", d); /* { dg-warning "into a region" } */
1424 T ( 1, "%f", d); /* { dg-warning "into a region" } */
1425 T ( 2, "%F", d); /* { dg-warning "into a region" } */
1426 T ( 2, "%f", d); /* { dg-warning "into a region" } */
1427 T ( 3, "%F", d); /* { dg-warning "into a region" } */
1428 T ( 3, "%f", d); /* { dg-warning "into a region" } */
1429 T ( 4, "%F", d); /* { dg-warning "into a region" } */
1430 T ( 4, "%f", d); /* { dg-warning "into a region" } */
1431 T ( 5, "%F", d); /* { dg-warning "into a region" } */
1432 T ( 5, "%f", d); /* { dg-warning "into a region" } */
1433 T ( 6, "%F", d); /* { dg-warning "into a region" } */
1434 T ( 6, "%f", d); /* { dg-warning "into a region" } */
1435 T ( 7, "%F", d); /* { dg-warning "into a region" } */
1436 T ( 7, "%f", d); /* { dg-warning "into a region" } */
1437 T ( 8, "%F", d); /* { dg-warning "nul past the end" } */
1438 T ( 8, "%f", d); /* { dg-warning "nul past the end" } */
1439 T ( 9, "%F", d);
1440 T ( 9, "%f", d);
1441 }
1442
1443 /* Tests for __builtin_vsprintf_chk are the same as those for
1444 __builtin_sprintf_chk with non-constant arguments. */
1445 #undef T
1446 #define T(size, fmt) \
1447 (FUNC (__vsprintf_chk) (buffer (size), 0, objsize (size), fmt, va), \
1448 sink (buffer))
1449
1450 void test_vsprintf_chk_c (va_list va)
1451 {
1452 T (-1, "%c");
1453
1454 /* Verify the full text of the diagnostic for just the distinct messages
1455 and use abbreviations in subsequent test cases. */
1456 T (0, "%c"); /* { dg-warning ".%c. directive writing 1 byte into a region of size 0" } */
1457 T (1, "%c"); /* { dg-warning "writing a terminating nul past the end" } */
1458 T (1, "%c"); /* { dg-warning "nul past the end" } */
1459 T (2, "%c");
1460 T (2, "%2c"); /* { dg-warning "nul past the end" } */
1461 T (2, "%3c"); /* { dg-warning "into a region" } */
1462 T (2, "%c%c"); /* { dg-warning "nul past the end" } */
1463 T (3, "%c%c");
1464
1465 /* Wide characters. */
1466 T (0, "%lc"); /* { dg-warning "up to 6 bytes " } */
1467 T (1, "%lc"); /* { dg-warning "up to 6 bytes " } */
1468 T (2, "%lc"); /* { dg-warning "may write a terminating nul" } */
1469
1470 /* The following could result in as few as a single byte and in as many
1471 as MB_CUR_MAX, but since the MB_CUR_MAX value is a runtime property
1472 the write cannot be reliably diagnosed. */
1473 T (2, "%1lc"); /* { dg-warning "may write a terminating nul" } */
1474 /* Writing some unknown number of bytes into a field two characters wide. */
1475 T (2, "%2lc"); /* { dg-warning "nul past the end" } */
1476 T (2, "%lc%lc"); /* { dg-warning "writing up to 6 bytes into a region of size between 0 and 2" } */
1477
1478 T (3, "%lc%c"); /* { dg-warning "may write a terminating nul" } */
1479 /* Here in the best case each argument will format as single character,
1480 causing the terminating NUL to be written past the end. */
1481 T (3, "%lc%c%c"); /* { dg-warning "writing 1 byte into a region of size between 0 and 2" } */
1482 }
1483
1484 void test_vsprintf_chk_int (va_list va)
1485 {
1486 T (-1, "%d");
1487
1488 T (0, "%d"); /* { dg-warning "into a region" } */
1489 T (0, "%i"); /* { dg-warning "into a region" } */
1490 T (0, "%u"); /* { dg-warning "into a region" } */
1491 T (0, "%x"); /* { dg-warning "into a region" } */
1492
1493 T (1, "%d"); /* { dg-warning "nul past the end" } */
1494 T (1, "%i"); /* { dg-warning "nul past the end" } */
1495 T (1, "%u"); /* { dg-warning "nul past the end" } */
1496 T (1, "%x"); /* { dg-warning "nul past the end" } */
1497
1498 T (1, "% d"); /* { dg-warning "into a region" } */
1499 T (1, "% i"); /* { dg-warning "into a region" } */
1500 T (1, "%+d"); /* { dg-warning "into a region" } */
1501 T (1, "%+i"); /* { dg-warning "into a region" } */
1502 T (1, "%-d"); /* { dg-warning "nul past the end" } */
1503 T (1, "%-i"); /* { dg-warning "nul past the end" } */
1504
1505 T (2, "%d");
1506 T (2, "%i");
1507 T (2, "%o");
1508 T (2, "%u");
1509 T (2, "%x");
1510
1511 T (2, "% d"); /* { dg-warning "nul past the end" } */
1512 T (2, "% i"); /* { dg-warning "nul past the end" } */
1513 T (2, "% o"); /* { dg-warning ". . flag used with .%o." } */
1514 T (2, "% u"); /* { dg-warning ". . flag used with .%u." } */
1515 T (2, "% x"); /* { dg-warning ". . flag used with .%x." } */
1516
1517 T (2, "#%o"); /* { dg-warning "nul past the end" } */
1518 T (2, "#%x"); /* { dg-warning "nul past the end" } */
1519
1520 T (3, "%2d");
1521 T (3, "%2i");
1522 T (3, "%2o");
1523 T (3, "%2u");
1524 T (3, "%2x");
1525 }
1526
1527 #undef T
1528 #define T(size, fmt, ...) \
1529 (FUNC (snprintf) (buffer (size), objsize (size), fmt, __VA_ARGS__), \
1530 sink (buffer))
1531
1532 void test_snprintf_c_const (char *d)
1533 {
1534 T (-1, "%c", 0); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
1535
1536 /* Verify the full text of the diagnostic for just the distinct messages
1537 and use abbreviations in subsequent test cases. */
1538
1539 /* A call to snprintf with a buffer of zero size is a request to determine
1540 the size of output without writing anything into the destination. No
1541 warning must be issued. */
1542 T (0, "%c", 0);
1543 T (1, "%c", 0); /* { dg-warning "output truncated before the last format character" } */
1544 T (1, "%c", '1'); /* { dg-warning "output truncated" } */
1545 T (2, "%c", '1');
1546 T (2, "%2c", '1'); /* { dg-warning "output truncated" } */
1547 T (2, "%3c", '1'); /* { dg-warning "directive output truncated" } */
1548 T (2, "%c%c", '1', '2'); /* { dg-warning "output truncated" } */
1549 T (3, "%c%c", '1', '2');
1550
1551 /* Wide characters. */
1552 T (0, "%lc", (wint_t)0);
1553 T (1, "%lc", (wint_t)0); /* { dg-warning "output may be truncated before the last format character" } */
1554 T (2, "%lc", (wint_t)0);
1555
1556 /* The following could result in as few as a single byte and in as many
1557 as MB_CUR_MAX, but since the MB_CUR_MAX value is a runtime property
1558 the write cannot be reliably diagnosed. */
1559 T (2, "%lc", (wint_t)L'1');
1560 T (2, "%1lc", (wint_t)L'1');
1561 /* Writing at least 1 characted into a field two characters wide. */
1562 T (2, "%2lc", (wint_t)L'1'); /* { dg-warning "output truncated before the last format character" } */
1563
1564 T (3, "%lc%c", (wint_t)'1', '2');
1565 /* Here %lc may result in anywhere between 0 and MB_CUR_MAX characters
1566 so the output range is [0, 2, 6, 6] with the middle being used for
1567 the diagnostic (and the extremes for optimization). The cast is
1568 to prevent sign extension. */
1569 T (3, "%lc%c%c", (wint_t)(unsigned char)'\x80', '2', '3'); /* { dg-warning ".%c. directive output may be truncated writing 1 byte into a region of size between 0 and 2" } */
1570 /* It's reasonably safe that L'1' converts into the single byte '1'. */
1571 T (3, "%lc%c%c", (wint_t)'1', '2', '3'); /* { dg-warning "output may be truncated" } */
1572 T (3, "%lc%lc%c", (wint_t)'1', (wint_t)'2', '3'); /* { dg-warning "output may be truncated" } */
1573 }
1574
1575 #undef T
1576 #define T(size, fmt, ...) \
1577 (FUNC (__snprintf_chk) (buffer (size), objsize (size), \
1578 0, objsize (size), fmt, __VA_ARGS__), \
1579 sink (buffer))
1580
1581 void test_snprintf_chk_c_const (void)
1582 {
1583 /* Verify that specifying a size of the destination buffer that's
1584 bigger than its actual size (normally determined and passed to
1585 the function by __builtin_object_size) is diagnosed. */
1586 FUNC (__snprintf_chk)(buffer, 3, 0, 2, " "); /* { dg-warning "specified bound 3 exceeds destination size 2" } */
1587
1588 T (-1, "%c", 0); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
1589
1590 T (0, "%c", 0);
1591 T (0, "%c%c", 0, 0);
1592 T (0, "%c_%c", 0, 0);
1593 T (0, "_%c_%c", 0, 0);
1594
1595 T (1, "%c", 0); /* { dg-warning "output truncated before the last format character" } */
1596 T (1, "%c", '1'); /* { dg-warning "output truncated" } */
1597 T (2, "%c", '1');
1598 T (2, "%2c", '1'); /* { dg-warning "output truncated" } */
1599 T (2, "%3c", '1'); /* { dg-warning "directive output truncated" } */
1600 T (2, "%c%c", '1', '2'); /* { dg-warning "output truncated before the last format character" } */
1601 T (3, "%c%c", '1', '2');
1602 T (3, "%c_%c", '1', '2'); /* { dg-warning "output truncated" } */
1603
1604 /* Wide characters. */
1605 T (0, "%lc", (wint_t)0);
1606 T (1, "%lc", (wint_t)0); /* { dg-warning "output may be truncated before the last format character" } */
1607 T (2, "%lc", (wint_t)0);
1608
1609 /* The following could result in as few as a single byte and in as many
1610 as MB_CUR_MAX, but since the MB_CUR_MAX value is a runtime property
1611 the write cannot be reliably diagnosed. */
1612 T (2, "%lc", (wint_t)L'1');
1613 T (2, "%1lc", (wint_t)L'1');
1614 /* Writing at least 1 characted into a field two characters wide. */
1615 T (2, "%2lc", (wint_t)'1'); /* { dg-warning "output truncated before the last format character" } */
1616
1617 T (3, "%lc%c", (wint_t)'1', '2');
1618 /* Here %lc may result in anywhere between 0 and MB_CUR_MAX characters
1619 so the minimum number of bytes on output is 2 (plus the terminating
1620 nul), but the likely number is 3 (plus the nul). */
1621 T (3, "%lc%c%c", (wint_t)'1', '2', '3'); /* { dg-warning "output may be truncated" } */
1622 T (3, "%lc%lc%c", (wint_t)'1', (wint_t)'2', '3'); /* { dg-warning "output may be truncated" } */
1623 }
1624
1625 /* Macro to verify that calls to __builtin_vsprintf (i.e., with no size
1626 argument) issue diagnostics by correctly determining the size of
1627 the destination buffer. */
1628 #undef T
1629 #define T(size, fmt) \
1630 (FUNC (vsprintf) (buffer (size), fmt, va), \
1631 sink (buffer))
1632
1633 void test_vsprintf_s (va_list va)
1634 {
1635 T (-1, "%s");
1636
1637 T (0, "%s"); /* { dg-warning "writing a terminating nul" } */
1638 T (1, "%s");
1639 T (1, "%1s"); /* { dg-warning "writing a terminating nul past the end" } */
1640
1641 T (2, "%s%s");
1642 T (2, "%s%s1");
1643 T (2, "%s%s12"); /* { dg-warning "writing a terminating nul" } */
1644 T (2, "%s%s123"); /* { dg-warning "writing 3 bytes into a region of size 2" } */
1645 T (2, "%s_%s");
1646 T (2, "_%s%s");
1647 T (2, "_%s_%s"); /* { dg-warning "writing a terminating nul" } */
1648 }
1649
1650 /* Exercise all integer specifiers with no modifier and a non-constant
1651 argument. */
1652
1653 void test_vsprintf_int (va_list va)
1654 {
1655 T (-1, "%d");
1656
1657 T (0, "%d"); /* { dg-warning "into a region" } */
1658 T (0, "%i"); /* { dg-warning "into a region" } */
1659 T (0, "%u"); /* { dg-warning "into a region" } */
1660 T (0, "%x"); /* { dg-warning "into a region" } */
1661
1662 T (1, "%d"); /* { dg-warning "nul past the end" } */
1663 T (1, "%i"); /* { dg-warning "nul past the end" } */
1664 T (1, "%u"); /* { dg-warning "nul past the end" } */
1665 T (1, "%x"); /* { dg-warning "nul past the end" } */
1666
1667 T (1, "% d"); /* { dg-warning "into a region" } */
1668 T (1, "% i"); /* { dg-warning "into a region" } */
1669 T (1, "%+d"); /* { dg-warning "into a region" } */
1670 T (1, "%+i"); /* { dg-warning "into a region" } */
1671 T (1, "%-d"); /* { dg-warning "nul past the end" } */
1672 T (1, "%-i"); /* { dg-warning "nul past the end" } */
1673
1674 T (2, "%d");
1675 T (2, "%i");
1676 T (2, "%o");
1677 T (2, "%u");
1678 T (2, "%x");
1679
1680 T (2, "% d"); /* { dg-warning "nul past the end" } */
1681 T (2, "% i"); /* { dg-warning "nul past the end" } */
1682 T (2, "% o"); /* { dg-warning ". . flag used with .%o." } */
1683 T (2, "% u"); /* { dg-warning ". . flag used with .%u." } */
1684 T (2, "% x"); /* { dg-warning ". . flag used with .%x." } */
1685
1686 T (2, "#%o"); /* { dg-warning "nul past the end" } */
1687 T (2, "#%x"); /* { dg-warning "nul past the end" } */
1688
1689 T (3, "%2d");
1690 T (3, "%2i");
1691 T (3, "%2o");
1692 T (3, "%2u");
1693 T (3, "%2x");
1694 }
1695
1696 #undef T
1697 #define T(size, fmt) \
1698 (FUNC (vsnprintf) (buffer (size), objsize (size), fmt, va), \
1699 sink (buffer))
1700
1701 void test_vsnprintf_s (va_list va)
1702 {
1703 T (-1, "%s"); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
1704
1705 T (0, "%s");
1706 T (1, "%s");
1707 T (1, "%1s"); /* { dg-warning "output truncated before the last format character" } */
1708
1709 T (2, "%s%s");
1710 T (2, "%s%s_");
1711 T (2, "%s_%s");
1712 T (2, "_%s%s");
1713 T (2, "_%s_%s"); /* { dg-warning "output truncated" } */
1714 }
1715
1716 #undef T
1717 #define T(size, fmt) \
1718 (FUNC (__vsnprintf_chk) (buffer (size), objsize (size), \
1719 0, objsize (size), fmt, va), \
1720 sink (buffer))
1721
1722 void test_vsnprintf_chk_s (va_list va)
1723 {
1724 /* Verify that specifying a size of the destination buffer that's
1725 bigger than its actual size (normally determined and passed to
1726 the function by __builtin_object_size) is diagnosed. */
1727 FUNC (__vsnprintf_chk)(buffer, 123, 0, 122, "%-s", va); /* { dg-warning "specified bound 123 exceeds destination size 122" } */
1728
1729 FUNC (__vsnprintf_chk)(buffer, __SIZE_MAX__, 0, 2, "%-s", va); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
1730
1731 T (0, "%s");
1732 T (1, "%s");
1733 T (1, "%1s"); /* { dg-warning "output truncated before the last format character" } */
1734
1735 T (2, "%s%s");
1736 T (2, "%s%s_");
1737 T (2, "%s_%s");
1738 T (2, "_%s%s");
1739 T (2, "_%s_%s"); /* { dg-warning "output truncated" } */
1740 }