<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/110385>110385</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
__builtin_dynamic_object_size() fails to return correct size depending on depth of flexible array
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
kees
</td>
</tr>
</table>
<pre>
As seen in the Linux kernel:
- https://lore.kernel.org/all/202409170436.C3C6E7F7A@keescook/
- https://lore.kernel.org/all/CAGG=3QVWCQB-3sM=iwgTmX8zrU81H+F_A1icJwROvW_DSvsBeA@mail.gmail.com
we're having erroneous size reporting from `__builtin_dynamic_object_size()`, where the depth of dereference for the flexible array causes a 0 size report:
```
https://godbolt.org/z/qohGd5xh1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
struct variable {
int a;
int b;
int length;
short array[] __attribute__((counted_by(length)));
};
struct bucket {
int a;
struct variable *growable;
int b;
};
int main(int argc, char *argv[])
{
struct bucket *p;
struct variable *v;
p = malloc(sizeof(*p));
v = malloc(sizeof(*p->growable) + sizeof(*p->growable->array) * 32);
v->length = 32;
printf("%zu\n", __builtin_dynamic_object_size(v->array, 1));
p->growable = v;
printf("%zu\n", __builtin_dynamic_object_size(p->growable->array, 1));
return 0;
}
GCC shows 64 64, but Clang shows 64 0.
```
cc @isanbard @bwendling
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVV1z6yYQ_TX4hYlHBn0-6MGWo3Q67XRuv27fNAitJBoMLiA5ya_vICm148RtOlejwZhd9pw9oF1mregUQI6iHYr2Kza4Xpv8EcCuat0851uLLYDCQmHXA_5BqOEJP4JRIBHdomCPgmW8w71zR-tXSYlIKbWB9ey51qZDpGRSIlKSgIRBtkmCkMbrghbxfVImWxQGHpRr_ei3_6-IxfbhAdE9_fL71-LL7o7aHxHdi1P36-GP9MX8lm6-Q2RXVtu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