| 1 |
/* PR 101832: |
| 2 |
GCC extension accepts the case when a struct with a C99 flexible array |
| 3 |
member is embedded into another struct (possibly recursively). |
| 4 |
__builtin_object_size will treat such struct as flexible size. |
| 5 |
However, when a structure with non-C99 flexible array member, i.e, trailing |
| 6 |
[0], [1], or [4], is embedded into anther struct, the stucture will not |
| 7 |
be treated as flexible size. */ |
| 8 |
/* { dg-do run } */ |
| 9 |
/* { dg-options "-O2" } */ |
| 10 |
|
| 11 |
#include "builtin-object-size-common.h" |
| 12 |
|
| 13 |
#define expect(p, _v) do { \ |
| 14 |
size_t v = _v; \ |
| 15 |
if (p == v) \ |
| 16 |
__builtin_printf ("ok: %s == %zd\n", #p, p); \ |
| 17 |
else {\ |
| 18 |
__builtin_printf ("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ |
| 19 |
FAIL (); \ |
| 20 |
} \ |
| 21 |
} while (0); |
| 22 |
|
| 23 |
|
| 24 |
struct A { |
| 25 |
int n; |
| 26 |
char data[]; |
| 27 |
}; |
| 28 |
|
| 29 |
struct B { |
| 30 |
int m; |
| 31 |
struct A a; |
| 32 |
}; |
| 33 |
|
| 34 |
struct C { |
| 35 |
int q; |
| 36 |
struct B b; |
| 37 |
}; |
| 38 |
|
| 39 |
struct A0 { |
| 40 |
int n; |
| 41 |
char data[0]; |
| 42 |
}; |
| 43 |
|
| 44 |
struct B0 { |
| 45 |
int m; |
| 46 |
struct A0 a; |
| 47 |
}; |
| 48 |
|
| 49 |
struct C0 { |
| 50 |
int q; |
| 51 |
struct B0 b; |
| 52 |
}; |
| 53 |
|
| 54 |
struct A1 { |
| 55 |
int n; |
| 56 |
char data[1]; |
| 57 |
}; |
| 58 |
|
| 59 |
struct B1 { |
| 60 |
int m; |
| 61 |
struct A1 a; |
| 62 |
}; |
| 63 |
|
| 64 |
struct C1 { |
| 65 |
int q; |
| 66 |
struct B1 b; |
| 67 |
}; |
| 68 |
|
| 69 |
struct An { |
| 70 |
int n; |
| 71 |
char data[8]; |
| 72 |
}; |
| 73 |
|
| 74 |
struct Bn { |
| 75 |
int m; |
| 76 |
struct An a; |
| 77 |
}; |
| 78 |
|
| 79 |
struct Cn { |
| 80 |
int q; |
| 81 |
struct Bn b; |
| 82 |
}; |
| 83 |
|
| 84 |
volatile void *magic1, *magic2; |
| 85 |
|
| 86 |
int main (int argc, char *argv[]) |
| 87 |
{ |
| 88 |
struct B *outer; |
| 89 |
struct C *outest; |
| 90 |
|
| 91 |
/* Make sure optimization can't find some other object size. */ |
| 92 |
outer = (void *)magic1; |
| 93 |
outest = (void *)magic2; |
| 94 |
|
| 95 |
expect (__builtin_object_size (&outer->a, 1), -1); |
| 96 |
expect (__builtin_object_size (&outest->b, 1), -1); |
| 97 |
expect (__builtin_object_size (&outest->b.a, 1), -1); |
| 98 |
|
| 99 |
struct B0 *outer0; |
| 100 |
struct C0 *outest0; |
| 101 |
|
| 102 |
/* Make sure optimization can't find some other object size. */ |
| 103 |
outer0 = (void *)magic1; |
| 104 |
outest0 = (void *)magic2; |
| 105 |
|
| 106 |
expect (__builtin_object_size (&outer0->a, 1), sizeof (outer0->a)); |
| 107 |
expect (__builtin_object_size (&outest0->b, 1), sizeof (outest0->b)); |
| 108 |
expect (__builtin_object_size (&outest0->b.a, 1), sizeof (outest0->b.a)); |
| 109 |
|
| 110 |
struct B1 *outer1; |
| 111 |
struct C1 *outest1; |
| 112 |
|
| 113 |
/* Make sure optimization can't find some other object size. */ |
| 114 |
outer1 = (void *)magic1; |
| 115 |
outest1 = (void *)magic2; |
| 116 |
|
| 117 |
expect (__builtin_object_size (&outer1->a, 1), sizeof (outer1->a)); |
| 118 |
expect (__builtin_object_size (&outest1->b, 1), sizeof (outest1->b)); |
| 119 |
expect (__builtin_object_size (&outest1->b.a, 1), sizeof (outest1->b.a)); |
| 120 |
|
| 121 |
struct Bn *outern; |
| 122 |
struct Cn *outestn; |
| 123 |
|
| 124 |
/* Make sure optimization can't find some other object size. */ |
| 125 |
outern = (void *)magic1; |
| 126 |
outestn = (void *)magic2; |
| 127 |
|
| 128 |
expect (__builtin_object_size (&outern->a, 1), sizeof (outern->a)); |
| 129 |
expect (__builtin_object_size (&outestn->b, 1), sizeof (outestn->b)); |
| 130 |
expect (__builtin_object_size (&outestn->b.a, 1), sizeof (outestn->b.a)); |
| 131 |
|
| 132 |
DONE (); |
| 133 |
return 0; |
| 134 |
} |