[Issue 22680] @safe hole with destructors

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jan 17 12:41:36 UTC 2022


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

ag0aep6g <ag0aep6g at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |safe
                 CC|                            |ag0aep6g at gmail.com
           Severity|enhancement                 |minor

--- Comment #2 from ag0aep6g <ag0aep6g at gmail.com> ---
(In reply to RazvanN from comment #1)
> There is nothing unsafe in assigning a class reference to another.

Unless you're assigning garbage, which is happening here. A more elaborate
demonstration of the unsafety:

----
import std.stdio: writeln;
import core.memory: GC;
C c;
class C
{
    immutable int* ip;
    this(int x) @safe { this.ip = new int(x); }
    ~this() @safe { c = this; }
}
void main() @safe
{
    () { new C(42); } ();
    () { ubyte[1000] clear_stack; } ();
    () @trusted { GC.collect(); } ();
    immutable int* ip = c.ip;
    writeln(*ip); /* Prints "42". */
    new int(13);
    int should_still_be_42 = *ip;
    writeln(should_still_be_42); /* Prints "13" - immutable data has changed.
*/
}
----

--


More information about the Digitalmars-d-bugs mailing list