]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gcc.dg/tree-ssa/builtin-fprintf-warn-2.c
1 /* PR middle-end/88993 - GCC 9 -Wformat-overflow=2 should reflect real
3 Verify that -Wformat-overflow=2 "may exceed" warnings are not issued
4 for printf family of functions.
6 { dg-options "-O -Wformat -Wformat-overflow=2 -ftrack-macro-expansion=0" }
7 { dg-require-effective-target int32plus } */
9 #define INT_MAX __INT_MAX__
11 typedef __SIZE_TYPE__
size_t ;
14 typedef __WCHAR_TYPE__
wchar_t ;
17 typedef struct FILE FILE ;
21 #define T(...) __builtin_fprintf (__VA_ARGS__)
23 /* Exercise the "%c" directive with constant arguments. */
25 void test_fprintf_c_const ( int width
)
27 /* Verify that a warning is only issued when the output is definitely
28 exceeded but not when exceeding it is possible but not inevitable.
29 Also verify that a note is printed with amount of output produced
30 by the call (the result - 1). */
31 T ( fp
, "%2147483647c" , '1' );
32 T ( fp
, "X%2147483647c" , '2' ); /* { dg-warning ".%2147483647c. directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
33 /* { dg-message ".__builtin_fprintf. output 2147483649 bytes" "note" { target *-*-* } .-1 } */
34 T ( fp
, "%2147483647cY" , '3' ); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
36 T ( fp
, "%2147483648c" , '1' ); /* { dg-warning ".%2147483648c. directive output of 2147483648 bytes exceeds .INT_MAX." } */
37 T ( fp
, "X%2147483649c" , '2' ); /* { dg-warning ".%2147483649c. directive output of 2147483649 bytes exceeds .INT_MAX." } */
38 T ( fp
, "%2147483650cY" , '3' ); /* { dg-warning ".%2147483650c. directive output of 2147483650 bytes exceeds .INT_MAX." } */
40 T ( fp
, "%*c" , INT_MAX
, '1' );
41 T ( fp
, "X%*c" , INT_MAX
, '1' ); /* { dg-warning ".%*c. directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
42 T ( fp
, "%*cY" , INT_MAX
, '1' ); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
44 T ( fp
, "X%*c" , INT_MAX
- 1 , '1' );
45 T ( fp
, "%*cY" , INT_MAX
- 1 , '1' );
47 T ( fp
, "%*cY" , INT_MAX
, '1' ); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
48 T ( fp
, "X%*c" , INT_MAX
, '1' ); /* { dg-warning ".%*c. directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
51 /* Exercise the "%c" directive with arguments in a known range. */
53 void test_fprintf_c_range ( int width
)
55 /* Verify that an known upper bound doesn't trigger a warning. */
56 if ( width
> INT_MAX
- 1 )
59 T ( fp
, "%*c" , width
, '1' );
60 T ( fp
, "X%*c" , width
, '1' );
61 T ( fp
, "%*cY" , width
, '1' );
63 T ( fp
, "%*c" , width
, '1' );
64 T ( fp
, "X%*c" , width
, '1' );
65 T ( fp
, "%*cY" , width
, '1' );
67 T ( fp
, "%*c%*c" , width
, '1' , width
, '2' );
68 T ( fp
, "X%*cY%*cZ" , width
, '1' , width
, '2' );
70 /* Verify that a lower bound in excess of 4095 doesn't trigger
75 T ( fp
, "%*c" , width
, '1' );
76 T ( fp
, "X%*c" , width
, '1' );
77 T ( fp
, "%*cY" , width
, '1' );
79 /* Verify that a large lower bound triggers a warning when the total
80 result of the function definitely exceeds INT_MAX. */
81 if ( width
< INT_MAX
- 1 )
84 T ( fp
, "%*c" , width
, '1' );
85 T ( fp
, "X%*c" , width
, '2' );
86 T ( fp
, "%*cY" , width
, '3' );
87 T ( fp
, "X%*cY" , width
, '4' ); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
88 /* { dg-message ".__builtin_fprintf. output 2147483649 bytes" "note" { target *-*-* } .-1 } */
92 /* Exercise the "%s" directive. */
94 void test_fprintf_s_const ( int width
, const char * s
)
96 T ( fp
, "%2147483647s" , s
);
97 T ( fp
, "%2147483647s" , "1" );
99 T ( fp
, "%2147483647.2147483647s" , s
);
100 T ( fp
, "%2147483647.2147483647s" , "12" );
102 T ( fp
, "X%2147483647s" , s
); /* { dg-warning ".%2147483647s. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
103 T ( fp
, "%2147483647sY" , s
); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
105 T ( fp
, "X%2147483647.1s" , s
); /* { dg-warning ".%2147483647\\\.1s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
106 T ( fp
, "%2147483647.2sY" , s
); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
108 T ( fp
, "X%1.2147483647s" , s
);
109 T ( fp
, "%2.2147483647sY" , s
);
111 T ( fp
, "X%1.2147483647s" , "123" );
112 T ( fp
, "%2.2147483647sY" , "1234" );
114 T ( fp
, "%2147483648s" , s
); /* { dg-warning "%2147483648s. directive output between 2147483648 and \[0-9\]+ bytes exceeds .INT_MAX." } */
115 T ( fp
, "X%2147483649s" , s
); /* { dg-warning "%2147483649s. directive output between 2147483649 and \[0-9\]+ bytes exceeds .INT_MAX." } */
116 T ( fp
, "%2147483650sY" , s
); /* { dg-warning ".%2147483650s. directive output between 2147483650 and \[0-9\]+ bytes exceeds .INT_MAX." } */
118 T ( fp
, "%*s" , INT_MAX
, s
);
119 T ( fp
, "X%*s" , INT_MAX
, s
); /* { dg-warning ".%\\\*s. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
120 T ( fp
, "%*sY" , INT_MAX
, s
); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
122 T ( fp
, "X%*s" , INT_MAX
- 1 , s
);
123 T ( fp
, "%*sY" , INT_MAX
- 1 , s
);
125 T ( fp
, "%*sY" , INT_MAX
, s
); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
126 T ( fp
, "X%*s" , INT_MAX
, s
); /* { dg-warning ".%\\\*s. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
128 if ( width
> INT_MAX
- 1 )
131 T ( fp
, "%*s" , width
, s
);
132 T ( fp
, "X%*s" , width
, s
);
133 T ( fp
, "%*sY" , width
, s
);
135 T ( fp
, "%*s" , width
, s
);
136 T ( fp
, "X%*s" , width
, s
);
137 T ( fp
, "%*sY" , width
, s
);
139 T ( fp
, "%*s%*s" , width
, s
, width
, s
);
140 T ( fp
, "X%*sY%*sZ" , width
, s
, width
, s
);
145 T ( fp
, "%*s" , width
, s
);
146 T ( fp
, "X%*s" , width
, s
);
147 T ( fp
, "%*sY" , width
, s
);
149 if ( width
< INT_MAX
- 1 )
152 T ( fp
, "%*s" , width
, s
);
153 T ( fp
, "X%*s" , width
, s
);
154 T ( fp
, "%*sY" , width
, s
);
155 T ( fp
, "X%*sY" , width
, s
); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
158 /* Exercise the "%ls" directive. */
160 void test_fprintf_ls_const ( int width
, const wchar_t * s
)
162 T ( fp
, "%2147483647ls" , s
);
163 T ( fp
, "X%2147483647ls" , s
); /* { dg-warning ".%2147483647ls. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
164 T ( fp
, "%2147483647lsY" , s
); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
166 T ( fp
, "%2147483648ls" , s
); /* { dg-warning "%2147483648ls. directive output between 2147483648 and \[0-9\]+ bytes exceeds .INT_MAX." } */
167 T ( fp
, "X%2147483649ls" , s
); /* { dg-warning "%2147483649ls. directive output between 2147483649 and \[0-9\]+ bytes exceeds .INT_MAX." } */
168 T ( fp
, "%2147483650lsY" , s
); /* { dg-warning ".%2147483650ls. directive output between 2147483650 and \[0-9\]+ bytes exceeds .INT_MAX." } */
170 T ( fp
, "%*ls" , INT_MAX
, s
);
171 T ( fp
, "X%*ls" , INT_MAX
, s
); /* { dg-warning ".%\\\*ls. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
172 T ( fp
, "%*lsY" , INT_MAX
, s
); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
174 T ( fp
, "X%*ls" , INT_MAX
- 1 , s
);
175 T ( fp
, "%*lsY" , INT_MAX
- 1 , s
);
177 T ( fp
, "%*lsY" , INT_MAX
, s
); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
178 T ( fp
, "X%*ls" , INT_MAX
, s
); /* { dg-warning ".%\\\*ls. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
180 if ( width
> INT_MAX
- 1 )
183 T ( fp
, "%*ls" , width
, s
);
184 T ( fp
, "X%*ls" , width
, s
);
185 T ( fp
, "%*lsY" , width
, s
);
187 T ( fp
, "%*ls" , width
, s
);
188 T ( fp
, "X%*ls" , width
, s
);
189 T ( fp
, "%*lsY" , width
, s
);
191 T ( fp
, "%*ls%*ls" , width
, s
, width
, s
);
192 T ( fp
, "X%*lsY%*lsZ" , width
, s
, width
, s
);
197 T ( fp
, "%*ls" , width
, s
);
198 T ( fp
, "X%*ls" , width
, s
);
199 T ( fp
, "%*lsY" , width
, s
);
201 if ( width
< INT_MAX
- 1 )
204 T ( fp
, "%*ls" , width
, s
);
205 T ( fp
, "X%*ls" , width
, s
);
206 T ( fp
, "%*lsY" , width
, s
);
207 T ( fp
, "X%*lsY" , width
, s
); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
211 /* Also exercise fprintf_chk. */
214 #define T(...) __builtin___fprintf_chk (__VA_ARGS__)
216 void test_fprintf_chk_s_const ( int width
)
218 const char * s
= "0123456789" ;
220 T ( fp
, 0 , "%2147483647s" , s
);
221 T ( fp
, 0 , "X%2147483647s" , s
); /* { dg-warning ".%2147483647s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
222 T ( fp
, 0 , "%2147483647sY" , s
); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
224 T ( fp
, 0 , "%2147483648s" , s
); /* { dg-warning "%2147483648s. directive output of 2147483648 bytes exceeds .INT_MAX." } */
225 T ( fp
, 0 , "X%2147483649s" , s
); /* { dg-warning "%2147483649s. directive output of 2147483649 bytes exceeds .INT_MAX." } */
226 T ( fp
, 0 , "%2147483650sY" , s
); /* { dg-warning ".%2147483650s. directive output of 2147483650 bytes exceeds .INT_MAX." } */
228 T ( fp
, 0 , "%*s" , INT_MAX
, s
);
229 T ( fp
, 0 , "X%*s" , INT_MAX
, s
); /* { dg-warning ".%\\\*s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
230 T ( fp
, 0 , "%*sY" , INT_MAX
, s
); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
232 T ( fp
, 0 , "X%*s" , INT_MAX
- 1 , s
);
233 T ( fp
, 0 , "%*sY" , INT_MAX
- 1 , s
);
235 T ( fp
, 0 , "%*sY" , INT_MAX
, s
); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
236 T ( fp
, 0 , "X%*s" , INT_MAX
, s
); /* { dg-warning ".%\\\*s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
238 if ( width
> INT_MAX
- 1 )
241 T ( fp
, 0 , "%*s" , width
, s
);
242 T ( fp
, 0 , "X%*s" , width
, s
);
243 T ( fp
, 0 , "%*sY" , width
, s
);
245 T ( fp
, 0 , "%*s" , width
, s
);
246 T ( fp
, 0 , "X%*s" , width
, s
);
247 T ( fp
, 0 , "%*sY" , width
, s
);
249 T ( fp
, 0 , "%*s%*s" , width
, s
, width
, s
);
250 T ( fp
, 0 , "X%*sY%*sZ" , width
, s
, width
, s
);
255 T ( fp
, 0 , "%*s" , width
, s
);
256 T ( fp
, 0 , "X%*s" , width
, s
);
257 T ( fp
, 0 , "%*sY" , width
, s
);
259 if ( width
< INT_MAX
- 1 )
262 T ( fp
, 0 , "%*s" , width
, s
);
263 T ( fp
, 0 , "X%*s" , width
, s
);
264 T ( fp
, 0 , "%*sY" , width
, s
);
265 T ( fp
, 0 , "X%*sY" , width
, s
); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
269 /* And finally exercise fprintf_unlocked. */
272 #define T(...) __builtin_fprintf_unlocked (__VA_ARGS__)
274 void test_fprintf_unlocked_s_const ( int width
)
276 const char * s
= "0123456789" ;
278 T ( fp
, "%2147483647s" , s
);
279 T ( fp
, "X%2147483647s" , s
); /* { dg-warning ".%2147483647s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
280 T ( fp
, "%2147483647sY" , s
); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
282 T ( fp
, "%2147483648s" , s
); /* { dg-warning "%2147483648s. directive output of 2147483648 bytes exceeds .INT_MAX." } */
283 T ( fp
, "X%2147483649s" , s
); /* { dg-warning "%2147483649s. directive output of 2147483649 bytes exceeds .INT_MAX." } */
284 T ( fp
, "%2147483650sY" , s
); /* { dg-warning ".%2147483650s. directive output of 2147483650 bytes exceeds .INT_MAX." } */
286 T ( fp
, "%*s" , INT_MAX
, s
);
287 T ( fp
, "X%*s" , INT_MAX
, s
); /* { dg-warning ".%\\\*s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
288 T ( fp
, "%*sY" , INT_MAX
, s
); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
290 T ( fp
, "X%*s" , INT_MAX
- 1 , s
);
291 T ( fp
, "%*sY" , INT_MAX
- 1 , s
);
293 T ( fp
, "%*sY" , INT_MAX
, s
); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
294 T ( fp
, "X%*s" , INT_MAX
, s
); /* { dg-warning ".%\\\*s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
296 if ( width
> INT_MAX
- 1 )
299 T ( fp
, "%*s" , width
, s
);
300 T ( fp
, "X%*s" , width
, s
);
301 T ( fp
, "%*sY" , width
, s
);
303 T ( fp
, "%*s" , width
, s
);
304 T ( fp
, "X%*s" , width
, s
);
305 T ( fp
, "%*sY" , width
, s
);
307 T ( fp
, "%*s%*s" , width
, s
, width
, s
);
308 T ( fp
, "X%*sY%*sZ" , width
, s
, width
, s
);
313 T ( fp
, "%*s" , width
, s
);
314 T ( fp
, "X%*s" , width
, s
);
315 T ( fp
, "%*sY" , width
, s
);
317 if ( width
< INT_MAX
- 1 )
320 T ( fp
, "%*s" , width
, s
);
321 T ( fp
, "X%*s" , width
, s
);
322 T ( fp
, "%*sY" , width
, s
);
323 T ( fp
, "X%*sY" , width
, s
); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */