Comment by ogoffart

2 days ago

> This is going to silently break so much existing code

The code was already broken. It was an undefined behavior.

That's a problem with C and it's undefined behavior minefields.

GCC has long been known to define undefined behavior in C unions. In particular, type punning in unions is undefined behavior under the C and C++ standards, but GCC (and Clang) define it.

  • I have always thought that punning through a union was legal in C but UB in C++, and that punning through incompatible pointer casting was UB in both.

    I am basing this entirely on memory and the wikipedia article on type punning. I welcome extremely pedantic feedback.

    • > punning through a union was legal in C

      In C89, it was implementation-defined. In C99, it was made expressly legal, but it was erroneously included in the list of undefined behavior annex. From C11 on, the annex was fixed.

      > but UB in C++

      C++11 adopted "unrestricted unions", which added a concept of active members that is UB to access other members unless you make them active. Except active members rely on constructors and destructors, which primitive types don't have, so the standard isn't particularly clear on what happens here. The current consensus is that it's UB.

      C++20 added std::bit_cast which is a much safer interface to type punning than unions.

      > punning through incompatible pointer casting was UB in both

      There is a general rule that accessing an object through an 'incompatible' lvalue is illegal in both languages. In general, changing the const or volatile qualifier on the object is legal, as is reading via a different signed or unsigned variant, and char pointers can read anything.

      8 replies →

    • There has been plenty of misinformation spread on that. One of the GCC developers told me explicitly that type punning through a union was UB in C, but defined by GCC when I asked (after I had a bug report closed due to UB). I could find the bug report if I look for it, but I would rather not do the search.

      15 replies →

  • > type punning in unions is undefined behavior under the C and C++ standards

    Union type punning is entirely valid in C, but UB in C++ (one of the surprisingly many subtle but still fundamental differences between C and C++). There's specifically a (somewhat obscure) footnote about this in the C standard, which also has been more clarified in one of the recent C standards.

    • There is no footnote about it in the C standard. Someone proposed adding one to standardize the behavior, but it was never accepted. Ever since then, people keep quoting it even though it is a rejected amendment.

      10 replies →

  • EDIT: This comment is wrong, see fsmv’s comment below. Leaving for posterity because I’m no coward!

    - - -

    Undefined behavior only means that the spec leaves a particular situation undefined and that the compiler implementor can do whatever they want. Every compiler defines undefined behavior, whether it’s documented (or easy to qualify, or deterministic) or not.

    It is in poor taste that gcc has had widely used, documented behaviors that are changing, especially in a point release.

    • I think you're confusing unspecified and undefined behavior. UB could do something randomly different every time and unspecified must chose an option.

      In a lot of cases in optimizing compilers they just assume UB doesn't exist. Yes technically the compiler does do something but there's still a big difference between the two.

      1 reply →

When you have a big system many people rely on you generally try to look for ways to keep their code working - not look for the changes you’re contractually allowed to make.

GCC probably has a better justification than “we are allowed to”.

  • > GCC probably has a better justification than “we are allowed to”.

    Maybe, but I've seen GCC people justify such changes with little more than "it's UB, we can change it, end of story", so I wouldn't assume it.

Undefined in the standard doesn't mean undefined in GCC. Type-punning through unions has always been a special case that GCC has taken care with beyond the standard.