| |
| ▲ | mtklein a day ago | parent | next [-] | | 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. | | |
| ▲ | jcranmer a day ago | parent | next [-] | | > 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. | | |
| ▲ | trealira a day ago | parent | next [-] | | > In C99, it was made expressly legal, but it was erroneously included in the list of undefined behavior annex. In C99, union type punning was put under Annex J.1, which is unspecified behavior, not undefined behavior. Unspecified behavior is basically implementation-defined behavior, except that the implementor is not required to document the behavior. | | |
| ▲ | ryao a day ago | parent [-] | | We can use UB to refer to both. :) | | |
| ▲ | hermitdev a day ago | parent | next [-] | | > We can use UB to refer to both. :) You can, but in the context of the standard, you'd be wrong to do so. Undefined behavior and unspecified behavior have specific, different, meanings in context of the C and C++ standards. Conflate them at your own peril. | |
| ▲ | trealira a day ago | parent | prev [-] | | Maybe, but we were talking about "undefined behavior," not "UB," so the point is moot. | | |
|
| |
| ▲ | ryao a day ago | parent | prev [-] | | The GCC developers disagree as of last December: > Type punning via unions is undefined behavior in both c and c++. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118141#c13 | | |
| |
| ▲ | jotux a day ago | parent | prev | next [-] | | Saw this recently and thought it was good: https://www.youtube.com/watch?v=NRV_bgN92DI | |
| ▲ | ryao a day ago | parent | prev [-] | | 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. | | |
| ▲ | trealira a day ago | parent | next [-] | | From a draft of the C23 standard, this is what it has to say about union type punning: > If the member used to read the contents of a union object is not the same as the member last used to store a value in the object the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called type punning). This might be a non-value representation. In past standards, it said "trap representation" rather than "non-value representation," but in none of them did it say that union type punning was undefined behavior. If you have a PDF of any standard or draft standard, just doing a search for "type punning" should direct you to this footnote quickly. So I'm going to say that if the GCC developer explicitly said that union type punning was undefined behavior in C, then they were wrong, because that's not what the C standard says. | | |
| ▲ | amboar a day ago | parent | next [-] | | Section J.1 _Unspecified_ behavior says > (11) The values of bytes that correspond to union members other than the one last stored into (6.2.6.1). So it's a little more constrained in the ramifications, but the outcomes may still be surprising. It's a bit unfortunate that "UB" aliases to both "Undefined behavior" and "Unspecified behavior" given they have subtly different definitions. From section 4 we have: > A program that is correct in all other aspects, operating on correct data, containing unspecified behavior shall be a correct program and act in accordance with 5.1.2.4. | |
| ▲ | ryao a day ago | parent | prev [-] | | Here is what was said: > Type punning via unions is undefined behavior in both c and c++. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118141#c13 Feel free to start a discussion on the GCC mailing list. | | |
| ▲ | trealira a day ago | parent [-] | | I actually might, although not now. Thanks for the link. I'm surprised he directly contradicted the C standard, rather than it just being a misunderstanding. | | |
| ▲ | ryao a day ago | parent [-] | | According to another comment, the C standard contradicts the C standard on this: https://news.ycombinator.com/item?id=43794268 Taking snippets of the C standard out of context of the whole seems to result in misunderstandings on this. | | |
| ▲ | trealira a day ago | parent [-] | | It doesn't. That commenter is saying that in C99, it was unspecified behavior. Since C11 onward, it's been removed from the unspecified behavior annex and type punning is allowed, though it may generate a trap/non-value representation. It was never undefined behavior, which is different. Edit: no, it's still in the unspecified behavior annex, that's my mistake. It's still not undefined, though. | | |
| ▲ | ryao a day ago | parent [-] | | Most of the C code I write is C99 code, so it is undefined behavior either way for me (if I care about compilers other than GCC and Clang). That said, I am going to defer to the GCC developers on this since I do not have time to make sense of all versions of the C standard. | | |
| ▲ | trealira a day ago | parent [-] | | That's fair. In the end, what matters is how C is implemented in practice on the platforms your code targets, not what the C standard says. |
|
|
|
|
|
| |
| ▲ | uecker a day ago | parent | prev | next [-] | | Union type punning is allowed and supported by GCC: https://godbolt.org/z/vd7h6vf5q | | |
| ▲ | ryao a day ago | parent [-] | | I said that GCC defines type punning via unions. It is an extension to the C standard that GCC did. That said, using “the code compiles in godbolt” as proof that it is not relying on what the standard specifies to be UB is fallacious. | | |
| ▲ | uecker a day ago | parent [-] | | I am a member of the standards committee and a GCC maintainer. The C standard supports union punning. (You are right though that relying on godbolt examples can be misleading.) |
|
| |
| ▲ | jotux a day ago | parent | prev [-] | | https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Typ... | | |
| ▲ | ryao a day ago | parent [-] | | What is your point? I already said that GCC defines it even though the C standard does not. As per the GCC developers: > Type punning via unions is undefined behavior in both c and c++. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118141#c13 | | |
| ▲ | jotux a day ago | parent [-] | | > 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 I just was citing the source of this for reference. | | |
|
|
|
| |
| ▲ | flohofwoe a day ago | parent | prev | next [-] | | > 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. | | |
| ▲ | ryao a day ago | parent [-] | | 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. | | |
| ▲ | jcranmer a day ago | parent [-] | | Footnote 107 in C23, on page 75 in §6.5.2.3: > If the member used to read the contents of a union object is not the same as the member last used to store a value in the object the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called type punning). This might be a non-value representation. (though this footnote has been present as far back as C99, albeit with different numbers as the standard has added more text in the intervening 24 years). | | |
| ▲ | ryao a day ago | parent [-] | | The GCC developers disagree with your interpretation: > Type punning via unions is undefined behavior in both c and c++. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118141#c13 | | |
| ▲ | nialv7 2 hours ago | parent | next [-] | | I wouldn't be surprised if Andrew Pinski was just wrong. It's anecdotal but my impression of him isn't very good. | |
| ▲ | flohofwoe a day ago | parent | prev [-] | | I'm not sure tbh what's there to 'interpret' or how a compiler developer could misread that, the wording is quite clear. | | |
| ▲ | ryao a day ago | parent [-] | | It is an excerpt being taken out of context. Of course it is quite clear. Taking it out of context ignores everything else that the standard says. That interpretation is wrong as far as compiler authors are concerned. | | |
| ▲ | trealira a day ago | parent [-] | | The context is that it's a footnote. The footnote is referenced in this paragraph: A postfix expression followed by the . operator and an identifier designates a member of a structure or union object. The value is that of the named member (106), and is an lvalue if the first expression is an lvalue. If the first expression has qualified type, the result has the so-qualified version of the type
of the designated member. 106) If the member used to read the contents of a union object is not the same as the member last used to store a value in the object the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called type punning). This might be a non-value representation. In that same document, union type punning is explicitly listed under Annex J.1, Unspecified Behavior: (11) The values of bytes that correspond to union members other than the one last stored into (6.2.6.1). The standard is extremely clear and explicit that it's not undefined behavior. | | |
| ▲ | ryao a day ago | parent [-] | | This is not considering the document as a whole. I will defer to the GCC developers on what the document means on this. | | |
| ▲ | jcranmer a day ago | parent | next [-] | | I am a member of the C standards committee, and I'm telling you you're wrong here. Martin Uecker is also member of the C standards committee, and has just responded to that bug saying that the comment you linked is wrong. I, and others here, have quoted literal standards text to you explaining why type punning through unions is well-defined behavior in C. I don't know who Andrew Pinski is, but they're factually incorrect regarding the legality of type punning via unions in C. | | |
| ▲ | uecker a day ago | parent [-] | | Andrew is a GCC developer who is very competent (much more than myself regarding GCC), but I think he was mistakenly assuming the C++ rules apply to C here as well. |
| |
| ▲ | trealira a day ago | parent | prev [-] | | I'm interested in hearing how considering the document as a whole leads to a different conclusion. |
|
|
|
|
|
|
|
| |
| ▲ | mat_epice a day ago | parent | prev [-] | | 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. | | |
| ▲ | fsmv a day ago | parent [-] | | 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. | | |
|
|