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
Comment #1 by bugzilla — 2018-03-12T06:36:40Z
Comment #2 by github-bugzilla — 2018-03-14T09:14:34Z
Commits pushed to master at https://github.com/dlang/dlang.org https://github.com/dlang/dlang.org/commit/e84d1e91ebaaaa6572b7cf81f01a486c7a54484c fix Issue 18568 - partially overlapping assignments have undefined behavior but are accepted in @safe code https://github.com/dlang/dlang.org/commit/39c2d41f81584c16613b18847e375a275212d7df Merge pull request #2271 from WalterBright/fix18568 fix Issue 18568 - partially overlapping assignments have undefined be… merged-on-behalf-of: Petar Kirov <[email protected]>