Skip to content

Non-constant array index within offsetof should be diagnosed as an extension #156264

@Halalaluyafail3

Description

@Halalaluyafail3

The following program is accepted by Clang even with -pedantic-errors in both C and C++:

#include<stddef.h>
int main(){//when compiling in C before C23 add void between the parentheses
    int x=2;
    struct y{
        int z[4];
    };
    offsetof(struct y,z[x]);
}

This program shouldn't be accepted with -pedantic-errors since it violates the following text:

The macros are

...

offsetof(type, member-designator)

which expands to an integer constant expression that has type size_t, the
value of which is the offset in bytes, to the subobject (designated by
member-designator), from the beginning of any object of type type. The type
and member designator shall be such that given

static type t;

then the expression &(t.member-designator) evaluates to an address constant.
If the specified type name contains a comma not between matching parentheses
or if the specified member is a bit-field, the behavior is undefined.

Section 7.21 "Common definitions <stddef.h>" Paragraph 4 ISO/IEC 9899:2024

Strictly, because the behavior is undefined not diagnosing the program is correct. However, diagnosing it with -pedantic doesn't seem that difficult so I think it should be done.

Related GCC bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121736

CC @sdkrystian

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions