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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
/* 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
typedef struct FILE FILE;
FILE *fp;
#define T(...) __builtin_fprintf (__VA_ARGS__)
/* Exercise the "%c" directive with constant arguments. */
void test_fprintf_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.
Also verify that a note is printed with amount of output produced
by the call (the result - 1). */
T (fp, "%2147483647c", '1');
T (fp, "X%2147483647c", '2'); /* { dg-warning ".%2147483647c. directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
/* { dg-message ".__builtin_fprintf. output 2147483649 bytes" "note" { target *-*-* } .-1 } */
T (fp, "%2147483647cY", '3'); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (fp, "%2147483648c", '1'); /* { dg-warning ".%2147483648c. directive output of 2147483648 bytes exceeds .INT_MAX." } */
T (fp, "X%2147483649c", '2'); /* { dg-warning ".%2147483649c. directive output of 2147483649 bytes exceeds .INT_MAX." } */
T (fp, "%2147483650cY", '3'); /* { dg-warning ".%2147483650c. directive output of 2147483650 bytes exceeds .INT_MAX." } */
T (fp, "%*c", INT_MAX, '1');
T (fp, "X%*c", INT_MAX, '1'); /* { dg-warning ".%*c. directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
T (fp, "%*cY", INT_MAX, '1'); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (fp, "X%*c", INT_MAX - 1, '1');
T (fp, "%*cY", INT_MAX - 1, '1');
T (fp, "%*cY", INT_MAX, '1'); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (fp, "X%*c", INT_MAX, '1'); /* { dg-warning ".%*c. directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
}
/* Exercise the "%c" directive with arguments in a known range. */
void test_fprintf_c_range (int width)
{
/* Verify that an known upper bound doesn't trigger a warning. */
if (width > INT_MAX - 1)
width = INT_MAX - 1;
T (fp, "%*c", width, '1');
T (fp, "X%*c", width, '1');
T (fp, "%*cY", width, '1');
T (fp, "%*c", width, '1');
T (fp, "X%*c", width, '1');
T (fp, "%*cY", width, '1');
T (fp, "%*c%*c", width, '1', width, '2');
T (fp, "X%*cY%*cZ", width, '1', width, '2');
/* Verify that a lower bound in excess of 4095 doesn't trigger
a warning. */
if (width < 4096)
width = 4096;
T (fp, "%*c", width, '1');
T (fp, "X%*c", width, '1');
T (fp, "%*cY", width, '1');
/* Verify that a large lower bound triggers a warning when the total
result of the function definitely exceeds INT_MAX. */
if (width < INT_MAX - 1)
width = INT_MAX - 1;
T (fp, "%*c", width, '1');
T (fp, "X%*c", width, '2');
T (fp, "%*cY", width, '3');
T (fp, "X%*cY", width, '4'); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
/* { dg-message ".__builtin_fprintf. output 2147483649 bytes" "note" { target *-*-* } .-1 } */
}
/* Exercise the "%s" directive. */
void test_fprintf_s_const (int width, const char *s)
{
T (fp, "%2147483647s", s);
T (fp, "%2147483647s", "1");
T (fp, "%2147483647.2147483647s", s);
T (fp, "%2147483647.2147483647s", "12");
T (fp, "X%2147483647s", s); /* { dg-warning ".%2147483647s. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
T (fp, "%2147483647sY", s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (fp, "X%2147483647.1s", s); /* { dg-warning ".%2147483647\\\.1s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
T (fp, "%2147483647.2sY", s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (fp, "X%1.2147483647s", s);
T (fp, "%2.2147483647sY", s);
T (fp, "X%1.2147483647s", "123");
T (fp, "%2.2147483647sY", "1234");
T (fp, "%2147483648s", s); /* { dg-warning "%2147483648s. directive output between 2147483648 and \[0-9\]+ bytes exceeds .INT_MAX." } */
T (fp, "X%2147483649s", s); /* { dg-warning "%2147483649s. directive output between 2147483649 and \[0-9\]+ bytes exceeds .INT_MAX." } */
T (fp, "%2147483650sY", s); /* { dg-warning ".%2147483650s. directive output between 2147483650 and \[0-9\]+ bytes exceeds .INT_MAX." } */
T (fp, "%*s", INT_MAX, s);
T (fp, "X%*s", INT_MAX, s); /* { dg-warning ".%\\\*s. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
T (fp, "%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (fp, "X%*s", INT_MAX - 1, s);
T (fp, "%*sY", INT_MAX - 1, s);
T (fp, "%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (fp, "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 (fp, "%*s", width, s);
T (fp, "X%*s", width, s);
T (fp, "%*sY", width, s);
T (fp, "%*s", width, s);
T (fp, "X%*s", width, s);
T (fp, "%*sY", width, s);
T (fp, "%*s%*s", width, s, width, s);
T (fp, "X%*sY%*sZ", width, s, width, s);
if (width < 4096)
width = 4096;
T (fp, "%*s", width, s);
T (fp, "X%*s", width, s);
T (fp, "%*sY", width, s);
if (width < INT_MAX - 1)
width = INT_MAX - 1;
T (fp, "%*s", width, s);
T (fp, "X%*s", width, s);
T (fp, "%*sY", width, s);
T (fp, "X%*sY", width, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
}
/* Exercise the "%ls" directive. */
void test_fprintf_ls_const (int width, const wchar_t *s)
{
T (fp, "%2147483647ls", s);
T (fp, "X%2147483647ls", s); /* { dg-warning ".%2147483647ls. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
T (fp, "%2147483647lsY", s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (fp, "%2147483648ls", s); /* { dg-warning "%2147483648ls. directive output between 2147483648 and \[0-9\]+ bytes exceeds .INT_MAX." } */
T (fp, "X%2147483649ls", s); /* { dg-warning "%2147483649ls. directive output between 2147483649 and \[0-9\]+ bytes exceeds .INT_MAX." } */
T (fp, "%2147483650lsY", s); /* { dg-warning ".%2147483650ls. directive output between 2147483650 and \[0-9\]+ bytes exceeds .INT_MAX." } */
T (fp, "%*ls", INT_MAX, s);
T (fp, "X%*ls", INT_MAX, s); /* { dg-warning ".%\\\*ls. directive output between 2147483647 and \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
T (fp, "%*lsY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (fp, "X%*ls", INT_MAX - 1, s);
T (fp, "%*lsY", INT_MAX - 1, s);
T (fp, "%*lsY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (fp, "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 (fp, "%*ls", width, s);
T (fp, "X%*ls", width, s);
T (fp, "%*lsY", width, s);
T (fp, "%*ls", width, s);
T (fp, "X%*ls", width, s);
T (fp, "%*lsY", width, s);
T (fp, "%*ls%*ls", width, s, width, s);
T (fp, "X%*lsY%*lsZ", width, s, width, s);
if (width < 4096)
width = 4096;
T (fp, "%*ls", width, s);
T (fp, "X%*ls", width, s);
T (fp, "%*lsY", width, s);
if (width < INT_MAX - 1)
width = INT_MAX - 1;
T (fp, "%*ls", width, s);
T (fp, "X%*ls", width, s);
T (fp, "%*lsY", width, s);
T (fp, "X%*lsY", width, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
}
/* Also exercise fprintf_chk. */
#undef T
#define T(...) __builtin___fprintf_chk (__VA_ARGS__)
void test_fprintf_chk_s_const (int width)
{
const char *s = "0123456789";
T (fp, 0, "%2147483647s", s);
T (fp, 0, "X%2147483647s", s); /* { dg-warning ".%2147483647s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
T (fp, 0, "%2147483647sY", s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (fp, 0, "%2147483648s", s); /* { dg-warning "%2147483648s. directive output of 2147483648 bytes exceeds .INT_MAX." } */
T (fp, 0, "X%2147483649s", s); /* { dg-warning "%2147483649s. directive output of 2147483649 bytes exceeds .INT_MAX." } */
T (fp, 0, "%2147483650sY", s); /* { dg-warning ".%2147483650s. directive output of 2147483650 bytes exceeds .INT_MAX." } */
T (fp, 0, "%*s", INT_MAX, s);
T (fp, 0, "X%*s", INT_MAX, s); /* { dg-warning ".%\\\*s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
T (fp, 0, "%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (fp, 0, "X%*s", INT_MAX - 1, s);
T (fp, 0, "%*sY", INT_MAX - 1, s);
T (fp, 0, "%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (fp, 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 (fp, 0, "%*s", width, s);
T (fp, 0, "X%*s", width, s);
T (fp, 0, "%*sY", width, s);
T (fp, 0, "%*s", width, s);
T (fp, 0, "X%*s", width, s);
T (fp, 0, "%*sY", width, s);
T (fp, 0, "%*s%*s", width, s, width, s);
T (fp, 0, "X%*sY%*sZ", width, s, width, s);
if (width < 4096)
width = 4096;
T (fp, 0, "%*s", width, s);
T (fp, 0, "X%*s", width, s);
T (fp, 0, "%*sY", width, s);
if (width < INT_MAX - 1)
width = INT_MAX - 1;
T (fp, 0, "%*s", width, s);
T (fp, 0, "X%*s", width, s);
T (fp, 0, "%*sY", width, s);
T (fp, 0, "X%*sY", width, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
}
/* And finally exercise fprintf_unlocked. */
#undef T
#define T(...) __builtin_fprintf_unlocked (__VA_ARGS__)
void test_fprintf_unlocked_s_const (int width)
{
const char *s = "0123456789";
T (fp, "%2147483647s", s);
T (fp, "X%2147483647s", s); /* { dg-warning ".%2147483647s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
T (fp, "%2147483647sY", s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (fp, "%2147483648s", s); /* { dg-warning "%2147483648s. directive output of 2147483648 bytes exceeds .INT_MAX." } */
T (fp, "X%2147483649s", s); /* { dg-warning "%2147483649s. directive output of 2147483649 bytes exceeds .INT_MAX." } */
T (fp, "%2147483650sY", s); /* { dg-warning ".%2147483650s. directive output of 2147483650 bytes exceeds .INT_MAX." } */
T (fp, "%*s", INT_MAX, s);
T (fp, "X%*s", INT_MAX, s); /* { dg-warning ".%\\\*s. directive output of 2147483647 bytes causes result to exceed .INT_MAX." } */
T (fp, "%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (fp, "X%*s", INT_MAX - 1, s);
T (fp, "%*sY", INT_MAX - 1, s);
T (fp, "%*sY", INT_MAX, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
T (fp, "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 (fp, "%*s", width, s);
T (fp, "X%*s", width, s);
T (fp, "%*sY", width, s);
T (fp, "%*s", width, s);
T (fp, "X%*s", width, s);
T (fp, "%*sY", width, s);
T (fp, "%*s%*s", width, s, width, s);
T (fp, "X%*sY%*sZ", width, s, width, s);
if (width < 4096)
width = 4096;
T (fp, "%*s", width, s);
T (fp, "X%*s", width, s);
T (fp, "%*sY", width, s);
if (width < INT_MAX - 1)
width = INT_MAX - 1;
T (fp, "%*s", width, s);
T (fp, "X%*s", width, s);
T (fp, "%*sY", width, s);
T (fp, "X%*sY", width, s); /* { dg-warning ".Y. directive output of 1 bytes causes result to exceed .INT_MAX." } */
}
|