https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68727

            Bug ID: 68727
           Summary: [4.9 regression] invalid offsetof expressions accepted
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

While coming up with a comprehensive test case for c++/68711 I noticed that GCC
silently accepts other invalid offsetof expressions that then cause the
ill-formed program to crash at runtime (below).  Since this problem is not
caused by the merge of the C++ delayed folding branch or a regression from GCC
5.1, I raise it a bug of its own.  While strictly speaking, GCC's behavior
isn't non-conforming because the expressions have undefined behavior, since GCC
otherwise makes an effort to detect and diagnose such constructs and in the
past did diagnose these, this is a defect in the implementation.

$ /build/gcc-trunk-svn/gcc/xgcc -B /build/gcc-trunk-svn/gcc -Wall -Wextra
-Wpedantic x.cpp
tmp$ 
tmp$ cat x.cpp && /build/gcc-trunk-svn/gcc/xgcc -B /build/gcc-trunk-svn/gcc
-Wall -Wextra -Wpedantic x.cpp && ./a.out 
struct A { int i; };
struct B: virtual A { };

__SIZE_TYPE__ a[] = {
    !&((B*)0)->A::i,
    __builtin_offsetof (B, A::i)
};

int main ()
{
    return a[0] + a[1];
}
Segmentation fault (core dumped)
$ 

The absence of diagnostics on these expressions is a regression from 4.9.3
which issues the following warnings.  (It also successfully compiles the
program that then crashes at runtime).  

x.cpp:5:19: warning: invalid access to non-static data member ‘A::i’  of NULL
object [-Winvalid-offsetof]
     !&((B*)0)->A::i,
                   ^
x.cpp:5:19: warning: (perhaps the ‘offsetof’ macro was used incorrectly)
[-Winvalid-offsetof]
x.cpp:6:31: warning: invalid access to non-static data member ‘A::i’  of NULL
object [-Winvalid-offsetof]
     __builtin_offsetof (B, A::i)
                               ^
x.cpp:6:31: warning: (perhaps the ‘offsetof’ macro was used incorrectly)
[-Winvalid-offsetof]

Reply via email to