www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18597] New: more unsafe unaligned pointer errors


          Issue ID: 18597
           Summary: more unsafe unaligned pointer errors
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: bugzilla digitalmars.com

ag0aep6g gmail.com writes:

//////////////////////////////// test.d ////////////////////////////////

struct Victim
    bool alive = true;
    ~this() { alive = false; }

struct Unaligned
    ubyte filler;
    Victim* p;

pragma(msg, Unaligned.sizeof);

void main()
    enum N = 100;

    Unaligned[N] hosts;

    foreach (n; 0..N)
        version (original) hosts[n].p = new Victim;
        else version (variation1) hosts[n] = Unaligned(0, new Victim);
        else version (variation2)
            Unaligned u = { p: new Victim };
            hosts[n] = u;
        else static assert(false);

    // Unaligned.p is invisible to the GC due to alignment

    void trustedCollect()  trusted { import core.memory; GC.collect(); }

    foreach (n; 0..N)
        assert(hosts[n].p.alive); // Dangling pointer!

These should all fail with the same error:

    dmd -version=original test.d
    dmd -version=variation1 test.d
    dmd -version=variation2 test.d

The different versions effectively do the same thing, just with different

Mar 12 2018