current __builtin_dynamic_object_size cannot handle VLA correctly for the sub-object size, please see the following testing case: #include <stdio.h> #include <stddef.h> #define expect(p, _v) do { \ size_t v = _v; \ if (p == v) \ __builtin_printf ("ok: %s == %zd\n", #p, p); \ else \ { \ __builtin_printf ("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ } \ } while (0); #define noinline __attribute__((__noinline__)) static void noinline bar (int index) { struct annotated { long foo; char b; char array[index]; long c; } q, *p; p = &q; expect (__builtin_dynamic_object_size(p->array, 0), sizeof (struct annotated) - offsetof (struct annotated, array[0])); expect (__builtin_dynamic_object_size(p->array, 1), offsetof (struct annotated, array[index]) - offsetof (struct annotated, array[0])); return; } int main () { bar (10); return 0; } when compiled with the latest gcc and run: /home/opc/Install/latest-d/bin/gcc -O t.c ok: __builtin_dynamic_object_size(p->array, 0) == 23 WAT: __builtin_dynamic_object_size(p->array, 1) == 23 (expected 10)
For completeness, the ML discussion was https://inbox.sourceware.org/gcc-patches/34DCF245-5E66-4FC3-B817-E3C205EB5484@oracle.com.