Bug 23145 – Stack allocation of scope new variables defeats @safe

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-05-29T00:55:18Z
Last change time
2023-02-09T07:29:14Z
Keywords
pull, safe, wrong-code
Assigned to
No Owner
Creator
timon.gehr

Comments

Comment #0 by timon.gehr — 2022-05-29T00:55:18Z
DMD v2.100.0 Compiles even with -dip1000: --- class D{ C c; } class C { D d; int x=3; this(D d)@safe @nogc{ d.c=this; this.d=d; } } C foo(D d)@nogc @safe{ scope c = new C(d); // remove `scope` and program does not crash return c.d.c; // escape c } void main(){ import std.stdio; writeln(foo(new D)); // segfault } ---
Comment #1 by timon.gehr — 2022-05-29T00:58:12Z
Probably the fix for this is to only stack-allocate if the constructor is `scope`.
Comment #2 by bugzilla — 2022-05-29T05:59:53Z
Marking the C.this constructor as `scope` does indeed detect the error.
Comment #3 by dlang-bot — 2022-05-29T23:10:59Z
@WalterBright created dlang/dmd pull request #14175 "fix Issue 23145 - Stack allocation of scope new variables defeats @safe" fixing this issue: - fix Issue 23145 - Stack allocation of scope new variables defeats @safe https://github.com/dlang/dmd/pull/14175
Comment #4 by dlang-bot — 2023-02-09T07:29:14Z
dlang/dmd pull request #14175 "fix Issue 23145 - Stack allocation of scope new variables defeats @safe" was merged into master: - 03027d60f883fbfb1644b86db6b5fc60d7f46a33 by Walter Bright: fix Issue 23145 - Stack allocation of scope new variables defeats @safe https://github.com/dlang/dmd/pull/14175