| 1 |
/* Test the attribute counted_by and its usage in |
| 2 |
* __builtin_dynamic_object_size: when the type of the flexible array member |
| 3 |
* is casting to another type. */ |
| 4 |
/* { dg-do run } */ |
| 5 |
/* { dg-options "-O2" } */ |
| 6 |
|
| 7 |
#include "builtin-object-size-common.h" |
| 8 |
|
| 9 |
typedef unsigned short u16; |
| 10 |
|
| 11 |
struct info { |
| 12 |
u16 data_len; |
| 13 |
char data[] __attribute__((counted_by(data_len))); |
| 14 |
}; |
| 15 |
|
| 16 |
struct foo { |
| 17 |
int a; |
| 18 |
int b; |
| 19 |
}; |
| 20 |
|
| 21 |
static __attribute__((__noinline__)) |
| 22 |
struct info *setup () |
| 23 |
{ |
| 24 |
struct info *p; |
| 25 |
size_t bytes = 3 * sizeof(struct foo); |
| 26 |
|
| 27 |
p = (struct info *)malloc (sizeof (struct info) + bytes); |
| 28 |
p->data_len = bytes; |
| 29 |
|
| 30 |
return p; |
| 31 |
} |
| 32 |
|
| 33 |
static void |
| 34 |
__attribute__((__noinline__)) report (struct info *p) |
| 35 |
{ |
| 36 |
struct foo *bar = (struct foo *)p->data; |
| 37 |
EXPECT(__builtin_dynamic_object_size((char *)(bar + 1), 1), 16); |
| 38 |
EXPECT(__builtin_dynamic_object_size((char *)(bar + 2), 1), 8); |
| 39 |
} |
| 40 |
|
| 41 |
int main(int argc, char *argv[]) |
| 42 |
{ |
| 43 |
struct info *p = setup(); |
| 44 |
report(p); |
| 45 |
return 0; |
| 46 |
} |