1 /* { dg-do compile } */
2 /* { dg-options "-O2 -Wformat -Wformat-truncation=1 -ftrack-macro-expansion=0" } */
7 /* Separate a0 from a1 to prevent the former from being substituted
8 for the latter and causing false positives. */
18 #define buffer(size) (buffer + sizeof buffer - size)
20 static int value_range (int min
, int max
)
22 extern int value (void);
24 return val
< min
|| max
< val
? min
: val
;
27 #define R(min, max) value_range (min, max)
29 extern void sink (void*);
31 /* Verify that calls to snprintf whose return value is unused are
32 diagnosed if certain or possible truncation is detected. */
34 #define T(size, ...) \
35 __builtin_snprintf (buffer (size), size, __VA_ARGS__), sink (buffer)
37 void test_int_retval_unused (void)
39 T (2, "%i", 123); /* { dg-warning "output truncated" } */
40 T (2, "%i", R (1, 99)); /* { dg-warning "output may be truncated" } */
41 T (2, "%i", R (10, 99)); /* { dg-warning "output truncated" } */
42 T (3, "%i%i", R (1, 99), R (1, 99)); /* { dg-warning "output may be truncated" } */
45 void test_string_retval_unused (const Arrays
*ar
)
47 /* At level 1 strings of unknown length are assumed to be empty so
48 the following is not diagnosed. */
50 /* A one-byte array can only hold an empty string, so the following
53 /* Unlike the ar->a0 case above, at level 1, the length of an unknown
54 string that points to an array of known size is assumed to be the
55 size of the array minus 1. */
56 T (1, "%-s", ar
->a2
); /* { dg-warning "output may be truncated" } */
57 T (1, "%-s", ar
->a3
); /* { dg-warning "output may be truncated" } */
58 T (1, "%-s", ar
->a4
); /* { dg-warning "output may be truncated" } */
59 /* Same as the ar->a0 case above. */
64 /* Verify that calls to snprintf whose return value is used are
65 diagnosed only if certain truncation is detected but not when
66 truncation is only possible but not certain. */
71 #define T(size, ...) \
72 retval = __builtin_snprintf (buffer (size), size, __VA_ARGS__)
74 void test_int_retval_used (void)
76 T (2, "%i", 123); /* { dg-warning "output truncated" } */
77 T (2, "%i", R (1, 99));
78 T (2, "%i", R (10, 99)); /* { dg-warning "output truncated" } */
79 T (3, "%i%i", R (1, 99), R (1, 99));
82 void test_string_retval_used (const Arrays
*ar
)
89 T (1, "%-s", "123"); /* { dg-warning "output truncated" } */