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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org

--- Comment #4 from Martin Sebor <msebor at gcc dot gnu.org> ---
The optimization aside, the code in the test violates the C constraint that:

When two pointers are subtracted, both shall point to elements of the same
array object, or one past the last element of the array object; ...

Since s.f and s.b and distinct arrays (with s.b being treated as an array of
one element), the behavior of the test case is undefined.  GCC should diagnose
it before some optimization relies on code not doing these kinds of bad things
(as we have seen recently in bug 86259).

A valid (and more straightforward) way of writing the same code is:

  struct S
  {
    char a, b, f[3];
  } s;

  long i = offsetof (struct S, f) - offsetof (struct S, b);

or (for the purposes of testing):

  long i = ((char*)&s + offsetof (struct S, f)) - (char*)&s + offsetof (struct
S, b));

Reply via email to