| 1 |
/* PR middle-end/91631 - buffer overflow into an array member of a declared |
| 2 |
object not detected |
| 3 |
Test to verify that past-the-end accesses by string functions to member |
| 4 |
arrays by-reference objects are diagnosed. |
| 5 |
{ dg-do compile } |
| 6 |
{ dg-options "-O2 -Wall -Wno-unused-local-typedefs -Wno-stringop-overflow -ftrack-macro-expansion=0" } */ |
| 7 |
|
| 8 |
#define SA(expr) typedef int StaticAssert [2 * !!(expr) - 1] |
| 9 |
|
| 10 |
typedef __SIZE_TYPE__ size_t; |
| 11 |
|
| 12 |
extern char* strcpy (char*, const char*); |
| 13 |
extern char* strncpy (char*, const char*, size_t); |
| 14 |
|
| 15 |
void sink (void*); |
| 16 |
|
| 17 |
struct MA17 |
| 18 |
{ |
| 19 |
char pad[4]; |
| 20 |
char a1[1], a2[2], a3[3], a4[4], a5[5], a6[6], a7[7], a8[8], a9[9], a10[10]; |
| 21 |
char a11[11], a12[12], a13[13], a14[14], a15[15], a16[16], a17[17], ax[]; |
| 22 |
}; |
| 23 |
|
| 24 |
extern struct MA17 gma; |
| 25 |
extern struct MA17 gma2[2]; |
| 26 |
|
| 27 |
struct MA17 igma_3 = { .ax = { 1, 2, 3 } }; |
| 28 |
struct MA17 igma2_[2]; |
| 29 |
|
| 30 |
#define S36 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 31 |
|
| 32 |
#define S(N) (S36 + sizeof (S36) - N - 1) |
| 33 |
|
| 34 |
/* In the test macro, prevent the strcpy to memcpy transformation |
| 35 |
by using a local array initialized with the string literal. Without |
| 36 |
it, GCC transforms the strcpy call with memcpy which (unfortunately) |
| 37 |
permits accesses that cross subobject boundaries. */ |
| 38 |
#define T(dst, n) \ |
| 39 |
do { \ |
| 40 |
const char a[] = S36; \ |
| 41 |
strcpy (dst, a + sizeof a - n - 1); \ |
| 42 |
sink (dst); \ |
| 43 |
} while (0) |
| 44 |
|
| 45 |
void strcpy_global (void) |
| 46 |
{ |
| 47 |
T (gma.a1, 0); |
| 48 |
T (gma.a1, 1); // { dg-warning "'strcpy' offset 5 from the object at 'gma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 4" } |
| 49 |
T (gma.a1, 4); // { dg-warning "'strcpy' offset \\\[5, 8] from the object at 'gma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 4" } |
| 50 |
|
| 51 |
T (gma.a2, 1); |
| 52 |
T (gma.a2, 2); // { dg-warning "'strcpy' offset 7 from the object at 'gma' is out of the bounds of referenced subobject 'a2' with type 'char\\\[2]' at offset 5" } |
| 53 |
|
| 54 |
T (gma.a3, 2); |
| 55 |
T (gma.a3, 3); // { dg-warning "'strcpy' offset 10 from the object at 'gma' is out of the bounds of referenced subobject 'a3' with type 'char\\\[3]' at offset 7" } |
| 56 |
|
| 57 |
T (gma.a4, 3); |
| 58 |
T (gma.a4, 4); // { dg-warning "'strcpy' offset 14 from the object at 'gma' is out of the bounds of referenced subobject 'a4' with type 'char\\\[4]' at offset 10" } |
| 59 |
|
| 60 |
T (gma.a5, 4); |
| 61 |
T (gma.a5, 5); // { dg-warning "'strcpy' offset 19 from the object at 'gma' is out of the bounds of referenced subobject 'a5' with type 'char\\\[5]' at offset 14" } |
| 62 |
|
| 63 |
SA (__builtin_offsetof (struct MA17, a17) == 140); |
| 64 |
|
| 65 |
T (gma.a17, 16); |
| 66 |
T (gma.a17, 17); // { dg-warning "'strcpy' offset 157 from the object at 'gma' is out of the bounds of referenced subobject 'a17' with type 'char\\\[17]' at offset 140" } |
| 67 |
|
| 68 |
SA (__builtin_offsetof (struct MA17, ax) == 157); |
| 69 |
// GCC allows static initialization of flexible array members of |
| 70 |
// non-local objects. Verify that writing into one that may be |
| 71 |
// initialized in another translation unit isn't diagnosed. */ |
| 72 |
T (gma.ax, 0); // { dg-bogus "\\\[-Warray-bounds" } |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
void strcpy_global_array (void) |
| 77 |
{ |
| 78 |
T (gma2[0].a1, 0); |
| 79 |
T (gma2[0].a1, 1); // { dg-warning "'strcpy' offset 5 from the object at 'gma2' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 4" } |
| 80 |
T (gma2[0].a1, 4); // { dg-warning "'strcpy' offset \\\[5, 8] from the object at 'gma2' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 4" } |
| 81 |
|
| 82 |
T (gma2[0].a2, 1); |
| 83 |
T (gma2[0].a2, 2); // { dg-warning "'strcpy' offset 7 from the object at 'gma2' is out of the bounds of referenced subobject 'a2' with type 'char\\\[2]' at offset 5" } |
| 84 |
|
| 85 |
T (gma2[0].a3, 2); |
| 86 |
T (gma2[0].a3, 3); // { dg-warning "'strcpy' offset 10 from the object at 'gma2' is out of the bounds of referenced subobject 'a3' with type 'char\\\[3]' at offset 7" } |
| 87 |
|
| 88 |
T (gma2[0].a4, 3); |
| 89 |
T (gma2[0].a4, 4); // { dg-warning "'strcpy' offset 14 from the object at 'gma2' is out of the bounds of referenced subobject 'a4' with type 'char\\\[4]' at offset 10" } |
| 90 |
|
| 91 |
T (gma2[0].a5, 4); |
| 92 |
T (gma2[0].a5, 5); // { dg-warning "'strcpy' offset 19 from the object at 'gma2' is out of the bounds of referenced subobject 'a5' with type 'char\\\[5]' at offset 14" } |
| 93 |
|
| 94 |
T (gma2[0].a17, 16); |
| 95 |
T (gma2[0].a17, 17); // { dg-warning "'strcpy' offset 157 from the object at 'gma2' is out of the bounds of referenced subobject 'a17' with type 'char\\\[17]' at offset 140" } |
| 96 |
|
| 97 |
/* GMA2 is external but because it's an array its definition in another |
| 98 |
translation unit may not provide an initializer for the flexible array |
| 99 |
member. Verify that a warning is issued for access to it. */ |
| 100 |
T (gma2[0].ax, 1); // { dg-warning "'strcpy' offset \\\[157, 158] from the object at 'gma2' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 157" } |
| 101 |
T (gma2[0].ax, 7); // { dg-warning "'strcpy' offset \\\[157, 164] from the object at 'gma2' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 157" } |
| 102 |
|
| 103 |
/* IGMA2_ is internal and provides no definition for the flexible array |
| 104 |
member. Verify that a warning is issued for out-of-bounds accesses |
| 105 |
to it. */ |
| 106 |
T (igma2_[0].ax, 1); // { dg-warning "'strcpy' offset \\\[157, 158] from the object at 'igma2_' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 157" } |
| 107 |
|
| 108 |
T (igma_3.ax, 0); |
| 109 |
T (igma_3.ax, 1); |
| 110 |
T (igma_3.ax, 1); |
| 111 |
T (igma_3.ax, 3); // { dg-warning " offset 160 " } |
| 112 |
T (igma_3.ax, 9); // { dg-warning " offset \\\[160, 166] " } |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
void strcpy_local (void) |
| 117 |
{ |
| 118 |
struct MA17 lma; |
| 119 |
|
| 120 |
T (lma.a1, 0); |
| 121 |
T (lma.a1, 1); // { dg-warning "'strcpy' offset 5 from the object at 'lma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 4" } |
| 122 |
T (lma.a1, 4); // { dg-warning "'strcpy' offset \\\[5, 8] from the object at 'lma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 4" } |
| 123 |
|
| 124 |
T (lma.a2, 1); |
| 125 |
T (lma.a2, 2); // { dg-warning "'strcpy' offset 7 from the object at 'lma' is out of the bounds of referenced subobject 'a2' with type 'char\\\[2]' at offset 5" } |
| 126 |
|
| 127 |
T (lma.a3, 2); |
| 128 |
T (lma.a3, 3); // { dg-warning "'strcpy' offset 10 from the object at 'lma' is out of the bounds of referenced subobject 'a3' with type 'char\\\[3]' at offset 7" } |
| 129 |
|
| 130 |
T (lma.a4, 3); |
| 131 |
T (lma.a4, 4); // { dg-warning "'strcpy' offset 14 from the object at 'lma' is out of the bounds of referenced subobject 'a4' with type 'char\\\[4]' at offset 10" } |
| 132 |
|
| 133 |
T (lma.a5, 4); |
| 134 |
T (lma.a5, 5); // { dg-warning "'strcpy' offset 19 from the object at 'lma' is out of the bounds of referenced subobject 'a5' with type 'char\\\[5]' at offset 14" } |
| 135 |
|
| 136 |
T (lma.a17, 16); |
| 137 |
T (lma.a17, 17); // { dg-warning "'strcpy' offset 157 from the object at 'lma' is out of the bounds of referenced subobject 'a17' with type 'char\\\[17]' at offset 140" } |
| 138 |
|
| 139 |
T (lma.ax, 0); // { dg-warning "'strcpy' offset 157 from the object at 'lma' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 157" } |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
void strcpy_ref (struct MA17 *pma) |
| 144 |
{ |
| 145 |
T (pma->a1, 0); |
| 146 |
T (pma->a1, 1); // { dg-warning "'strcpy' offset 5 from the object at 'pma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 4" } |
| 147 |
T (pma->a1, 4); // { dg-warning "'strcpy' offset \\\[5, 8] from the object at 'pma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 4" } |
| 148 |
|
| 149 |
T (pma->a2, 1); |
| 150 |
T (pma->a2, 2); // { dg-warning "'strcpy' offset 7 from the object at 'pma' is out of the bounds of referenced subobject 'a2' with type 'char\\\[2]' at offset 5" } |
| 151 |
|
| 152 |
T (pma->a3, 2); |
| 153 |
T (pma->a3, 3); // { dg-warning "'strcpy' offset 10 from the object at 'pma' is out of the bounds of referenced subobject 'a3' with type 'char\\\[3]' at offset 7" } |
| 154 |
|
| 155 |
T (pma->a4, 3); |
| 156 |
T (pma->a4, 4); // { dg-warning "'strcpy' offset 14 from the object at 'pma' is out of the bounds of referenced subobject 'a4' with type 'char\\\[4]' at offset 10" } |
| 157 |
|
| 158 |
T (pma->a5, 4); |
| 159 |
T (pma->a5, 5); // { dg-warning "'strcpy' offset 19 from the object at 'pma' is out of the bounds of referenced subobject 'a5' with type 'char\\\[5]' at offset 14" } |
| 160 |
|
| 161 |
T (pma->a17, 16); |
| 162 |
T (pma->a17, 17); // { dg-warning "'strcpy' offset 157 from the object at 'pma' is out of the bounds of referenced subobject 'a17' with type 'char\\\[17]' at offset 140" } |
| 163 |
|
| 164 |
T (pma->ax, 0); |
| 165 |
T ((*pma).ax, 8); |
| 166 |
T (pma[0].ax, 9); |
| 167 |
|
| 168 |
SA (__builtin_offsetof (struct MA17, a1) == 4 |
| 169 |
&& sizeof (struct MA17) == 157); |
| 170 |
|
| 171 |
T (pma[1].a1, 0); |
| 172 |
T (pma[1].a1, 1); // { dg-warning "'strcpy' offset 162 from the object at 'pma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 161" } |
| 173 |
T (pma[1].a1, 4); // { dg-warning "'strcpy' offset \\\[162, 165] from the object at 'pma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset 161" } |
| 174 |
|
| 175 |
T (pma[1].a2, 1); |
| 176 |
T (pma[1].a2, 2); // { dg-warning "'strcpy' offset 164 from the object at 'pma' is out of the bounds of referenced subobject 'a2' with type 'char\\\[2]' at offset 162" } |
| 177 |
|
| 178 |
T (pma[1].a3, 2); |
| 179 |
T (pma[1].a3, 3); // { dg-warning "'strcpy' offset 167 from the object at 'pma' is out of the bounds of referenced subobject 'a3' with type 'char\\\[3]' at offset 164" } |
| 180 |
|
| 181 |
T (pma[1].a4, 3); |
| 182 |
T (pma[1].a4, 4); // { dg-warning "'strcpy' offset 171 from the object at 'pma' is out of the bounds of referenced subobject 'a4' with type 'char\\\[4]' at offset 167" } |
| 183 |
|
| 184 |
T (pma[1].a5, 4); |
| 185 |
T (pma[1].a5, 5); // { dg-warning "'strcpy' offset 176 from the object at 'pma' is out of the bounds of referenced subobject 'a5' with type 'char\\\[5]' at offset 171" } |
| 186 |
|
| 187 |
T (pma[1].a17, 16); |
| 188 |
T (pma[1].a17, 17); // { dg-warning "'strcpy' offset 314 from the object at 'pma' is out of the bounds of referenced subobject 'a17' with type 'char\\\[17]' at offset 297" } |
| 189 |
|
| 190 |
/* Since PMA points to an array of structs, accessing the flexible |
| 191 |
member of any of the elements of the array except for the last one |
| 192 |
would necessarily access a part of the next element of the enclosing |
| 193 |
array. The warning assumes that PMA doesn't point to the last element |
| 194 |
of the array which could in theory have nonzero elements without |
| 195 |
overlapping other objects. */ |
| 196 |
T (pma[1].ax, 0); // { dg-warning "'strcpy' offset 314 from the object at 'pma' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 314" } |
| 197 |
T ((pma + 1)->ax, 1); // { dg-warning "'strcpy' offset \\\[314, 315] from the object at 'pma' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 314" } |
| 198 |
T ((pma + 1)[1].ax, 2); // { dg-warning "'strcpy' offset \\\[471, 473] from the object at 'pma' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 471" } |
| 199 |
T ((*(pma + 2)).ax, 2); // { dg-warning "'strcpy' offset \\\[471, 473] from the object at 'pma' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 471" } |
| 200 |
T (pma[3].ax, 9); // { dg-warning "'strcpy' offset \\\[628, 637] from the object at 'pma' is out of the bounds of referenced subobject 'ax' with type 'char\\\[]' at offset 628" } |
| 201 |
|
| 202 |
T (pma[-1].a1, 0); |
| 203 |
T (pma[-1].a1, 1); // { dg-warning "'strcpy' offset -152 from the object at 'pma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset -153" } |
| 204 |
T (pma[-1].a1, 4); // { dg-warning "'strcpy' offset \\\[-152, -149] from the object at 'pma' is out of the bounds of referenced subobject 'a1' with type 'char\\\[1]' at offset -153" } |
| 205 |
} |
| 206 |
|
| 207 |
struct MA3 |
| 208 |
{ |
| 209 |
char a4[4]; // { dg-message "'a4' declared here" } |
| 210 |
char a3[3]; // { dg-message "'a3' declared here" } |
| 211 |
char c; |
| 212 |
}; |
| 213 |
|
| 214 |
void strcpy_ref_note (struct MA17 *pma, struct MA3 *pma3) |
| 215 |
{ |
| 216 |
T (pma3[-1].a4, 0); |
| 217 |
T (pma3[-1].a4, 1); |
| 218 |
T (pma3[-1].a4, 2); |
| 219 |
T (pma3[-1].a4, 3); |
| 220 |
T (pma3[-1].a4, 4); // { dg-warning "'strcpy' offset -4 from the object at 'pma3' is out of the bounds of referenced subobject 'a4' with type 'char\\\[4]' at offset -8" } |
| 221 |
T (pma3[-1].a4, 5); // { dg-warning "'strcpy' offset \\\[-4, -3] from the object at 'pma3' is out of the bounds of referenced subobject 'a4' with type 'char\\\[4]' at offset -8" } |
| 222 |
|
| 223 |
T (pma3[-1].a3, 0); |
| 224 |
T (pma3[-1].a3, 1); |
| 225 |
T (pma3[-1].a3, 2); |
| 226 |
T (pma3[-1].a3, 3); // { dg-warning "'strcpy' offset -1 from the object at 'pma3' is out of the bounds of referenced subobject 'a3' with type 'char\\\[3]' at offset -4" } |
| 227 |
T (pma3[-1].a3, 4); // { dg-warning "'strcpy' offset \\\[-1, 0] from the object at 'pma3' is out of the bounds of referenced subobject 'a3' with type 'char\\\[3]' at offset -4" } |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
void strncpy_vla_member (unsigned n) |
| 232 |
{ |
| 233 |
struct VarLenStruct { |
| 234 |
char a4[4], an[n], bn[n]; |
| 235 |
} x; |
| 236 |
|
| 237 |
sink (&x); |
| 238 |
|
| 239 |
strncpy (x.bn, x.a4, sizeof x.bn); |
| 240 |
sink (&x); |
| 241 |
|
| 242 |
strncpy (x.a4, x.bn, sizeof x.a4); |
| 243 |
x.a4[sizeof x.a4 - 1] = '\0'; |
| 244 |
sink (&x); |
| 245 |
|
| 246 |
strncpy (x.a4, x.bn, n); |
| 247 |
sink (&x); |
| 248 |
|
| 249 |
strncpy (x.an, x.bn, sizeof x.bn); /* { dg-bogus "\\\[-Warray-bounds" } */ |
| 250 |
sink (&x); |
| 251 |
} |