Bug 8956 – Ability to break typesystem with constructor/postblit/destructor (e.g. modify immutable)

Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-11-03T06:47:00Z
Last change time
2013-07-10T08:29:36Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
verylonglogin.reg
Depends on
8958
Blocks
2573

Comments

Comment #0 by verylonglogin.reg — 2012-11-03T06:47:40Z
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.
Comment #1 by verylonglogin.reg — 2012-11-03T08:01:11Z
The first fix proposal: Issue 8958
Comment #2 by smjg — 2012-11-04T16:52:41Z
this(int* i) has no const/immutable qualifier. As such, if I remember correctly, the compiler should not be allowing it to be used to construct an immutable instance. In any case, it's a bug that it accepts the call to it to construct an immutable without having first checked that it's still valid when the this pointer is immutable.
Comment #3 by maxim — 2013-07-10T08:29:36Z
It was fixed in 2.063 (now ctor attributes should correspond to those in variable declarations).