Bug 18568 – partially overlapping assignments have undefined behavior but are accepted in @safe code
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-03-07T14:58:47Z
Last change time
2018-03-14T09:14:34Z
Keywords
safe
Assigned to
No Owner
Creator
ag0aep6g
Comments
Comment #0 by ag0aep6g — 2018-03-07T14:58:47Z
Prompted by this forum post:
https://forum.dlang.org/post/[email protected]
On assignments, the spec says [1]:
> Undefined Behavior:
> 1. if the lvalue and rvalue have partially overlapping storage
> 2. if the lvalue and rvalue's storage overlaps exactly but the types are
> different
But DMD accepts this:
----
struct S
{
union
{
int i;
byte b;
float f;
struct
{
byte b2;
align(1) int i2;
}
}
}
void main() @safe
{
S s;
s.i = s.b; /* Partially overlapping, different types. */
s.f = s.i; /* Exactly overlapping, different types. */
s.i = s.i2; /* Partially overlapping, same type. */
}
----
According to the spec, all those assignments have undefined behavior. So they shouldn't be allowed in @safe code.
(As always, this can be fixed by letting DMD reject the code, or by changing the spec to give the code defined behavior.)
[1] https://dlang.org/spec/expression.html#assign_expressions