Skip to content

__builtin_object_size(P->M, 1) where M is an array and the last member of a struct fails (need "-fstrict-flex-array") #55741

Open
@kees

Description

@kees

When bos1 is used on a member who is both an array and at the end of a structure, it fails to correctly resolve. This kind of behavior should only happen for flexible array members:

struct middle_array {
    int a;
    unsigned char c[16];
    int b;
};

struct trailing_array {
    int a;
    int b;
    unsigned char c[16];
};

struct flex_array {
    int a;
    int b;
    unsigned char c[];
};

Both "middle" and "trailing" should see that "c" is 16 bytes. Only "flex" should be "unbounded":

ok:  sizeof(*middle) == 24
ok:  sizeof(middle->c) == 16
ok:  __builtin_object_size(middle, 1) == -1
ok:  __builtin_object_size(middle->c, 1) == 16
ok:  sizeof(*flex) == 8
ok:  __builtin_object_size(flex, 1) == -1
ok:  __builtin_object_size(flex->c, 1) == -1
ok:  sizeof(*trailing) == 24
ok:  sizeof(trailing->c) == 16
ok:  __builtin_object_size(trailing, 1) == -1
WAT: __builtin_object_size(trailing->c, 1) == -1 (expected 16)

https://godbolt.org/z/s9nb4Y7q4

This is likely due to trailing all trailing arrays as historical flexible arrays, but it breaks FORTIFY_SOURCE in that any struct with a fixed size trailing array will receive no sanity checking. Please introduce something like "-fstrict-flex-array".

Metadata

Metadata

Labels

clang:driver'clang' and 'clang++' user-facing binaries. Not 'clang-cl'

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions