1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
/* PR middle-end/88993 - GCC 9 -Wformat-overflow=2 should reflect real
libc limits
Verify that -Wformat-overflow=2 "may exceed" warnings are not issued
for printf family of functions.
{ dg-do compile }
{ dg-options "-O -Wformat -Wformat-overflow=2 -ftrack-macro-expansion=0" }
{ dg-require-effective-target int32plus } */
#define INT_MAX __INT_MAX__
typedef __SIZE_TYPE__ size_t;
#if !__cplusplus
typedef __WCHAR_TYPE__ wchar_t;
#endif
#define T(...) __builtin_printf (__VA_ARGS__)
/* Exercise the "%c" directive with constant arguments. */
void test_printf_c_const (int width)
{
/* Verify that a warning is only issued when the output is definitely
exceeded but not when exceeding it is possible but not inevitable. */
T ("%2147483647c", '1');
T ("X%2147483647c", '2'); /* { dg-warning ".%*c. directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
T ("%2147483647cY", '3'); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T ("%2147483648c", '1'); /* { dg-warning ".%2147483648c. directive output of 2147483648 bytes exceeds .INT_MAX." } */
T ("X%2147483649c", '2'); /* { dg-warning ".%2147483649c. directive output of 2147483649 bytes exceeds .INT_MAX." } */
T ("%2147483650cY", '3'); /* { dg-warning ".%2147483650c. directive output of 2147483650 bytes exceeds .INT_MAX." } */
T ("%*c", INT_MAX, '1');
T ("X%*c", INT_MAX, '1'); /* { dg-warning ".%*c. directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
T ("%*cY", INT_MAX, '1'); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T ("X%*c", INT_MAX - 1, '1');
T ("%*cY", INT_MAX - 1, '1');
T ("%*cY", INT_MAX, '1'); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T ("X%*c", INT_MAX, '1'); /* { dg-warning ".%*c. directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
if (width > INT_MAX - 1)
width = INT_MAX - 1;
T ("%*c", width, '1');
T ("X%*c", width, '1');
T ("%*cY", width, '1');
T ("%*c", width, '1');
T ("X%*c", width, '1');
T ("%*cY", width, '1');
T ("%*c%*c", width, '1', width, '2');
T ("X%*cY%*cZ", width, '1', width, '2');
if (width < 4096)
width = 4096;
T ("%*c", width, '1');
T ("X%*c", width, '1');
T ("%*cY", width, '1');
if (width < INT_MAX - 1)
width = INT_MAX - 1;
T ("%*c", width, '1');
T ("X%*c", width, '2');
T ("%*cY", width, '3');
T ("X%*cY", width, '4'); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
}
/* Exercise the "%s" directive with constant arguments. */
void test_printf_s_const (int width, const char *s)
{
T ("%2147483647s", s);
T ("X%2147483647s", s); /* { dg-warning ".%2147483647s. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
T ("%2147483647sY", s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T ("%2147483648s", s); /* { dg-warning "%2147483648s. directive output between 2147483648 and \[0-9\]+ bytes exceeds .INT_MAX." } */
T ("X%2147483649s", s); /* { dg-warning "%2147483649s. directive output between 2147483649 and \[0-9\]+ bytes exceeds .INT_MAX." } */
T ("%2147483650sY", s); /* { dg-warning ".%2147483650s. directive output between 2147483650 and \[0-9\]+ bytes exceeds .INT_MAX." } */
T ("%*s", INT_MAX, s);
T ("X%*s", INT_MAX, s); /* { dg-warning ".%\\\*s. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
T ("%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T ("X%*s", INT_MAX - 1, s);
T ("%*sY", INT_MAX - 1, s);
T ("%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T ("X%*s", INT_MAX, s); /* { dg-warning ".%\\\*s. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
if (width > INT_MAX - 1)
width = INT_MAX - 1;
T ("%*s", width, s);
T ("X%*s", width, s);
T ("%*sY", width, s);
T ("%*s", width, s);
T ("X%*s", width, s);
T ("%*sY", width, s);
T ("%*s%*s", width, s, width, s);
T ("X%*sY%*sZ", width, s, width, s);
if (width < 4096)
width = 4096;
T ("%*s", width, s);
T ("X%*s", width, s);
T ("%*sY", width, s);
if (width < INT_MAX - 1)
width = INT_MAX - 1;
T ("%*s", width, s);
T ("X%*s", width, s);
T ("%*sY", width, s);
T ("X%*sY", width, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
}
/* Exercise the "%ls" directive with constant arguments. */
void test_printf_ls_const (int width, const wchar_t *s)
{
T ("%2147483647ls", s);
T ("X%2147483647ls", s); /* { dg-warning ".%2147483647ls. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
T ("%2147483647lsY", s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T ("%2147483648ls", s); /* { dg-warning "%2147483648ls. directive output between 2147483648 and \[0-9\]+ bytes exceeds .INT_MAX." } */
T ("X%2147483649ls", s); /* { dg-warning "%2147483649ls. directive output between 2147483649 and \[0-9\]+ bytes exceeds .INT_MAX." } */
T ("%2147483650lsY", s); /* { dg-warning ".%2147483650ls. directive output between 2147483650 and \[0-9\]+ bytes exceeds .INT_MAX." } */
T ("%*ls", INT_MAX, s);
T ("X%*ls", INT_MAX, s); /* { dg-warning ".%\\\*ls. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
T ("%*lsY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T ("X%*ls", INT_MAX - 1, s);
T ("%*lsY", INT_MAX - 1, s);
T ("%*lsY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T ("X%*ls", INT_MAX, s); /* { dg-warning ".%\\\*ls. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
if (width > INT_MAX - 1)
width = INT_MAX - 1;
T ("%*ls", width, s);
T ("X%*ls", width, s);
T ("%*lsY", width, s);
T ("%*ls", width, s);
T ("X%*ls", width, s);
T ("%*lsY", width, s);
T ("%*ls%*ls", width, s, width, s);
T ("X%*lsY%*lsZ", width, s, width, s);
if (width < 4096)
width = 4096;
T ("%*ls", width, s);
T ("X%*ls", width, s);
T ("%*lsY", width, s);
if (width < INT_MAX - 1)
width = INT_MAX - 1;
T ("%*ls", width, s);
T ("X%*ls", width, s);
T ("%*lsY", width, s);
T ("X%*lsY", width, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
}
/* Also exercise printf_chk. */
#undef T
#define T(...) __builtin___printf_chk (__VA_ARGS__)
void test_printf_chk_s_const (int width)
{
const char *s = "0123456789";
T (0, "%2147483647s", s);
T (0, "X%2147483647s", s); /* { dg-warning ".%2147483647s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
T (0, "%2147483647sY", s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (0, "%2147483648s", s); /* { dg-warning "%2147483648s. directive output of 2147483648 bytes exceeds .INT_MAX." } */
T (0, "X%2147483649s", s); /* { dg-warning "%2147483649s. directive output of 2147483649 bytes exceeds .INT_MAX." } */
T (0, "%2147483650sY", s); /* { dg-warning ".%2147483650s. directive output of 2147483650 bytes exceeds .INT_MAX." } */
T (0, "%*s", INT_MAX, s);
T (0, "X%*s", INT_MAX, s); /* { dg-warning ".%\\\*s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
T (0, "%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (0, "X%*s", INT_MAX - 1, s);
T (0, "%*sY", INT_MAX - 1, s);
T (0, "%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (0, "X%*s", INT_MAX, s); /* { dg-warning ".%\\\*s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
if (width > INT_MAX - 1)
width = INT_MAX - 1;
T (0, "%*s", width, s);
T (0, "X%*s", width, s);
T (0, "%*sY", width, s);
T (0, "%*s", width, s);
T (0, "X%*s", width, s);
T (0, "%*sY", width, s);
T (0, "%*s%*s", width, s, width, s);
T (0, "X%*sY%*sZ", width, s, width, s);
if (width < 4096)
width = 4096;
T (0, "%*s", width, s);
T (0, "X%*s", width, s);
T (0, "%*sY", width, s);
if (width < INT_MAX - 1)
width = INT_MAX - 1;
T (0, "%*s", width, s);
T (0, "X%*s", width, s);
T (0, "%*sY", width, s);
T (0, "X%*sY", width, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
}
/* And finally exercise printf_unlocked. */
#undef T
#define T(...) __builtin_printf_unlocked (__VA_ARGS__)
void test_printf_unlocked_s_const (int width)
{
const char *s = "0123456789";
T ("%2147483647s", s);
T ("X%2147483647s", s); /* { dg-warning ".%2147483647s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
T ("%2147483647sY", s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T ("%2147483648s", s); /* { dg-warning "%2147483648s. directive output of 2147483648 bytes exceeds .INT_MAX." } */
T ("X%2147483649s", s); /* { dg-warning "%2147483649s. directive output of 2147483649 bytes exceeds .INT_MAX." } */
T ("%2147483650sY", s); /* { dg-warning ".%2147483650s. directive output of 2147483650 bytes exceeds .INT_MAX." } */
T ("%*s", INT_MAX, s);
T ("X%*s", INT_MAX, s); /* { dg-warning ".%\\\*s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
T ("%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T ("X%*s", INT_MAX - 1, s);
T ("%*sY", INT_MAX - 1, s);
T ("%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T ("X%*s", INT_MAX, s); /* { dg-warning ".%\\\*s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
if (width > INT_MAX - 1)
width = INT_MAX - 1;
T ("%*s", width, s);
T ("X%*s", width, s);
T ("%*sY", width, s);
T ("%*s", width, s);
T ("X%*s", width, s);
T ("%*sY", width, s);
T ("%*s%*s", width, s, width, s);
T ("X%*sY%*sZ", width, s, width, s);
if (width < 4096)
width = 4096;
T ("%*s", width, s);
T ("X%*s", width, s);
T ("%*sY", width, s);
if (width < INT_MAX - 1)
width = INT_MAX - 1;
T ("%*s", width, s);
T ("X%*s", width, s);
T ("%*sY", width, s);
T ("X%*sY", width, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
}
|