[Issue 8956] New: Ability to break typesystem with constructor/postblit/destructor (e.g. modify immutable)

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Nov 3 06:47:41 PDT 2012


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

           Summary: Ability to break typesystem with
                    constructor/postblit/destructor (e.g. modify
                    immutable)
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: critical
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: verylonglogin.reg at gmail.com


--- Comment #0 from Denis Shelomovskij <verylonglogin.reg at gmail.com> 2012-11-03 16:47:40 MSK ---
As was mentioned in Issue 4338 and Issue 4867 one is able to break typesystem
using postblit/destructor. But that's not all. Constructor also can be used for
this.

Example:
---
int* p1, p2, p3;

struct S
{
    int* p;

    this(int* p) { p1 = this.p = p; }
    this(this)   { p2 = p; }
    ~this()      { p3 = p; }
}

void main()
{
    immutable s = immutable S(new int); // call constructor
    { immutable tmp = s; } // call postblit and destructor
    assert(p1 is s.p && p2 is s.p && p3 is s.p);
}
---

The worst is that it can be done accidentally. E.g.:
---
struct S
{
    int* p;
    this(int* p) { this.p = p; } // Looks really innocuous, isn't it?
}

void main()
{
    int i;
    immutable s = immutable S(&i); // Feel constructor's destructive power!
    assert(&i is s.p);
}
---


Please, create enhancement requests as other issues and add e.g. `Depends on`
links here.

-- 
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