https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111030

            Bug ID: 111030
           Summary: tree-object-size: incorrect sub-object size for VLA
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: qinzhao at gcc dot gnu.org
  Target Milestone: ---

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)

Reply via email to