https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896
--- Comment #40 from qinzhao at gcc dot gnu.org ---
I had an initial patch to support the element_count attribute and use this
attribute in builtin_dynamic_object_size(), for the following testing case:
1 #include <stdlib.h>
2 #include <assert.h>
3 #ifdef ENABLE_ELEMENT_COUNT_ATTRIBUTE
4 int k;
5 struct P {
6 int k;
7 int x[] __attribute__ ((element_count ("k")));
8 } *p;
9 #else
10 struct P {
11 int k;
12 int x[];
13 } *p;
14 #endif
15
16 void store(int a, int b)
17 {
18 p = (struct P *)malloc (sizeof (struct P) + a * sizeof (int));
19 p->k = a;
20 p->x[b] = 0;
21 assert (__builtin_dynamic_object_size (p->x, 1) == (p->k * sizeof
(int)));
22 assert (__builtin_dynamic_object_size (p, 0) == (p->k * sizeof (int) +
sizeof (struct P)));
23 return;
24 }
25
26 int main()
27 {
28 store(7, 7);
29 p->x[8] = 1;
30 #ifdef ENABLE_ELEMENT_COUNT_ATTRIBUTE
31 assert (__builtin_dynamic_object_size (p->x, 1) == (p->k * sizeof (int)));
32 assert (__builtin_dynamic_object_size (p, 0) == (p->k * sizeof (int) +
sizeof (struct P)));
33 #endif
34 }
when compiled with my gcc as:
opc@qinzhao-ol8u3-x86 108896]$ sh t
/home/opc/Install/latest-d/bin/gcc -O -DENABLE_ELEMENT_COUNT_ATTRIBUTE t.c
a.out: t.c:32: main: Assertion `__builtin_dynamic_object_size (p, 0) == (p->k *
sizeof (int) + sizeof (struct P))' failed.
t: line 21: 3266310 Aborted (core dumped) ./a.out
with this private gcc, the __builtin_dynamic_object_size (p->x,1) at line 31
can be correctly computed based on the attribute.
However, due to PR109557, the __builtin_dynamic_object_size (p, 0) at line 32
cannot be computed.
Due to bug PR109557, the assertion at line 32 failed.