C++ Logo

std-discussion

Advanced search

Re: UB in P2641 'Checking if a union alternative is active'

From: Brian Bi <bbi5291_at_[hidden]>
Date: Mon, 19 Jun 2023 19:41:35 -0400
On Mon, Jun 19, 2023 at 3:28 PM Jens Maurer via Std-Discussion <
std-discussion_at_[hidden]> wrote:

>
>
> On 19/06/2023 19.59, Barry Revzin via Std-Discussion wrote:
> > Given:
> >
> > int main() {
> > union U { int i; char c; };
> > U u{.i=1};
> >
> > char c1 = (char&)u;
> > char c2 = (char&)u.i;
> > char c3 = u.c;
> > return c1 + c2 + c3;
> > }
> >
> > c1, c2, and c3 are obviously equivalent here. If there's wording to
> suggest otherwise, that's a wording defect.
>
> Note that this uses the "object representation" and aliasing special-cases
> for "char";
> unfortunately, there are some specification holes in that area.
>
> Jens
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>

To elaborate on what Jens said,

It is a known issue that the current wording of the standard does not
technically permit a user to implement their own `memcpy`, because there is
no provision in the language to be able to read a byte of an object
representation through a char glvalue; see P1839
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1839r5.pdf> for
discussion. The intent is to fix this wording gap eventually, but it's not
so easy.

It is assumed that we will find some kind of solution to this problem, in
which the naive strategy of `reinterpret_cast`ing to `char*` and then
reading through that pointer will actually be well-defined and yield the
object representation, and whatever the resolution is, it will be
considered as a DR.

If that's the case, then you can use a `char` glvalue to read the object
representation of a `U` object, notwithstanding the fact that the active
member is something other than a `char`. In the meantime, you can rely on
this not being UB for all practical purposes, even though the wording to
make it well-defined has not been figured out yet.

-- 
*Brian Bi*

Received on 2023-06-19 23:41:50