Skip to content

__builtin_dynamic_object_size() fails to return correct size depending on depth of flexible array #110385

@kees

Description

@kees

As seen in the Linux kernel:

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

Metadata

Metadata

Assignees

Labels

bugIndicates an unexpected problem or unintended behaviorclang:codegenIR generation bugs: mangling, exceptions, etc.

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions