Skip to content

__builtin_object_size(P->M, 1) where M ends with a flex-array behaves like sizeof() #72032

@kees

Description

@kees

Using __builtin_object_size (and __builtin_dynamic_object_size) on a composite structure's member that has a flexible array loses the sense of how large it is.

For example, on a struct that has a flexible array, __bdos correctly says it doesn't know the size (bounded here by alloc_size):

    expect(__builtin_object_size(wrap->msg.nlmsg_content, 1), 4076);
    expect(__builtin_object_size(wrap, 0), 4096);
    expect(__builtin_object_size(wrap, 1), 4096);

But if it is part of a wrapper, it start behaving like sizeof():

    expect(__builtin_object_size(wrap->msg.nlmsg_content, 1), 4076);
    expect(__builtin_object_size(&wrap->msg, 0), 4092);
    /* But suddenly gets it wrong? */
    expect(__builtin_object_size(&wrap->msg, 1), 4092);

https://godbolt.org/z/YrGsh8Ybs

This was recently fixed in GCC:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101832

PoC:
composite.c.txt

Metadata

Metadata

Assignees

Labels

clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions