[Issue 21561] Unsafe aliasing of immutable union member allowed in @safe code

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jan 20 18:28:29 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=21561

--- Comment #2 from Paul Backus <snarwin+bugzilla at gmail.com> ---
Yes, because the data can still be modified by assigning a new value to the
whole union.

---
void oops()
{
    U a = { y: 0 };
    U b = { x: 1 };
    assert(a.y == 0);
    a = b;
    assert(a.y == 1);
}
---

Strictly speaking, the problem is that we are allowed to access a.y after its
lifetime has ended. However, this only leads to undefined behavior when unsafe
aliasing is involved, so the unsafe aliasing is what needs to be fixed.

--


More information about the Digitalmars-d-bugs mailing list