Skip to content

__builtin_object_size does not match structure size when flexible array members are statically initialized #62789

@kees

Description

@kees

Given a structure ending in a flexible array, that has a static initializer:

struct foo {
    size_t count;
    int some[];
};

static struct foo instance = {
    .count = 3,
    .some = { 5, 10, 15, },
};

Clang reports the incorrect size with __builtin_object_size().

GCC correctly reports 20:

__builtin_object_size(&instance, 1) == 20

Clang misses the flexible array members and reports only 8 (the sizeof()):

__builtin_object_size(&instance, 1) == 8

https://godbolt.org/z/W5soYdrdK

cc @nickdesaulniers @isanbard

Metadata

Metadata

Labels

clang:frontendLanguage frontend issues, e.g. anything involving "Sema"confirmedVerified by a second party

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions