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
|
/* PR middle-end/79376 - wrong lower bound with %s and non-constant
strings in -Wformat-overflow
{ dg-do compile }
{ dg-options "-O2 -Wall -Wformat-overflow=1 -ftrack-macro-expansion=0" } */
typedef __SIZE_TYPE__ size_t;
#define INT_MAX __INT_MAX__
#define INT_MIN (-INT_MAX - 1)
/* When debugging, define LINE to the line number of the test case to exercise
and avoid exercising any of the others. The buffer and objsize macros
below make use of LINE to avoid warnings for other lines. */
#ifndef LINE
# define LINE 0
#endif
extern int int_value (void);
static int int_range (int min, int max)
{
int n = int_value ();
return n < min || max < n ? min : n;
}
static const char*
choose_string (const char *s1, const char *s2, const char *s3)
{
int i = int_value ();
return i < 0 ? s1 : 0 < i ? s3 : s2;
}
void sink (char*, char*);
int dummy_sprintf (char*, const char*, ...);
char buffer [256];
extern char *ptr;
const char s0[] = "";
const char s1[] = "1";
const char s2[] = "12";
const char s3[] = "123";
const char s4[] = "1234";
const char s5[] = "12345";
const char s6[] = "123456";
const char s7[] = "1234567";
const char s8[] = "12345678";
const char s9[] = "123456789";
extern const char sx[];
extern const char sy[];
/* Evaluate to an array of SIZE characters when non-negative, or to
a pointer to an unknown object otherwise. */
#define buffer(size) \
((0 <= size) ? buffer + sizeof buffer - (size) : ptr)
/* Helper to expand function to either __builtin_f or dummy_f to
make debugging GCC easy. */
#define FUNC(f) \
((!LINE || LINE == __LINE__) ? __builtin_ ## f : dummy_ ## f)
/* Macro to verify that calls to __builtin_sprintf (i.e., with no size
argument) issue diagnostics by correctly determining the size of
the destination buffer. */
#define T(size, ...) \
(FUNC (sprintf) (buffer (size), __VA_ARGS__), \
sink (buffer, ptr))
/* Return a value in the range [MIN, MAX]. */
#define R(min, max) int_range (min, max)
/* Return one of the strings S1, S2, S3. */
#define S(s1, s2, s3) choose_string (s1, s2, s3)
struct S {
char a1[1];
char a2[2];
char a3[3];
char a4[4];
char a5[5];
char ax[];
};
void test_strings (struct S *s)
{
T (0, "%-s", S (s->a1, s->a1, s1)); /* { dg-warning "writing up to 1 byte" } */
T (0, "%-s", S (s->a1, s->a2, s1)); /* { dg-warning "writing up to 1 byte" } */
T (0, "%-s", S (s->a2, s->a1, s1)); /* { dg-warning "writing up to 1 byte" } */
T (0, "%-s", S (s->a1, s1, s->a1)); /* { dg-warning "writing up to 1 byte" } */
T (0, "%-s", S (s->a1, s1, s->a2)); /* { dg-warning "writing up to 1 byte" } */
T (0, "%-s", S (s->a2, s1, s->a1)); /* { dg-warning "writing up to 1 byte" } */
T (0, "%-s", S (s1, s->a1, s1)); /* { dg-warning "writing up to 1 byte" } */
T (0, "%-s", S (s1, s->a1, s1)); /* { dg-warning "writing up to 1 byte" } */
T (0, "%-s", S (s1, s->a2, s1)); /* { dg-warning "writing up to 1 byte" } */
T (0, "%-s", S (s->a1, s->a1, s2)); /* { dg-warning "writing up to 2 bytes" } */
T (0, "%-s", S (s->a1, s->a2, s2)); /* { dg-warning "writing up to 2 bytes" } */
T (0, "%-s", S (s->a2, s->a1, s2)); /* { dg-warning "writing up to 2 bytes" } */
T (0, "%-s", S (s->a1, s2, s->a1)); /* { dg-warning "writing up to 2 bytes" } */
T (0, "%-s", S (s->a1, s2, s->a2)); /* { dg-warning "writing up to 2 bytes" } */
T (0, "%-s", S (s->a2, s2, s->a1)); /* { dg-warning "writing up to 2 bytes" } */
T (0, "%-s", S (s2, s->a1, s1)); /* { dg-warning "writing up to 2 bytes" } */
T (0, "%-s", S (s2, s->a1, s1)); /* { dg-warning "writing up to 2 bytes" } */
T (0, "%-s", S (s2, s->a2, s1)); /* { dg-warning "writing up to 2 bytes" } */
T (0, "%-s", S (s->a1, s->a1, s2)); /* { dg-warning "writing up to 2 bytes" } */
T (0, "%-s", S (s->a1, s->a2, s2)); /* { dg-warning "writing up to 2 bytes" } */
T (0, "%-s", S (s->a2, s->a1, s2)); /* { dg-warning "writing up to 2 bytes" } */
T (0, "%-s", S (s->a1, s2, s->a1)); /* { dg-warning "writing up to 2 bytes" } */
T (0, "%-s", S (s->a1, s2, s->a2)); /* { dg-warning "writing up to 2 bytes" } */
T (0, "%-s", S (s->a2, s2, s->a1)); /* { dg-warning "writing up to 2 bytes" } */
T (0, "%-s", S (s2, s->a1, s1)); /* { dg-warning "writing up to 2 bytes" } */
T (0, "%-s", S (s3, s->a1, s2)); /* { dg-warning "writing up to 3 bytes" } */
T (0, "%-s", S (s4, s->a2, s3)); /* { dg-warning "writing up to 4 bytes" } */
T (0, "%-s", S (s->a3, s->a5, s3)); /* { dg-warning "writing up to 4 bytes" } */
T (0, "%-s", S (s->a3, s->a5, s3)); /* { dg-warning "writing up to 4 bytes" } */
T (0, "%-s", S (s->a3, s->a5, s3)); /* { dg-warning "writing up to 4 bytes" } */
T (0, "%-s", S (s->a3, s3, s->a5)); /* { dg-warning "writing up to 4 bytes" } */
T (0, "%-s", S (s->a5, s3, s->a3)); /* { dg-warning "writing up to 4 bytes" } */
T (0, "%-s", S (s->a3, s3, s->a5)); /* { dg-warning "writing up to 4 bytes" } */
T (0, "%-s", S (s3, s->a1, s4)); /* { dg-warning "writing up to 4 bytes" } */
T (0, "%-s", S (s4, s->a2, s3)); /* { dg-warning "writing up to 4 bytes" } */
}
void test_strings_with_width (struct S *s)
{
T (0, "%*s", /* { dg-warning "writing up to 4 bytes" } */
R (0, 1),
S (s->a3, s->a5, s3));
T (0, "%*s", /* { dg-warning "writing up to 4 bytes" } */
R (0, 4),
S (s->a3, s->a5, s3));
T (0, "%*s", /* { dg-warning "writing between 1 and 4 bytes" } */
R (1, 4),
S (s->a3, s->a5, s3));
T (0, "%*s", /* { dg-warning "writing between 2 and 4 bytes" } */
R (2, 4),
S (s->a3, s->a5, s3));
T (0, "%*s", /* { dg-warning "writing between 3 and 4 bytes" } */
R (3, 4),
S (s->a3, s->a5, s3));
T (0, "%*s", /* { dg-warning "writing between 3 and 5 bytes" } */
R (3, 5),
S (s->a3, s->a5, s3));
T (0, "%*s", /* { dg-warning "writing between 3 and 9 bytes" } */
R (3, 9),
S (s->a3, s->a5, s3));
T (3, "%*s", /* { dg-warning "writing between 3 and 9 bytes" } */
R (3, 9),
S (s->a3, s->a5, s3));
/* The longest string fits but the terminating nul will overflow. Since
the null won't overflow with the shorter strings the warning is a "may
write." */
T (4, "%*s", /* { dg-warning "may write a terminating nul" } */
R (3, 9),
S (s->a3, s->a5, s3));
}
void test_strings_with_width_and_precisin (struct S *s)
{
T (0, "%*.*s", /* { dg-warning "writing up to 1 byte" } */
R (0, 1),
R (0, 1),
S (s->a3, s->a5, s3));
T (0, "%*.*s", /* { dg-warning "writing up to 2 bytes" } */
R (0, 2),
R (0, 1),
S (s->a3, s->a5, s3));
T (0, "%*.*s", /* { dg-warning "writing up to 9 bytes" } */
R (0, 9),
R (0, 1),
S (s->a3, s->a5, s3));
/* Since the longest string/array fits there is no warning even if
the maximum width would cause overflow. */
T (6, "%*.*s",
R (0, 9),
R (0, 1),
S (s->a3, s->a5, s3));
T (0, "%*.*s", /* { dg-warning "writing up to 2 bytes" } */
R (0, 2),
R (0, 2),
S (s->a3, s->a5, s3));
T (0, "%*.*s", /* { dg-warning "writing up to 3 bytes" } */
R (0, 3),
R (0, 2),
S (s->a3, s->a5, s3));
}
|