[Issue 11151] New: Undetected overlapping initialization

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Oct 1 09:38:16 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=11151

           Summary: Undetected overlapping initialization
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid, wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: k.hara.pg at gmail.com


--- Comment #0 from Kenji Hara <k.hara.pg at gmail.com> 2013-10-01 09:38:11 PDT ---
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
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list