| 1 |
/* Verify that all sprintf built-ins detect overflow involving directives |
| 2 |
with non-constant arguments known to be constrained by some range of |
| 3 |
values, and even when writing into dynamically allocated buffers. |
| 4 |
-O2 (-ftree-vrp) is necessary for the tests involving ranges to pass, |
| 5 |
otherwise -O1 is sufficient. |
| 6 |
{ dg-do compile } |
| 7 |
{ dg-options "-O2 -Wformat -Wformat-overflow=1 -ftrack-macro-expansion=0" } */ |
| 8 |
|
| 9 |
typedef __SIZE_TYPE__ size_t; |
| 10 |
|
| 11 |
/* Prevent equivalent functions from being merged. */ |
| 12 |
#define NOIPA __attribute__ ((noipa)) |
| 13 |
|
| 14 |
#ifndef LINE |
| 15 |
# define LINE 0 |
| 16 |
#endif |
| 17 |
|
| 18 |
#define bos(x) __builtin_object_size (x, 0) |
| 19 |
|
| 20 |
/* Defined (and redefined) to the allocation function to use, either |
| 21 |
malloc, or alloca, or a VLA. */ |
| 22 |
#define ALLOC(p, n) (p) = __builtin_malloc (n) |
| 23 |
|
| 24 |
/* Defined (and redefined) to the sprintf function to exercise. */ |
| 25 |
#define TEST_SPRINTF(d, maxsize, objsize, fmt, ...) \ |
| 26 |
__builtin___sprintf_chk (d, 0, objsize, fmt, __VA_ARGS__) |
| 27 |
|
| 28 |
#define T(bufsize, fmt, ...) \ |
| 29 |
do { \ |
| 30 |
if (!LINE || __LINE__ == LINE) \ |
| 31 |
{ \ |
| 32 |
char *d; \ |
| 33 |
ALLOC (d, bufsize); \ |
| 34 |
TEST_SPRINTF (d, 0, bos (d), fmt, __VA_ARGS__); \ |
| 35 |
sink (d); \ |
| 36 |
} \ |
| 37 |
} while (0) |
| 38 |
|
| 39 |
void sink (void*); |
| 40 |
|
| 41 |
/* Identity function to verify that the checker figures out the value |
| 42 |
of the operand even when it's not constant (i.e., makes use of |
| 43 |
inlining and constant propagation information). */ |
| 44 |
|
| 45 |
static int i (int x) { return x; } |
| 46 |
static const char* s (const char *str) { return str; } |
| 47 |
|
| 48 |
/* Function to "generate" a unique unknown number (as far as GCC can |
| 49 |
tell) each time it's called. It prevents the optimizer from being |
| 50 |
able to narrow down the ranges of possible values in test functions |
| 51 |
with repeated references to the same variable. */ |
| 52 |
extern int x (void); |
| 53 |
|
| 54 |
/* Verify that the checker can detect buffer overflow when the "%s" |
| 55 |
argument is in a known range of lengths and one or both of which |
| 56 |
exceed the size of the destination. */ |
| 57 |
|
| 58 |
NOIPA void test_sprintf_chk_string (const char *s, const char *t) |
| 59 |
{ |
| 60 |
#define x x () |
| 61 |
|
| 62 |
T (1, "%-s", x ? "" : "1"); /* { dg-warning "nul past the end" } */ |
| 63 |
T (1, "%-s", x ? "1" : ""); /* { dg-warning "nul past the end" } */ |
| 64 |
T (1, "%-s", x ? s : "1"); /* { dg-warning "nul past the end" } */ |
| 65 |
T (1, "%-s", x ? "1" : s); /* { dg-warning "nul past the end" } */ |
| 66 |
|
| 67 |
/* When neither string is known no warning should be issued at level 1 |
| 68 |
since their lenghts are assumed to be zero. */ |
| 69 |
T (1, "%s", x ? s : t); |
| 70 |
|
| 71 |
T (2, "%s", x ? "" : "1"); |
| 72 |
T (2, "%s", x ? "" : s); |
| 73 |
T (2, "%s", x ? "1" : ""); |
| 74 |
T (2, "%s", x ? s : ""); |
| 75 |
T (2, "%s", x ? "1" : "2"); |
| 76 |
T (2, "%s", x ? "" : "12"); /* { dg-warning "nul past the end" } */ |
| 77 |
T (2, "%s", x ? "12" : ""); /* { dg-warning "nul past the end" } */ |
| 78 |
|
| 79 |
T (2, "%s", x ? "" : "123"); /* { dg-warning "into a region" } */ |
| 80 |
T (2, "%s", x ? "123" : ""); /* { dg-warning "into a region" } */ |
| 81 |
|
| 82 |
#undef x |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
/* Verify that the checker makes use of integer constant propagation |
| 87 |
to detect buffer overflow in non-constant cases. */ |
| 88 |
|
| 89 |
NOIPA void test_sprintf_chk_integer_value (void) |
| 90 |
{ |
| 91 |
T ( 1, "%i", i ( 0)); /* { dg-warning "nul past the end" } */ |
| 92 |
T ( 1, "%i", i ( 1)); /* { dg-warning "nul past the end" } */ |
| 93 |
T ( 1, "%i", i ( -1)); /* { dg-warning "into a region" } */ |
| 94 |
T ( 1, "%i_", i ( 1)); /* { dg-warning " 1 byte into a region of size 0" } */ |
| 95 |
T ( 1, "_%i", i ( 1)); /* { dg-warning "into a region" } */ |
| 96 |
T ( 1, "_%i_",i ( 1)); /* { dg-warning "into a region" } */ |
| 97 |
T ( 1, "%o", i ( 0)); /* { dg-warning "nul past the end" } */ |
| 98 |
T ( 1, "%u", i ( 0)); /* { dg-warning "nul past the end" } */ |
| 99 |
T ( 1, "%x", i ( 0)); /* { dg-warning "nul past the end" } */ |
| 100 |
T ( 1, "%#x", i ( 0)); /* { dg-warning "nul past the end" } */ |
| 101 |
T ( 1, "%x", i ( 1)); /* { dg-warning "nul past the end" } */ |
| 102 |
T ( 1, "%#x", i ( 1)); /* { dg-warning "into a region" } */ |
| 103 |
|
| 104 |
T ( 2, "%i", i ( 0)); |
| 105 |
T ( 2, "%i", i ( 1)); |
| 106 |
T ( 2, "%i", i ( 9)); |
| 107 |
T ( 2, "%i", i ( -1)); /* { dg-warning "nul past the end" } */ |
| 108 |
T ( 2, "%i", i ( 10)); /* { dg-warning "nul past the end" } */ |
| 109 |
T ( 2, "%i_", i ( 0)); /* { dg-warning "nul past the end" } */ |
| 110 |
T ( 2, "_%i", i ( 0)); /* { dg-warning "nul past the end" } */ |
| 111 |
T ( 2, "_%i_",i ( 0)); /* { dg-warning " 1 byte into a region of size 0" } */ |
| 112 |
T ( 2, "%o", i ( 1)); |
| 113 |
T ( 2, "%o", i ( 7)); |
| 114 |
T ( 2, "%o", i ( 010)); /* { dg-warning "nul past the end" } */ |
| 115 |
T ( 2, "%o", i ( 0100)); /* { dg-warning "into a region" } */ |
| 116 |
T ( 2, "%x", i ( 1)); |
| 117 |
T ( 2, "%#x", i ( 1)); /* { dg-warning "into a region" } */ |
| 118 |
T ( 2, "%x", i ( 0xa)); |
| 119 |
T ( 2, "%x", i ( 0xf)); |
| 120 |
T ( 2, "%x", i ( 0x10)); /* { dg-warning "nul past the end" } */ |
| 121 |
T ( 2, "%x", i ( 0xff)); /* { dg-warning "nul past the end" } */ |
| 122 |
T ( 2, "%x", i (0x1ff)); /* { dg-warning "into a region" } */ |
| 123 |
|
| 124 |
T ( 3, "%i", i ( 0)); |
| 125 |
T ( 3, "%i", i ( 1)); |
| 126 |
T ( 3, "%i", i ( 9)); |
| 127 |
T ( 3, "%i", i ( -9)); |
| 128 |
T ( 3, "%i", i ( 10)); |
| 129 |
T ( 3, "%i", i ( 99)); |
| 130 |
T ( 3, "%i", i ( -99)); /* { dg-warning "nul past the end" } */ |
| 131 |
|
| 132 |
T ( 3, "%i", i (99) + i (1)); /* { dg-warning "nul past the end" } */ |
| 133 |
|
| 134 |
T ( 8, "%8u", i ( 1)); /* { dg-warning "nul past the end" } */ |
| 135 |
T ( 9, "%8u", i ( 1)); |
| 136 |
} |
| 137 |
|
| 138 |
extern int rand (void); |
| 139 |
|
| 140 |
/* Functions to require optimization to figure out the range of the operand. |
| 141 |
Used to verify that the checker makes use of the range information to |
| 142 |
avoid diagnosing the output of sufficiently constrained arguments to |
| 143 |
integer directives. */ |
| 144 |
|
| 145 |
static signed char |
| 146 |
range_schar (signed char min, signed char max) |
| 147 |
{ |
| 148 |
signed char val = rand (); |
| 149 |
return val < min || max < val ? min : val; |
| 150 |
} |
| 151 |
|
| 152 |
static unsigned char |
| 153 |
range_uchar (unsigned char min, unsigned char max) |
| 154 |
{ |
| 155 |
unsigned char val = rand (); |
| 156 |
return val < min || max < val ? min : val; |
| 157 |
} |
| 158 |
|
| 159 |
static signed short |
| 160 |
range_sshrt (signed short min, signed short max) |
| 161 |
{ |
| 162 |
signed short val = rand (); |
| 163 |
return val < min || max < val ? min : val; |
| 164 |
} |
| 165 |
|
| 166 |
static unsigned short |
| 167 |
range_ushrt (unsigned short min, unsigned short max) |
| 168 |
{ |
| 169 |
unsigned short val = rand (); |
| 170 |
return val < min || max < val ? min : val; |
| 171 |
} |
| 172 |
|
| 173 |
static signed int |
| 174 |
range_sint (signed int min, signed int max) |
| 175 |
{ |
| 176 |
signed int val = rand (); |
| 177 |
return val < min || max < val ? min : val; |
| 178 |
} |
| 179 |
|
| 180 |
static unsigned int |
| 181 |
range_uint (unsigned int min, unsigned int max) |
| 182 |
{ |
| 183 |
unsigned int val = rand (); |
| 184 |
return val < min || max < val ? min : val; |
| 185 |
} |
| 186 |
|
| 187 |
NOIPA void test_sprintf_chk_range_schar (void) |
| 188 |
{ |
| 189 |
#define R(min, max) range_sint (min, max) |
| 190 |
|
| 191 |
T ( 0, "%hhi", R (0, 1)); /* { dg-warning ".%hhi. directive writing 1 byte into a region of size 0" } */ |
| 192 |
/* { dg-message "directive argument in the range \\\[0, 1\\\]" "note" { target *-*-* } .-1 } */ |
| 193 |
|
| 194 |
T ( 0, "%hhi", R (0, 127)); /* { dg-warning ".%hhi. directive writing between 1 and 3 bytes into a region of size 0" } */ |
| 195 |
/* { dg-message "directive argument in the range \\\[0, 127\\\]" "note" { target *-*-* } .-1 } */ |
| 196 |
|
| 197 |
T ( 0, "%hhi", R (1024, 1033)); /* { dg-warning ".%hhi. directive writing 1 byte into a region of size 0" } */ |
| 198 |
/* { dg-message "directive argument in the range \\\[1024, 1033\\\]" "note" { target *-*-* } .-1 } */ |
| 199 |
|
| 200 |
T ( 0, "%hhi", R (1024, 1034)); /* { dg-warning ".%hhi. directive writing between 1 and 2 bytes into a region of size 0" } */ |
| 201 |
/* { dg-message "directive argument in the range \\\[1024, 1034\\\]" "note" { target *-*-* } .-1 } */ |
| 202 |
|
| 203 |
T ( 0, "%hhi", R (1024, 2035)); /* { dg-warning ".%hhi. directive writing between 1 and 4 bytes into a region of size 0" } */ |
| 204 |
/* { dg-message "using the range \\\[-128, 127\\\] for directive argument" "note" { target *-*-* } .-1 } */ |
| 205 |
|
| 206 |
T ( 2, "%#hhx", R (1234, 12345)); /* { dg-warning "'%#hhx' directive writing between 1 and 4 bytes into a region of size 2 " } */ |
| 207 |
T ( 3, "%#hhx", R (1234, 12345)); /* { dg-warning "may write a terminating nul" } */ |
| 208 |
T ( 4, "%#hhx", R (1234, 12345)); |
| 209 |
|
| 210 |
#undef R |
| 211 |
#define R(min, max) range_schar (min, max) |
| 212 |
|
| 213 |
T ( 0, "%i", R (0, 9)); /* { dg-warning ".%i. directive writing 1 byte into a region of size 0" } */ |
| 214 |
T ( 1, "%i", R (0, 9)); /* { dg-warning "nul past the end" } */ |
| 215 |
T ( 2, "%i", R (0, 9)); |
| 216 |
T ( 2, "%i", R (-1, 0)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 217 |
T ( 2, "%i", R (9, 10)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 218 |
|
| 219 |
T ( 3, "%i", R ( -9, 9)); |
| 220 |
T ( 3, "%i", R (-99, 99)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 221 |
T ( 3, "%i", R ( 0, 99)); |
| 222 |
T ( 3, "%i", R ( 0, 100)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 223 |
|
| 224 |
/* The following call may write as few as 2 bytes and as many as 4. |
| 225 |
It's a judgment call how best to diagnose it to make the potential |
| 226 |
problem clear. */ |
| 227 |
T ( 3, "%i%i", R (1, 10), R (9, 10)); /* { dg-warning "directive writing between 1 and 2 bytes into a region of size between 1 and 2" } */ |
| 228 |
|
| 229 |
T ( 4, "%i%i", R (10, 11), R (12, 13)); /* { dg-warning "nul past the end" } */ |
| 230 |
|
| 231 |
T ( 5, "%i%i", R (-9, 99), R (-9, 99)); |
| 232 |
|
| 233 |
T ( 6, "%i_%i_%i", R (0, 9), R (0, 9), R (0, 9)); |
| 234 |
T ( 6, "%i_%i_%i", R (0, 9), R (0, 9), R (0, 10)); /* { dg-warning "may write a terminating nul past the end" } */ |
| 235 |
T ( 6, "%i_%i_%i", R (0, 9), R (0, 10), R (0, 9)); /* { dg-warning "may write a terminating nul past the end" } */ |
| 236 |
T ( 6, "%i_%i_%i", R (0, 10), R (0, 9), R (0, 9)); /* { dg-warning "may write a terminating nul past the end" } */ |
| 237 |
T ( 6, "%hhi_%hi_%i", R (0, 9), R (0, 10), R (0, 10)); /* { dg-warning ".i. directive writing between 1 and 2 bytes into a region of size between 1 and 2" } */ |
| 238 |
T ( 6, "%3i|%2i/%1i", R (0, 99), R (0, 99), R (0, 99)); /* { dg-warning "./. directive writing 1 byte into a region of size 0" } */ |
| 239 |
T ( 6, "%.3i|%.2i/%i", R (0, 99), R (0, 99), R (0, 99)); /* { dg-warning "./. directive writing 1 byte into a region of size 0" } */ |
| 240 |
T ( 6, "%.3i|%.2i/%i", R (0, 119), R (0, 99), R (0, 99)); /* { dg-warning "./. directive writing 1 byte into a region of size 0" } */ |
| 241 |
T ( 6, "%.3i|%.2i/%i", R (0, 1), R (0, 2), R (0, 3)); /* { dg-warning "./. directive writing 1 byte into a region of size 0" } */ |
| 242 |
} |
| 243 |
|
| 244 |
NOIPA void test_sprintf_chk_range_uchar (void) |
| 245 |
{ |
| 246 |
#undef R |
| 247 |
#define R(min, max) range_uchar (min, max) |
| 248 |
|
| 249 |
T ( 0, "%i", R (0, 9)); /* { dg-warning ".%i. directive writing 1 byte into a region of size 0" } */ |
| 250 |
T ( 1, "%i", R (0, 9)); /* { dg-warning "nul past the end" } */ |
| 251 |
T ( 2, "%i", R (0, 9)); |
| 252 |
T ( 2, "%i", R (9, 10)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 253 |
|
| 254 |
T ( 3, "%i", R (0, 99)); |
| 255 |
T ( 3, "%i", R (0, 100)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 256 |
} |
| 257 |
|
| 258 |
NOIPA void test_sprintf_chk_range_sshrt (void) |
| 259 |
{ |
| 260 |
#undef R |
| 261 |
#define R(min, max) range_sshrt (min, max) |
| 262 |
|
| 263 |
T ( 0, "%i", R ( 0, 9)); /* { dg-warning ".%i. directive writing 1 byte into a region of size 0" } */ |
| 264 |
T ( 1, "%i", R ( 0, 1)); /* { dg-warning "nul past the end" } */ |
| 265 |
T ( 1, "%i", R ( 0, 9)); /* { dg-warning "nul past the end" } */ |
| 266 |
T ( 2, "%i", R ( 0, 1)); |
| 267 |
T ( 2, "%i", R ( 8, 9)); |
| 268 |
T ( 2, "%i", R ( 0, 9)); |
| 269 |
T ( 2, "%i", R (-1, 0)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 270 |
T ( 2, "%i", R ( 9, 10)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 271 |
|
| 272 |
T ( 3, "%i", R ( 0, 99)); |
| 273 |
T ( 3, "%i", R (99, 999)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 274 |
|
| 275 |
T ( 4, "%i", R ( 0, 999)); |
| 276 |
T ( 4, "%i", R ( 99, 999)); |
| 277 |
T ( 4, "%i", R (998, 999)); |
| 278 |
T ( 4, "%i", R (999, 1000)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 279 |
} |
| 280 |
|
| 281 |
NOIPA void test_sprintf_chk_range_ushrt (void) |
| 282 |
{ |
| 283 |
#undef R |
| 284 |
#define R(min, max) range_ushrt (min, max) |
| 285 |
|
| 286 |
T ( 0, "%i", R ( 0, 9)); /* { dg-warning ".%i. directive writing 1 byte into a region of size 0" } */ |
| 287 |
T ( 1, "%i", R ( 0, 1)); /* { dg-warning "nul past the end" } */ |
| 288 |
T ( 1, "%i", R ( 0, 9)); /* { dg-warning "nul past the end" } */ |
| 289 |
T ( 2, "%i", R ( 0, 1)); |
| 290 |
T ( 2, "%i", R ( 8, 9)); |
| 291 |
T ( 2, "%i", R ( 0, 9)); |
| 292 |
T ( 2, "%i", R ( 9, 10)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 293 |
|
| 294 |
T ( 3, "%i", R ( 0, 99)); |
| 295 |
T ( 3, "%i", R (99, 999)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 296 |
|
| 297 |
T ( 4, "%i", R ( 0, 999)); |
| 298 |
T ( 4, "%i", R ( 99, 999)); |
| 299 |
T ( 4, "%i", R (998, 999)); |
| 300 |
T ( 4, "%i", R (999, 1000)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 301 |
} |
| 302 |
|
| 303 |
NOIPA void test_sprintf_chk_range_sint (void) |
| 304 |
{ |
| 305 |
#undef R |
| 306 |
#define R(min, max) range_sint (min, max) |
| 307 |
|
| 308 |
T ( 0, "%i", R ( 0, 9)); /* { dg-warning ".%i. directive writing 1 byte into a region of size 0" } */ |
| 309 |
T ( 1, "%i", R ( 0, 1)); /* { dg-warning "nul past the end" } */ |
| 310 |
T ( 1, "%i", R ( 0, 9)); /* { dg-warning "nul past the end" } */ |
| 311 |
T ( 2, "%i", R ( 0, 1)); |
| 312 |
T ( 2, "%i", R ( 8, 9)); |
| 313 |
T ( 2, "%i", R ( 0, 9)); |
| 314 |
T ( 2, "%i", R (-1, 0)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 315 |
T ( 2, "%i", R ( 9, 10)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 316 |
|
| 317 |
T ( 3, "%i", R ( 0, 99)); |
| 318 |
T ( 3, "%i", R (99, 999)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 319 |
|
| 320 |
T ( 4, "%i", R ( 0, 999)); |
| 321 |
T ( 4, "%i", R ( 99, 999)); |
| 322 |
T ( 4, "%i", R (998, 999)); |
| 323 |
T ( 4, "%i", R (999, 1000)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 324 |
} |
| 325 |
|
| 326 |
NOIPA void test_sprintf_chk_range_uint (void) |
| 327 |
{ |
| 328 |
#undef R |
| 329 |
#define R(min, max) range_uint (min, max) |
| 330 |
|
| 331 |
T ( 0, "%i", R ( 0, 9)); /* { dg-warning ".%i. directive writing 1 byte into a region of size 0" } */ |
| 332 |
T ( 1, "%i", R ( 0, 1)); /* { dg-warning "nul past the end" } */ |
| 333 |
T ( 1, "%i", R ( 0, 9)); /* { dg-warning "nul past the end" } */ |
| 334 |
T ( 2, "%i", R ( 0, 1)); |
| 335 |
T ( 2, "%i", R ( 8, 9)); |
| 336 |
T ( 2, "%i", R ( 0, 9)); |
| 337 |
T ( 2, "%i", R ( 9, 10)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 338 |
|
| 339 |
T ( 3, "%i", R ( 0, 99)); |
| 340 |
T ( 3, "%i", R (99, 999)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 341 |
|
| 342 |
T ( 4, "%i", R ( 0, 999)); |
| 343 |
T ( 4, "%i", R ( 99, 999)); |
| 344 |
T ( 4, "%i", R (998, 999)); |
| 345 |
T ( 4, "%i", R (999, 1000)); /* { dg-warning "may write a terminating nul past the end of the destination" } */ |
| 346 |
} |
| 347 |
|
| 348 |
/* Verify that destination size in excess of INT_MAX (and, separately, |
| 349 |
in excess of the largest object) is diagnosed. The former because |
| 350 |
the functions are defined only for output of at most INT_MAX and |
| 351 |
specifying a large upper bound defeats the bounds checking (and, |
| 352 |
on some implementations such as Solaris, causes the function to |
| 353 |
fail. The latter because due to the limit of ptrdiff_t no object |
| 354 |
can be larger than PTRDIFF_MAX bytes. */ |
| 355 |
|
| 356 |
NOIPA void test_too_large (char *d, int x, __builtin_va_list va) |
| 357 |
{ |
| 358 |
const size_t imax = __INT_MAX__; |
| 359 |
const size_t imax_p1 = imax + 1; |
| 360 |
|
| 361 |
__builtin_snprintf (d, imax, "%c", x); |
| 362 |
__builtin_snprintf (d, imax_p1, "%c", x); /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "INT_MAX + 1" { target { lp64 || msp430_large } } } */ |
| 363 |
/* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" "INT_MAX + 1" { target { { avr-*-* } || { { ilp32 } || { int16 && { ! msp430_large } } } } } .-1 } */ |
| 364 |
|
| 365 |
__builtin_vsnprintf (d, imax, "%c", va); |
| 366 |
__builtin_vsnprintf (d, imax_p1, "%c", va); /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "INT_MAX + 1" { target { lp64 || msp430_large } } } */ |
| 367 |
/* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" "INT_MAX + 1" { target { { avr-*-* } || { { ilp32 } || { int16 && { ! msp430_large } } } } } .-1 } */ |
| 368 |
|
| 369 |
__builtin___snprintf_chk (d, imax, 0, imax, "%c", x); |
| 370 |
__builtin___snprintf_chk (d, imax_p1, 0, imax_p1, "%c", x); /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "INT_MAX + 1" { target { lp64 || msp430_large } } } */ |
| 371 |
/* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" "INT_MAX + 1" { target { { avr-*-* } || { { ilp32 } || { int16 && { ! msp430_large } } } } } .-1 } */ |
| 372 |
|
| 373 |
__builtin___vsnprintf_chk (d, imax, 0, imax, "%c", va); |
| 374 |
__builtin___vsnprintf_chk (d, imax_p1, 0, imax_p1, "%c", va); /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "INT_MAX + 1" { target { lp64 || msp430_large } } } */ |
| 375 |
/* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" "INT_MAX + 1" { target { { avr-*-* } || { { ilp32 } || { int16 && { ! msp430_large } } } } } .-1 } */ |
| 376 |
|
| 377 |
const size_t ptrmax = __PTRDIFF_MAX__; |
| 378 |
const size_t ptrmax_m1 = ptrmax - 1; |
| 379 |
|
| 380 |
__builtin_snprintf (d, ptrmax_m1, "%c", x); /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "PTRDIFF_MAX - 1" { target { lp64 || msp430_large } } } */ |
| 381 |
__builtin_snprintf (d, ptrmax, " %c", x); /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "PTRDIFF_MAX" { target { lp64 || msp430_large } } } */ |
| 382 |
|
| 383 |
__builtin_vsnprintf (d, ptrmax_m1, "%c", va); /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "PTRDIFF_MAX - 1" { target { lp64 || msp430_large } } } */ |
| 384 |
__builtin_vsnprintf (d, ptrmax, "%c", va); /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "PTRDIFF_MAX" { target { lp64 || msp430_large } } } */ |
| 385 |
|
| 386 |
__builtin___snprintf_chk (d, ptrmax_m1, 0, ptrmax_m1, "%c", x); /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "PTRDIFF_MAX - 1" { target { lp64 || msp430_large } } } */ |
| 387 |
__builtin___snprintf_chk (d, ptrmax, 0, ptrmax, "%c", x); /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "PTRDIFF_MAX" { target { lp64 || msp430_large } } } */ |
| 388 |
|
| 389 |
__builtin___vsnprintf_chk (d, ptrmax_m1, 0, ptrmax_m1, "%c", va); /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "PTRDIFF_MAX - 1" { target { lp64 || msp430_large } } } */ |
| 390 |
__builtin___vsnprintf_chk (d, ptrmax, 0, ptrmax, "%c", va); /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "PTRDIFF_MAX" { target { lp64 || msp430_large } } } */ |
| 391 |
} |
| 392 |
|
| 393 |
/* Exercise ordinary sprintf with malloc. */ |
| 394 |
#undef TEST_SPRINTF |
| 395 |
#define TEST_SPRINTF(d, maxsize, objsize, fmt, ...) \ |
| 396 |
__builtin_sprintf (d, fmt, __VA_ARGS__) |
| 397 |
|
| 398 |
NOIPA void test_sprintf_malloc (const char *s, const char *t) |
| 399 |
{ |
| 400 |
#define x x () |
| 401 |
|
| 402 |
T (1, "%-s", x ? "" : "1"); /* { dg-warning "nul past the end" } */ |
| 403 |
T (1, "%-s", x ? "1" : ""); /* { dg-warning "nul past the end" } */ |
| 404 |
T (1, "%-s", x ? s : "1"); /* { dg-warning "nul past the end" } */ |
| 405 |
T (1, "%-s", x ? "1" : s); /* { dg-warning "nul past the end" } */ |
| 406 |
T (1, "%-s", x ? s : t); |
| 407 |
|
| 408 |
T (2, "%-s", x ? "" : "1"); |
| 409 |
T (2, "%-s", x ? "" : s); |
| 410 |
T (2, "%-s", x ? "1" : ""); |
| 411 |
T (2, "%-s", x ? s : ""); |
| 412 |
T (2, "%-s", x ? "1" : "2"); |
| 413 |
T (2, "%-s", x ? "" : "12"); /* { dg-warning "nul past the end" } */ |
| 414 |
T (2, "%-s", x ? "12" : ""); /* { dg-warning "nul past the end" } */ |
| 415 |
|
| 416 |
T (2, "%-s", x ? "" : "123"); /* { dg-warning "into a region" } */ |
| 417 |
T (2, "%-s", x ? "123" : ""); /* { dg-warning "into a region" } */ |
| 418 |
|
| 419 |
#undef x |
| 420 |
} |
| 421 |
|
| 422 |
/* Exercise ordinary sprintf with alloca. */ |
| 423 |
#undef ALLOC |
| 424 |
#define ALLOC(p, n) (p) = __builtin_alloca (n) |
| 425 |
|
| 426 |
NOIPA void test_sprintf_alloca (const char *s, const char *t) |
| 427 |
{ |
| 428 |
#define x x () |
| 429 |
|
| 430 |
T (1, "%-s", x ? "" : "1"); /* { dg-warning "nul past the end" } */ |
| 431 |
T (1, "%-s", x ? "1" : ""); /* { dg-warning "nul past the end" } */ |
| 432 |
T (1, "%-s", x ? s : "1"); /* { dg-warning "nul past the end" } */ |
| 433 |
T (1, "%-s", x ? "1" : s); /* { dg-warning "nul past the end" } */ |
| 434 |
T (1, "%-s", x ? s : t); |
| 435 |
|
| 436 |
T (2, "%-s", x ? "" : "1"); |
| 437 |
T (2, "%-s", x ? "" : s); |
| 438 |
T (2, "%-s", x ? "1" : ""); |
| 439 |
T (2, "%-s", x ? s : ""); |
| 440 |
T (2, "%-s", x ? "1" : "2"); |
| 441 |
T (2, "%-s", x ? "" : "12"); /* { dg-warning "nul past the end" } */ |
| 442 |
T (2, "%-s", x ? "12" : ""); /* { dg-warning "nul past the end" } */ |
| 443 |
|
| 444 |
T (2, "%-s", x ? "" : "123"); /* { dg-warning "into a region" } */ |
| 445 |
T (2, "%-s", x ? "123" : ""); /* { dg-warning "into a region" } */ |
| 446 |
|
| 447 |
#undef x |
| 448 |
} |
| 449 |
|
| 450 |
/* Exercise ordinary sprintf with a VLA. */ |
| 451 |
#undef ALLOC |
| 452 |
#define ALLOC(p, n) char vla [i (n)]; (p) = vla |
| 453 |
|
| 454 |
NOIPA void test_sprintf_vla (const char *s, const char *t) |
| 455 |
{ |
| 456 |
#define x x () |
| 457 |
|
| 458 |
T (1, "%-s", x ? "" : "1"); /* { dg-warning "nul past the end" } */ |
| 459 |
T (1, "%-s", x ? "1" : ""); /* { dg-warning "nul past the end" } */ |
| 460 |
T (1, "%-s", x ? s : "1"); /* { dg-warning "nul past the end" } */ |
| 461 |
T (1, "%-s", x ? "1" : s); /* { dg-warning "nul past the end" } */ |
| 462 |
T (1, "%-s", x ? s : t); |
| 463 |
|
| 464 |
T (2, "%-s", x ? "" : "1"); |
| 465 |
T (2, "%-s", x ? "" : s); |
| 466 |
T (2, "%-s", x ? "1" : ""); |
| 467 |
T (2, "%-s", x ? s : ""); |
| 468 |
T (2, "%-s", x ? "1" : "2"); |
| 469 |
T (2, "%-s", x ? "" : "12"); /* { dg-warning "nul past the end" } */ |
| 470 |
T (2, "%-s", x ? "12" : ""); /* { dg-warning "nul past the end" } */ |
| 471 |
|
| 472 |
T (2, "%-s", x ? "" : "123"); /* { dg-warning "into a region" } */ |
| 473 |
T (2, "%-s", x ? "123" : ""); /* { dg-warning "into a region" } */ |
| 474 |
|
| 475 |
#undef x |
| 476 |
} |