D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 18597 - more unsafe unaligned pointer errors
Summary: more unsafe unaligned pointer errors
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: safe
Depends on:
Blocks:
 
Reported: 2018-03-12 09:59 UTC by Walter Bright
Modified: 2018-03-23 03:40 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Walter Bright 2018-03-12 09:59:05 UTC
ag0aep6g@gmail.com writes:

//////////////////////////////// test.d ////////////////////////////////
@safe:

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

align(1)
struct Unaligned
{
align(1):
    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);
        assert(hosts[n].p.alive);
    }

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

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

    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 syntax.
Comment 1 ag0aep6g 2018-03-12 10:07:19 UTC
(In reply to Walter Bright from comment #0)
> ag0aep6g@[...].com writes:

Please don't give away people's email addresses like that. It doesn't matter too much for me, because that's my D address, but generally it's not a nice thing to do.
Comment 2 Walter Bright 2018-03-13 06:10:47 UTC
(In reply to ag0aep6g from comment #1)
> (In reply to Walter Bright from comment #0)
> > ag0aep6g@[...].com writes:
> 
> Please don't give away people's email addresses like that. It doesn't matter
> too much for me, because that's my D address, but generally it's not a nice
> thing to do.

Sorry about that, but I just copy pasted from the public page:

  https://issues.dlang.org/show_bug.cgi?id=15399

I.e. your email address appears with every comment you write, including the one this is in reply to. You might want to check your bugzilla profile.
Comment 3 Walter Bright 2018-03-13 07:40:38 UTC
https://github.com/dlang/dmd/pull/8019
Comment 4 ag0aep6g 2018-03-13 11:22:37 UTC
(In reply to Walter Bright from comment #2)
> (In reply to ag0aep6g from comment #1)
[...]
> Sorry about that, but I just copy pasted from the public page:
[...]
> I.e. your email address appears with every comment you write, including the
> one this is in reply to. You might want to check your bugzilla profile.

Huh. I thought I had set the name properly. Sorry for lashing out.

Bugzilla didn't already make it public, though. It's smart enough to remove the domain part in replies and on truly public pages (i.e. when you're not logged in).

But then again, <https://forum.dlang.org/group/issues> shows all Bugzilla activity to the public, including everyone's addresses. So that's that.
Comment 5 github-bugzilla 2018-03-23 03:40:32 UTC
Commits pushed to master at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/3afbfb2c1b6328c18bdfd1f122091a74e22f9d69
fix Issue 18597 - more unsafe unaligned pointer errors

https://github.com/dlang/dmd/commit/a0764d9ea0e7b3132d0a362b2f32a6e74e6e76e0
Merge pull request #8019 from WalterBright/fix18597

fix Issue 18597 - more unsafe unaligned pointer errors
merged-on-behalf-of: Mike Franklin <JinShil@users.noreply.github.com>