Bug 11151 – Undetected overlapping initialization

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-10-01T09:38:00Z
Last change time
2013-10-05T00:34:25Z
Keywords
accepts-invalid, pull, wrong-code
Assigned to
nobody
Creator
k.hara.pg

Comments

Comment #0 by k.hara.pg — 2013-10-01T09:38:11Z
The union field U.a and U.y are overlapped but compiler doesn't detect the issue. import core.stdc.stdio : printf; union U { struct { align(1) long a; align(1) int b; } struct { align(1) int x; align(1) long y; } } void main() { pragma(msg, U.a.offsetof); // 0 pragma(msg, U.b.offsetof); // 8 pragma(msg, U.x.offsetof); // 0 pragma(msg, U.y.offsetof); // 4 U u = {a:1, y:2}; // overlapped initializing U.a and U.y printf("u.a = %lld\n", u.a); // 8589934593 , Wrong! printf("u.b = %d\n", u.b); // 0 printf("u.x = %d\n", u.x); // 1 printf("u.y = %lld\n", u.y); // 2 }
Comment #1 by k.hara.pg — 2013-10-01T09:50:15Z
Comment #2 by github-bugzilla — 2013-10-05T00:33:02Z