aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-3.c
blob: a4229668b44b6b6053e62b1e1afa1f5a828847eb (plain)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* Verify that all sprintf built-ins detect overflow involving directives
   with non-constant arguments known to be constrained by some range of
   values, and even when writing into dynamically allocated buffers.
   -O2 (-ftree-vrp) is necessary for the tests involving ranges to pass,
   otherwise -O1 is sufficient.
   { dg-do compile }
   { dg-options "-O2 -Wformat -Wformat-overflow=1 -ftrack-macro-expansion=0" } */

typedef __SIZE_TYPE__ size_t;

/* Prevent equivalent functions from being merged.  */
#define NOIPA __attribute__ ((noipa))

#ifndef LINE
#  define LINE 0
#endif

#define bos(x) __builtin_object_size (x, 0)

/* Defined (and redefined) to the allocation function to use, either
   malloc, or alloca, or a VLA.  */
#define ALLOC(p, n)   (p) = __builtin_malloc (n)

/* Defined (and redefined) to the sprintf function to exercise.  */
#define TEST_SPRINTF(d, maxsize, objsize, fmt, ...)		\
  __builtin___sprintf_chk (d, 0, objsize, fmt, __VA_ARGS__)

#define T(bufsize, fmt, ...)				\
  do {							\
    if (!LINE || __LINE__ == LINE)			\
      {							\
	char *d;					\
	ALLOC (d, bufsize);				\
	TEST_SPRINTF (d, 0, bos (d), fmt, __VA_ARGS__);	\
	sink (d);					\
      }							\
  } while (0)

void sink (void*);

/* Identity function to verify that the checker figures out the value
   of the operand even when it's not constant (i.e., makes use of
   inlining and constant propagation information).  */

static int i (int x) { return x; }
static const char* s (const char *str) { return str; }

/* Function to "generate" a unique unknown number (as far as GCC can
   tell) each time it's called.  It prevents the optimizer from being
   able to narrow down the ranges of possible values in test functions
   with repeated references to the same variable.  */
extern int x (void);

/* Verify that the checker can detect buffer overflow when the "%s"
   argument is in a known range of lengths and one or both of which
   exceed the size of the destination.  */

NOIPA void test_sprintf_chk_string (const char *s, const char *t)
{
#define x x ()

  T (1, "%-s", x ? "" : "1");       /* { dg-warning "nul past the end" } */
  T (1, "%-s", x ? "1" : "");       /* { dg-warning "nul past the end" } */
  T (1, "%-s", x ? s : "1");        /* { dg-warning "nul past the end" } */
  T (1, "%-s", x ? "1" : s);        /* { dg-warning "nul past the end" } */

  /* When neither string is known no warning should be issued at level 1
     since their lenghts are assumed to be zero.  */
  T (1, "%s", x ? s : t);

  T (2, "%s", x ? "" : "1");
  T (2, "%s", x ? "" : s);
  T (2, "%s", x ? "1" : "");
  T (2, "%s", x ? s : "");
  T (2, "%s", x ? "1" : "2");
  T (2, "%s", x ? "" : "12");      /* { dg-warning "nul past the end" } */
  T (2, "%s", x ? "12" : "");      /* { dg-warning "nul past the end" } */

  T (2, "%s", x ? "" : "123");     /* { dg-warning "into a region" } */
  T (2, "%s", x ? "123" : "");     /* { dg-warning "into a region" } */

#undef x
}


/* Verify that the checker makes use of integer constant propagation
   to detect buffer overflow in non-constant cases.  */

NOIPA void test_sprintf_chk_integer_value (void)
{
  T ( 1, "%i",  i (    0));         /* { dg-warning "nul past the end" } */
  T ( 1, "%i",  i (    1));         /* { dg-warning "nul past the end" } */
  T ( 1, "%i",  i (   -1));         /* { dg-warning "into a region" } */
  T ( 1, "%i_", i (    1));         /* { dg-warning " 1 byte into a region of size 0" } */
  T ( 1, "_%i", i (    1));         /* { dg-warning "into a region" } */
  T ( 1, "_%i_",i (    1));         /* { dg-warning "into a region" } */
  T ( 1, "%o",  i (    0));         /* { dg-warning "nul past the end" } */
  T ( 1, "%u",  i (    0));         /* { dg-warning "nul past the end" } */
  T ( 1, "%x",  i (    0));         /* { dg-warning "nul past the end" } */
  T ( 1, "%#x", i (    0));         /* { dg-warning "nul past the end" } */
  T ( 1, "%x",  i (    1));         /* { dg-warning "nul past the end" } */
  T ( 1, "%#x", i (    1));         /* { dg-warning "into a region" } */

  T ( 2, "%i",  i (    0));
  T ( 2, "%i",  i (    1));
  T ( 2, "%i",  i (    9));
  T ( 2, "%i",  i (   -1));         /* { dg-warning "nul past the end" } */
  T ( 2, "%i",  i (   10));         /* { dg-warning "nul past the end" } */
  T ( 2, "%i_", i (    0));         /* { dg-warning "nul past the end" } */
  T ( 2, "_%i", i (    0));         /* { dg-warning "nul past the end" } */
  T ( 2, "_%i_",i (    0));         /* { dg-warning " 1 byte into a region of size 0" } */
  T ( 2, "%o",  i (    1));
  T ( 2, "%o",  i (    7));
  T ( 2, "%o",  i (  010));         /* { dg-warning "nul past the end" } */
  T ( 2, "%o",  i ( 0100));         /* { dg-warning "into a region" } */
  T ( 2, "%x",  i (    1));
  T ( 2, "%#x", i (    1));         /* { dg-warning "into a region" } */
  T ( 2, "%x",  i (  0xa));
  T ( 2, "%x",  i (  0xf));
  T ( 2, "%x",  i ( 0x10));         /* { dg-warning "nul past the end" } */
  T ( 2, "%x",  i ( 0xff));         /* { dg-warning "nul past the end" } */
  T ( 2, "%x",  i (0x1ff));         /* { dg-warning "into a region" } */

  T ( 3, "%i",  i (    0));
  T ( 3, "%i",  i (    1));
  T ( 3, "%i",  i (    9));
  T ( 3, "%i",  i (   -9));
  T ( 3, "%i",  i (   10));
  T ( 3, "%i",  i (   99));
  T ( 3, "%i",  i (  -99));         /* { dg-warning "nul past the end" } */

  T ( 3, "%i",  i (99) + i (1));    /* { dg-warning "nul past the end" } */

  T ( 8, "%8u", i (    1));         /* { dg-warning "nul past the end" } */
  T ( 9, "%8u", i (    1));
}

extern int rand (void);

/* Functions to require optimization to figure out the range of the operand.
   Used to verify that the checker makes use of the range information to
   avoid diagnosing the output of sufficiently constrained arguments to
   integer directives.  */

static signed char
range_schar (signed char min, signed char max)
{
  signed char val = rand ();
  return val < min || max < val ? min : val;
}

static unsigned char
range_uchar (unsigned char min, unsigned char max)
{
  unsigned char val = rand ();
  return val < min || max < val ? min : val;
}

static signed short
range_sshrt (signed short min, signed short max)
{
  signed short val = rand ();
  return val < min || max < val ? min : val;
}

static unsigned short
range_ushrt (unsigned short min, unsigned short max)
{
  unsigned short val = rand ();
  return val < min || max < val ? min : val;
}

static signed int
range_sint (signed int min, signed int max)
{
  signed int val = rand ();
  return val < min || max < val ? min : val;
}

static unsigned int
range_uint (unsigned int min, unsigned int max)
{
  unsigned int val = rand ();
  return val < min || max < val ? min : val;
}

NOIPA void test_sprintf_chk_range_schar (void)
{
#define R(min, max) range_sint (min, max)

  T ( 0, "%hhi", R (0, 1));     /* { dg-warning ".%hhi. directive writing 1 byte into a region of size 0" } */
  /* { dg-message "directive argument in the range \\\[0, 1\\\]" "note" { target *-*-* } .-1 } */

  T ( 0, "%hhi", R (0, 127));   /* { dg-warning ".%hhi. directive writing between 1 and 3 bytes into a region of size 0" } */
  /* { dg-message "directive argument in the range \\\[0, 127\\\]" "note" { target *-*-* } .-1 } */

  T ( 0, "%hhi", R (1024, 1033));   /* { dg-warning ".%hhi. directive writing 1 byte into a region of size 0" } */
  /* { dg-message "directive argument in the range \\\[1024, 1033\\\]" "note" { target *-*-* } .-1 } */

  T ( 0, "%hhi", R (1024, 1034));   /* { dg-warning ".%hhi. directive writing between 1 and 2 bytes into a region of size 0" } */
  /* { dg-message "directive argument in the range \\\[1024, 1034\\\]" "note" { target *-*-* } .-1 } */

  T ( 0, "%hhi", R (1024, 2035));   /* { dg-warning ".%hhi. directive writing between 1 and 4 bytes into a region of size 0" } */
  /* { dg-message "using the range \\\[-128, 127\\\] for directive argument" "note" { target *-*-* } .-1 } */

  T ( 2, "%#hhx", R (1234, 12345));  /* { dg-warning "'%#hhx' directive writing between 1 and 4 bytes into a region of size 2 " } */
  T ( 3, "%#hhx", R (1234, 12345));  /* { dg-warning "may write a terminating nul" } */
  T ( 4, "%#hhx", R (1234, 12345));

#undef R
#define R(min, max) range_schar (min, max)

  T ( 0, "%i",  R (0, 9));      /* { dg-warning ".%i. directive writing 1 byte into a region of size 0" } */
  T ( 1, "%i",  R (0, 9));      /* { dg-warning "nul past the end" } */
  T ( 2, "%i",  R (0, 9));
  T ( 2, "%i",  R (-1, 0));     /* { dg-warning "may write a terminating nul past the end of the destination" } */
  T ( 2, "%i",  R (9, 10));     /* { dg-warning "may write a terminating nul past the end of the destination" } */

  T ( 3, "%i",  R ( -9,   9));
  T ( 3, "%i",  R (-99,  99));  /* { dg-warning "may write a terminating nul past the end of the destination" } */
  T ( 3, "%i",  R (  0,  99));
  T ( 3, "%i",  R (  0, 100));  /* { dg-warning "may write a terminating nul past the end of the destination" } */

  /* The following call may write as few as 2 bytes and as many as 4.
     It's a judgment call how best to diagnose it to make the potential
     problem clear.  */
  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" } */

  T ( 4, "%i%i", R (10, 11), R (12, 13));   /* { dg-warning "nul past the end" } */

  T ( 5, "%i%i", R (-9, 99), R (-9, 99));

  T ( 6, "%i_%i_%i", R (0, 9), R (0, 9), R (0,  9));
  T ( 6, "%i_%i_%i", R (0, 9), R (0, 9), R (0, 10));  /* { dg-warning "may write a terminating nul past the end" } */
  T ( 6, "%i_%i_%i", R (0, 9), R (0, 10), R (0, 9));  /* { dg-warning "may write a terminating nul past the end" } */
  T ( 6, "%i_%i_%i", R (0, 10), R (0, 9), R (0, 9));  /* { dg-warning "may write a terminating nul past the end" } */
  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" } */
  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" } */
  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" } */
  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" } */
  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" } */
}

NOIPA void test_sprintf_chk_range_uchar (void)
{
#undef R
#define R(min, max) range_uchar (min, max)

  T ( 0, "%i",  R (0,  9));   /* { dg-warning ".%i. directive writing 1 byte into a region of size 0" } */
  T ( 1, "%i",  R (0,  9));   /* { dg-warning "nul past the end" } */
  T ( 2, "%i",  R (0,  9));
  T ( 2, "%i",  R (9, 10));   /* { dg-warning "may write a terminating nul past the end of the destination" } */

  T ( 3, "%i",  R (0,  99));
  T ( 3, "%i",  R (0, 100));  /* { dg-warning "may write a terminating nul past the end of the destination" } */
}

NOIPA void test_sprintf_chk_range_sshrt (void)
{
#undef R
#define R(min, max) range_sshrt (min, max)

  T ( 0, "%i",  R ( 0, 9));     /* { dg-warning ".%i. directive writing 1 byte into a region of size 0" } */
  T ( 1, "%i",  R ( 0, 1));     /* { dg-warning "nul past the end" } */
  T ( 1, "%i",  R ( 0, 9));     /* { dg-warning "nul past the end" } */
  T ( 2, "%i",  R ( 0, 1));
  T ( 2, "%i",  R ( 8, 9));
  T ( 2, "%i",  R ( 0, 9));
  T ( 2, "%i",  R (-1, 0));     /* { dg-warning "may write a terminating nul past the end of the destination" } */
  T ( 2, "%i",  R ( 9, 10));    /* { dg-warning "may write a terminating nul past the end of the destination" } */

  T ( 3, "%i",  R ( 0, 99));
  T ( 3, "%i",  R (99, 999));   /* { dg-warning "may write a terminating nul past the end of the destination" } */

  T ( 4, "%i",  R (  0,  999));
  T ( 4, "%i",  R ( 99,  999));
  T ( 4, "%i",  R (998,  999));
  T ( 4, "%i",  R (999, 1000)); /* { dg-warning "may write a terminating nul past the end of the destination" } */
}

NOIPA void test_sprintf_chk_range_ushrt (void)
{
#undef R
#define R(min, max) range_ushrt (min, max)

  T ( 0, "%i",  R ( 0, 9));     /* { dg-warning ".%i. directive writing 1 byte into a region of size 0" } */
  T ( 1, "%i",  R ( 0, 1));     /* { dg-warning "nul past the end" } */
  T ( 1, "%i",  R ( 0, 9));     /* { dg-warning "nul past the end" } */
  T ( 2, "%i",  R ( 0, 1));
  T ( 2, "%i",  R ( 8, 9));
  T ( 2, "%i",  R ( 0, 9));
  T ( 2, "%i",  R ( 9, 10));    /* { dg-warning "may write a terminating nul past the end of the destination" } */

  T ( 3, "%i",  R ( 0, 99));
  T ( 3, "%i",  R (99, 999));   /* { dg-warning "may write a terminating nul past the end of the destination" } */

  T ( 4, "%i",  R (  0,  999));
  T ( 4, "%i",  R ( 99,  999));
  T ( 4, "%i",  R (998,  999));
  T ( 4, "%i",  R (999, 1000)); /* { dg-warning "may write a terminating nul past the end of the destination" } */
}

NOIPA void test_sprintf_chk_range_sint (void)
{
#undef R
#define R(min, max) range_sint (min, max)

  T ( 0, "%i",  R ( 0, 9));     /* { dg-warning ".%i. directive writing 1 byte into a region of size 0" } */
  T ( 1, "%i",  R ( 0, 1));     /* { dg-warning "nul past the end" } */
  T ( 1, "%i",  R ( 0, 9));     /* { dg-warning "nul past the end" } */
  T ( 2, "%i",  R ( 0, 1));
  T ( 2, "%i",  R ( 8, 9));
  T ( 2, "%i",  R ( 0, 9));
  T ( 2, "%i",  R (-1, 0));     /* { dg-warning "may write a terminating nul past the end of the destination" } */
  T ( 2, "%i",  R ( 9, 10));    /* { dg-warning "may write a terminating nul past the end of the destination" } */

  T ( 3, "%i",  R ( 0, 99));
  T ( 3, "%i",  R (99, 999));   /* { dg-warning "may write a terminating nul past the end of the destination" } */

  T ( 4, "%i",  R (  0,  999));
  T ( 4, "%i",  R ( 99,  999));
  T ( 4, "%i",  R (998,  999));
  T ( 4, "%i",  R (999, 1000)); /* { dg-warning "may write a terminating nul past the end of the destination" } */
}

NOIPA void test_sprintf_chk_range_uint (void)
{
#undef R
#define R(min, max) range_uint (min, max)

  T ( 0, "%i",  R ( 0, 9));     /* { dg-warning ".%i. directive writing 1 byte into a region of size 0" } */
  T ( 1, "%i",  R ( 0, 1));     /* { dg-warning "nul past the end" } */
  T ( 1, "%i",  R ( 0, 9));     /* { dg-warning "nul past the end" } */
  T ( 2, "%i",  R ( 0, 1));
  T ( 2, "%i",  R ( 8, 9));
  T ( 2, "%i",  R ( 0, 9));
  T ( 2, "%i",  R ( 9, 10));    /* { dg-warning "may write a terminating nul past the end of the destination" } */

  T ( 3, "%i",  R ( 0, 99));
  T ( 3, "%i",  R (99, 999));   /* { dg-warning "may write a terminating nul past the end of the destination" } */

  T ( 4, "%i",  R (  0,  999));
  T ( 4, "%i",  R ( 99,  999));
  T ( 4, "%i",  R (998,  999));
  T ( 4, "%i",  R (999, 1000)); /* { dg-warning "may write a terminating nul past the end of the destination" } */
}

/* Verify that destination size in excess of INT_MAX (and, separately,
   in excess of the largest object) is diagnosed.  The former because
   the functions are defined only for output of at most INT_MAX and
   specifying a large upper bound defeats the bounds checking (and,
   on some implementations such as Solaris, causes the function to
   fail.  The latter because due to the limit of ptrdiff_t no object
   can be larger than PTRDIFF_MAX bytes.  */

NOIPA void test_too_large (char *d, int x, __builtin_va_list va)
{
  const size_t imax = __INT_MAX__;
  const size_t imax_p1 = imax + 1;

  __builtin_snprintf (d, imax,    "%c", x);
  __builtin_snprintf (d, imax_p1, "%c", x);   /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "INT_MAX + 1" { target { lp64 || msp430_large } } } */
  /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" "INT_MAX + 1" { target { { avr-*-* } || { { ilp32 } || { int16 && { ! msp430_large } } } } } .-1 } */

  __builtin_vsnprintf (d, imax,    "%c", va);
  __builtin_vsnprintf (d, imax_p1, "%c", va);   /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "INT_MAX + 1" { target { lp64 || msp430_large } } } */
  /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" "INT_MAX + 1" { target { { avr-*-* } || { { ilp32 } || { int16 && { ! msp430_large } } } } } .-1 } */

  __builtin___snprintf_chk (d, imax,    0, imax,    "%c", x);
  __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 } } } */
  /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" "INT_MAX + 1" { target { { avr-*-* } || { { ilp32 } || { int16 && { ! msp430_large } } } } } .-1 } */

  __builtin___vsnprintf_chk (d, imax,    0, imax,    "%c", va);
  __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 } } } */
  /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size" "INT_MAX + 1" { target { { avr-*-* } || { { ilp32 } || { int16 && { ! msp430_large } } } } } .-1 } */

  const size_t ptrmax = __PTRDIFF_MAX__;
  const size_t ptrmax_m1 = ptrmax - 1;

  __builtin_snprintf (d, ptrmax_m1, "%c", x);  /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "PTRDIFF_MAX - 1" { target { lp64 || msp430_large } } } */
  __builtin_snprintf (d, ptrmax, "  %c", x);   /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "PTRDIFF_MAX" { target { lp64 || msp430_large } } } */

  __builtin_vsnprintf (d, ptrmax_m1, "%c", va);   /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "PTRDIFF_MAX - 1" { target { lp64 || msp430_large } } } */
  __builtin_vsnprintf (d, ptrmax,    "%c", va);   /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "PTRDIFF_MAX" { target { lp64 || msp430_large } } } */

  __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 } } } */
  __builtin___snprintf_chk (d, ptrmax,    0, ptrmax,    "%c", x);   /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "PTRDIFF_MAX" { target { lp64 || msp430_large } } } */

  __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 } } } */
  __builtin___vsnprintf_chk (d, ptrmax,    0, ptrmax,    "%c", va);   /* { dg-warning "specified bound \[0-9\]+ exceeds .INT_MAX." "PTRDIFF_MAX" { target { lp64 || msp430_large } } } */
}

/* Exercise ordinary sprintf with malloc.  */
#undef TEST_SPRINTF
#define TEST_SPRINTF(d, maxsize, objsize, fmt, ...)	\
  __builtin_sprintf (d, fmt, __VA_ARGS__)

NOIPA void test_sprintf_malloc (const char *s, const char *t)
{
#define x x ()

  T (1, "%-s", x ? "" : "1");       /* { dg-warning "nul past the end" } */
  T (1, "%-s", x ? "1" : "");       /* { dg-warning "nul past the end" } */
  T (1, "%-s", x ? s : "1");        /* { dg-warning "nul past the end" } */
  T (1, "%-s", x ? "1" : s);        /* { dg-warning "nul past the end" } */
  T (1, "%-s", x ? s : t);

  T (2, "%-s", x ? "" : "1");
  T (2, "%-s", x ? "" : s);
  T (2, "%-s", x ? "1" : "");
  T (2, "%-s", x ? s : "");
  T (2, "%-s", x ? "1" : "2");
  T (2, "%-s", x ? "" : "12");      /* { dg-warning "nul past the end" } */
  T (2, "%-s", x ? "12" : "");      /* { dg-warning "nul past the end" } */

  T (2, "%-s", x ? "" : "123");     /* { dg-warning "into a region" } */
  T (2, "%-s", x ? "123" : "");     /* { dg-warning "into a region" } */

#undef x
}

/* Exercise ordinary sprintf with alloca.  */
#undef ALLOC
#define ALLOC(p, n) (p) = __builtin_alloca (n)

NOIPA void test_sprintf_alloca (const char *s, const char *t)
{
#define x x ()

  T (1, "%-s", x ? "" : "1");       /* { dg-warning "nul past the end" } */
  T (1, "%-s", x ? "1" : "");       /* { dg-warning "nul past the end" } */
  T (1, "%-s", x ? s : "1");        /* { dg-warning "nul past the end" } */
  T (1, "%-s", x ? "1" : s);        /* { dg-warning "nul past the end" } */
  T (1, "%-s", x ? s : t);

  T (2, "%-s", x ? "" : "1");
  T (2, "%-s", x ? "" : s);
  T (2, "%-s", x ? "1" : "");
  T (2, "%-s", x ? s : "");
  T (2, "%-s", x ? "1" : "2");
  T (2, "%-s", x ? "" : "12");      /* { dg-warning "nul past the end" } */
  T (2, "%-s", x ? "12" : "");      /* { dg-warning "nul past the end" } */

  T (2, "%-s", x ? "" : "123");     /* { dg-warning "into a region" } */
  T (2, "%-s", x ? "123" : "");     /* { dg-warning "into a region" } */

#undef x
}

/* Exercise ordinary sprintf with a VLA.  */
#undef ALLOC
#define ALLOC(p, n) char vla [i (n)]; (p) = vla

NOIPA void test_sprintf_vla (const char *s, const char *t)
{
#define x x ()

  T (1, "%-s", x ? "" : "1");       /* { dg-warning "nul past the end" } */
  T (1, "%-s", x ? "1" : "");       /* { dg-warning "nul past the end" } */
  T (1, "%-s", x ? s : "1");        /* { dg-warning "nul past the end" } */
  T (1, "%-s", x ? "1" : s);        /* { dg-warning "nul past the end" } */
  T (1, "%-s", x ? s : t);

  T (2, "%-s", x ? "" : "1");
  T (2, "%-s", x ? "" : s);
  T (2, "%-s", x ? "1" : "");
  T (2, "%-s", x ? s : "");
  T (2, "%-s", x ? "1" : "2");
  T (2, "%-s", x ? "" : "12");      /* { dg-warning "nul past the end" } */
  T (2, "%-s", x ? "12" : "");      /* { dg-warning "nul past the end" } */

  T (2, "%-s", x ? "" : "123");     /* { dg-warning "into a region" } */
  T (2, "%-s", x ? "123" : "");     /* { dg-warning "into a region" } */

#undef x
}