| 1 |
/* PR tree-optimization/79327 - wrong code at -O2 and -fprintf-return-value |
| 2 |
{ dg-do compile } |
| 3 |
{ dg-options "-O2 -Wall -Wformat-overflow=1 -ftrack-macro-expansion=0" } |
| 4 |
{ dg-require-effective-target int32plus } */ |
| 5 |
|
| 6 |
typedef __SIZE_TYPE__ size_t; |
| 7 |
typedef __WCHAR_TYPE__ wchar_t; |
| 8 |
|
| 9 |
#define INT_MAX __INT_MAX__ |
| 10 |
#define INT_MIN (-INT_MAX - 1) |
| 11 |
|
| 12 |
/* When debugging, define LINE to the line number of the test case to exercise |
| 13 |
and avoid exercising any of the others. The buffer and objsize macros |
| 14 |
below make use of LINE to avoid warnings for other lines. */ |
| 15 |
#ifndef LINE |
| 16 |
# define LINE 0 |
| 17 |
#endif |
| 18 |
|
| 19 |
void sink (char*, char*); |
| 20 |
|
| 21 |
int dummy_sprintf (char*, const char*, ...); |
| 22 |
|
| 23 |
char buffer [256]; |
| 24 |
extern char *ptr; |
| 25 |
|
| 26 |
static int int_range (int min, int max) |
| 27 |
{ |
| 28 |
extern int int_value (void); |
| 29 |
int n = int_value (); |
| 30 |
return n < min || max < n ? min : n; |
| 31 |
} |
| 32 |
|
| 33 |
static unsigned uint_range (unsigned min, unsigned max) |
| 34 |
{ |
| 35 |
extern unsigned uint_value (void); |
| 36 |
unsigned n = uint_value (); |
| 37 |
return n < min || max < n ? min : n; |
| 38 |
} |
| 39 |
|
| 40 |
/* Evaluate to an array of SIZE characters when non-negative, or to |
| 41 |
a pointer to an unknown object otherwise. */ |
| 42 |
#define buffer(size) \ |
| 43 |
((0 <= size) ? buffer + sizeof buffer - (size) : ptr) |
| 44 |
|
| 45 |
/* Helper to expand function to either __builtin_f or dummy_f to |
| 46 |
make debugging GCC easy. */ |
| 47 |
#define FUNC(f) \ |
| 48 |
((!LINE || LINE == __LINE__) ? __builtin_ ## f : dummy_ ## f) |
| 49 |
|
| 50 |
/* Macro to verify that calls to __builtin_sprintf (i.e., with no size |
| 51 |
argument) issue diagnostics by correctly determining the size of |
| 52 |
the destination buffer. */ |
| 53 |
#define T(size, ...) \ |
| 54 |
(FUNC (sprintf) (buffer (size), __VA_ARGS__), \ |
| 55 |
sink (buffer, ptr)) |
| 56 |
|
| 57 |
/* Return a signed integer in the range [MIN, MAX]. */ |
| 58 |
#define R(min, max) int_range (min, max) |
| 59 |
|
| 60 |
/* Return a unsigned integer in the range [MIN, MAX]. */ |
| 61 |
#define U(min, max) uint_range (min, max) |
| 62 |
|
| 63 |
/* Exercise the hh length modifier with ranges. */ |
| 64 |
void test_hh (void) |
| 65 |
{ |
| 66 |
T (0, "%hhi", R ( -1, 0)); /* { dg-warning "between 1 and 2 bytes" } */ |
| 67 |
T (0, "%hhi", R ( -1, 1)); /* { dg-warning "between 1 and 2 bytes" } */ |
| 68 |
T (0, "%hhi", R ( -1, 12)); /* { dg-warning "between 1 and 2 bytes" } */ |
| 69 |
T (0, "%hhi", R ( -1, 123)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 70 |
T (0, "%hhi", R ( -1, 128)); /* { dg-warning "between 1 and 4 bytes" } */ |
| 71 |
T (0, "%hhi", R ( -1, 257)); /* { dg-warning "between 1 and 4 bytes" } */ |
| 72 |
T (0, "%hhi", R ( -1, 1234)); /* { dg-warning "between 1 and 4 bytes" } */ |
| 73 |
T (0, "%hhi", R ( -12, -11)); /* { dg-warning "writing 3 bytes" } */ |
| 74 |
T (0, "%hhi", R ( -12, -1)); /* { dg-warning "between 2 and 3 bytes" } */ |
| 75 |
T (0, "%hhi", R ( -12, 0)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 76 |
T (0, "%hhi", R ( -12, 1)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 77 |
T (0, "%hhi", R ( -12, 12)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 78 |
T (0, "%hhi", R ( -12, 123)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 79 |
T (0, "%hhi", R ( -12, 128)); /* { dg-warning "between 1 and 4 bytes" } */ |
| 80 |
T (0, "%hhi", R ( -12, 257)); /* { dg-warning "between 1 and 4 bytes" } */ |
| 81 |
T (0, "%hhi", R ( -12, 1234)); /* { dg-warning "between 1 and 4 bytes" } */ |
| 82 |
T (0, "%hhi", R ( -99, -10)); /* { dg-warning "writing 3 bytes" } */ |
| 83 |
T (0, "%hhi", R (-123, -1)); /* { dg-warning "between 2 and 4 bytes" } */ |
| 84 |
T (0, "%hhi", R (-123, 0)); /* { dg-warning "between 1 and 4 bytes" } */ |
| 85 |
T (0, "%hhi", R (-123, 1)); /* { dg-warning "between 1 and 4 bytes" } */ |
| 86 |
T (0, "%hhi", R (-123, 12)); /* { dg-warning "between 1 and 4 bytes" } */ |
| 87 |
T (0, "%hhi", R (-123, 123)); /* { dg-warning "between 1 and 4 bytes" } */ |
| 88 |
T (0, "%hhi", R (-123, 257)); /* { dg-warning "between 1 and 4 bytes" } */ |
| 89 |
T (0, "%hhi", R (-123, 1234)); /* { dg-warning "between 1 and 4 bytes" } */ |
| 90 |
T (0, "%hhi", R (-129, 1)); /* { dg-warning "between 1 and 4 bytes" } */ |
| 91 |
T (0, "%hhi", R (-130, -129)); /* { dg-warning "writing 3 bytes" } */ |
| 92 |
|
| 93 |
T (0, "%hhi", U ( 0, 127)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 94 |
|
| 95 |
/* The following results in either "127" and "-128" so the ideal result |
| 96 |
should be "between 3 and 4 bytes" but because of the overflow from |
| 97 |
128 to -128 in the %hhi directive the input range is reset to that |
| 98 |
of char, or [CHAR_MIN, CHAR_MAX], and the warning reflects that. */ |
| 99 |
T (0, "%hhi", U ( 127, 128)); /* { dg-warning "between \[13\] and 4 bytes" } */ |
| 100 |
/* The following results in either "-128" or "-127". */ |
| 101 |
T (0, "%hhi", U ( 128, 129)); /* { dg-warning "writing 4 bytes" } */ |
| 102 |
/* The following results in between "-128" and "-99". */ |
| 103 |
T (0, "%hhi", U ( 128, 157)); /* { dg-warning "writing between 3 and 4 bytes" } */ |
| 104 |
/* Between "-128" and "-1". */ |
| 105 |
T (0, "%hhi", U ( 128, 255)); /* { dg-warning "writing between 2 and 4 bytes" } */ |
| 106 |
/* Between "-128" and "0". */ |
| 107 |
T (0, "%hhi", U ( 128, 256)); /* { dg-warning "writing between 1 and 4 bytes" } */ |
| 108 |
/* Between "-128" and "" (zero formats as nothing with zero precision). */ |
| 109 |
T (0, "%.0hhi", U ( 128, 256)); /* { dg-warning "writing up to 4 bytes" } */ |
| 110 |
/* Same as above but with a range of precisions including zero. */ |
| 111 |
T (0, "%.*hhi", /* { dg-warning "writing up to 4 bytes" } */ |
| 112 |
R (0, 1), U ( 128, 256)); |
| 113 |
/* Same as above but with a positive range of precisions. */ |
| 114 |
T (0, "%.*hhi", /* { dg-warning "between 1 and 4 bytes" } */ |
| 115 |
R (1, 2), U ( 128, 256)); |
| 116 |
/* Precision range includes zero but width is non-zero so output cannot |
| 117 |
be empty. */ |
| 118 |
T (0, "%1.*hhi", /* { dg-warning "between 1 and 4 bytes" } */ |
| 119 |
R (0, 2), U ( 128, 256)); |
| 120 |
/* Same as above but with a width range. */ |
| 121 |
T (0, "%*.*hhi", /* { dg-warning "between 1 and 4 bytes" } */ |
| 122 |
R (1, 2), R (0, 2), U ( 128, 256)); |
| 123 |
/* Same as above but this time width range does include zero. */ |
| 124 |
T (0, "%*.*hhi", /* { dg-warning "up to 4 bytes" } */ |
| 125 |
R (0, 2), R (0, 2), U ( 128, 256)); |
| 126 |
|
| 127 |
/* Range of precisions in excess of the number of digits and sign. */ |
| 128 |
T (0, "%.*hhi", /* { dg-warning "between 5 and 8 bytes" } */ |
| 129 |
R (5, 7), U ( 128, 256)); |
| 130 |
|
| 131 |
/* Between "-128" and "+0". */ |
| 132 |
T (0, "%+hhi", U ( 128, 256)); /* { dg-warning "between 2 and 4 bytes" } */ |
| 133 |
/* Between "-128" and " 0". */ |
| 134 |
T (0, "% hhi", U ( 128, 256)); /* { dg-warning "between 2 and 4 bytes" } */ |
| 135 |
|
| 136 |
T (0, "%hhu", R ( -1, 1)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 137 |
T (0, "%hhu", R ( -1, 12)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 138 |
T (0, "%hhu", R ( -1, 123)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 139 |
T (0, "%hhu", R ( -1, 128)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 140 |
T (0, "%hhu", R ( -1, 257)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 141 |
T (0, "%hhu", R ( -1, 1234)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 142 |
T (0, "%hhu", R ( -12, 1)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 143 |
T (0, "%hhu", R ( -12, 12)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 144 |
T (0, "%hhu", R ( -12, 123)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 145 |
T (0, "%hhu", R ( -12, 128)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 146 |
T (0, "%hhu", R ( -12, 257)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 147 |
T (0, "%hhu", R ( -12, 1234)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 148 |
T (0, "%hhu", R (-123, 1)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 149 |
T (0, "%hhu", R (-123, 12)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 150 |
T (0, "%hhu", R (-123, 123)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 151 |
T (0, "%hhu", R (-123, 257)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 152 |
T (0, "%hhu", R (-123, 1234)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 153 |
T (0, "%hhu", R (-129, 1)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 154 |
T (0, "%hhu", R (-199, -159)); /* { dg-warning "writing 2 bytes" } */ |
| 155 |
T (0, "%hhu", R (-255, -250)); /* { dg-warning "writing 1 byte" } */ |
| 156 |
} |
| 157 |
|
| 158 |
/* Exercise the h length modifier. */ |
| 159 |
void test_h (void) |
| 160 |
{ |
| 161 |
T (0, "%hi", R ( -1, 0)); /* { dg-warning "between 1 and 2 bytes" } */ |
| 162 |
T (0, "%hi", R ( 0, 1)); /* { dg-warning "writing 1 byte" } */ |
| 163 |
T (0, "%hi", R ( -1, 1)); /* { dg-warning "between 1 and 2 bytes" } */ |
| 164 |
T (0, "%hi", R ( -1, 12)); /* { dg-warning "between 1 and 2 bytes" } */ |
| 165 |
T (0, "%hi", R ( -12, 1)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 166 |
T (0, "%hi", R ( -99, -10)); /* { dg-warning "writing 3 bytes" } */ |
| 167 |
T (0, "%hi", R ( -123, 4)); /* { dg-warning "between 1 and 4 bytes" } */ |
| 168 |
T (0, "%hi", R ( -1234, 56)); /* { dg-warning "between 1 and 5 bytes" } */ |
| 169 |
T (0, "%hi", R ( -1234, 567)); /* { dg-warning "between 1 and 5 bytes" } */ |
| 170 |
T (0, "%hi", R ( -1234, 5678)); /* { dg-warning "between 1 and 5 bytes" } */ |
| 171 |
T (0, "%ho", R (-32768,-32767)); /* { dg-warning "writing 6 bytes" } */ |
| 172 |
|
| 173 |
T (0, "%ho", R ( -1, 0)); /* { dg-warning "between 1 and 6 bytes" } */ |
| 174 |
T (0, "%ho", R ( 0, 1)); /* { dg-warning "writing 1 byte" } */ |
| 175 |
T (0, "%ho", R ( -1, 1)); /* { dg-warning "between 1 and 6 bytes" } */ |
| 176 |
T (0, "%ho", R ( -1, 12)); /* { dg-warning "between 1 and 6 bytes" } */ |
| 177 |
T (0, "%ho", R ( -12, 1)); /* { dg-warning "between 1 and 6 bytes" } */ |
| 178 |
T (0, "%ho", R ( -123, 4)); /* { dg-warning "between 1 and 6 bytes" } */ |
| 179 |
T (0, "%ho", R ( -1234, 56)); /* { dg-warning "between 1 and 6 bytes" } */ |
| 180 |
T (0, "%ho", R ( -1234, 567)); /* { dg-warning "between 1 and 6 bytes" } */ |
| 181 |
T (0, "%ho", R ( -1234, 5678)); /* { dg-warning "between 1 and 6 bytes" } */ |
| 182 |
T (0, "%ho", R (-32768,-32767)); /* { dg-warning "writing 6 bytes" } */ |
| 183 |
|
| 184 |
T (0, "%hu", R ( -1, 0)); /* { dg-warning "between 1 and 5 bytes" } */ |
| 185 |
T (0, "%hu", R ( 0, 1)); /* { dg-warning "writing 1 byte" } */ |
| 186 |
T (0, "%hu", R ( -1, 1)); /* { dg-warning "between 1 and 5 bytes" } */ |
| 187 |
T (0, "%hu", R ( -1, 12)); /* { dg-warning "between 1 and 5 bytes" } */ |
| 188 |
T (0, "%hu", R ( -12, 1)); /* { dg-warning "between 1 and 5 bytes" } */ |
| 189 |
T (0, "%hu", R ( -123, 4)); /* { dg-warning "between 1 and 5 bytes" } */ |
| 190 |
T (0, "%hu", R ( -1234, 56)); /* { dg-warning "between 1 and 5 bytes" } */ |
| 191 |
T (0, "%hu", R ( -1234, 567)); /* { dg-warning "between 1 and 5 bytes" } */ |
| 192 |
T (0, "%hu", R ( -1234, 5678)); /* { dg-warning "between 1 and 5 bytes" } */ |
| 193 |
T (0, "%hu", R (-32768,-32767)); /* { dg-warning "writing 5 bytes" } */ |
| 194 |
|
| 195 |
T (0, "%hx", R (-32768,-32767)); /* { dg-warning "writing 4 bytes" } */ |
| 196 |
} |
| 197 |
|
| 198 |
/* Exercise integer directives with no length modifier. */ |
| 199 |
void test_diou (void) |
| 200 |
{ |
| 201 |
T (0, "%d", R ( -1, 0)); /* { dg-warning "between 1 and 2 bytes" } */ |
| 202 |
T (0, "%i", R ( 0, 1)); /* { dg-warning "writing 1 byte" } */ |
| 203 |
T (0, "%d", R ( -1, 1)); /* { dg-warning "between 1 and 2 bytes" } */ |
| 204 |
T (0, "%i", R ( -1, 12)); /* { dg-warning "between 1 and 2 bytes" } */ |
| 205 |
T (0, "%d", R ( -12, 1)); /* { dg-warning "between 1 and 3 bytes" } */ |
| 206 |
T (0, "%i", R ( -123, 4)); /* { dg-warning "between 1 and 4 bytes" } */ |
| 207 |
T (0, "%d", R ( -1234, 56)); /* { dg-warning "between 1 and 5 bytes" } */ |
| 208 |
T (0, "%i", R ( -1234, 567)); /* { dg-warning "between 1 and 5 bytes" } */ |
| 209 |
T (0, "%d", R ( -1234, 5678)); /* { dg-warning "between 1 and 5 bytes" } */ |
| 210 |
|
| 211 |
T (0, "%u", R ( -1, 0)); /* { dg-warning "between 1 and 10 bytes" } */ |
| 212 |
T (0, "%u", R ( 0, 1)); /* { dg-warning "writing 1 byte" } */ |
| 213 |
T (0, "%u", R ( -1, 1)); /* { dg-warning "between 1 and 10 bytes" } */ |
| 214 |
T (0, "%u", R ( -1, 12)); /* { dg-warning "between 1 and 10 bytes" } */ |
| 215 |
T (0, "%u", R ( -12, 1)); /* { dg-warning "between 1 and 10 bytes" } */ |
| 216 |
T (0, "%u", R ( -123, 4)); /* { dg-warning "between 1 and 10 bytes" } */ |
| 217 |
T (0, "%u", R ( -1234, 56)); /* { dg-warning "between 1 and 10 bytes" } */ |
| 218 |
T (0, "%u", R ( -1234, 567)); /* { dg-warning "between 1 and 10 bytes" } */ |
| 219 |
T (0, "%u", R ( -1234, 5678)); /* { dg-warning "between 1 and 10 bytes" } */ |
| 220 |
|
| 221 |
T (0, "%o", R ( -1, 0)); /* { dg-warning "between 1 and 11 bytes" } */ |
| 222 |
T (0, "%o", R ( -2, 1)); /* { dg-warning "between 1 and 11 bytes" } */ |
| 223 |
T (0, "%o", R ( 0, 1)); /* { dg-warning "writing 1 byte" } */ |
| 224 |
|
| 225 |
T (0, "%x", R ( -1, 0)); /* { dg-warning "between 1 and 8 bytes" } */ |
| 226 |
T (0, "%x", R ( -2, 1)); /* { dg-warning "between 1 and 8 bytes" } */ |
| 227 |
T (0, "%x", R ( 0, 1)); /* { dg-warning "writing 1 byte" } */ |
| 228 |
} |