-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Description
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 givenstatic 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