Thread overview
[Issue 17318] Delegates allow escaping reference to stack variable
Jan 20, 2018
Carsten Blüggel
Mar 03, 2018
Walter Bright
Mar 11, 2018
Walter Bright
April 10, 2017
https://issues.dlang.org/show_bug.cgi?id=17318

hsteoh@quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid

--
January 20, 2018
https://issues.dlang.org/show_bug.cgi?id=17318

Carsten Blüggel <chilli@posteo.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |chilli@posteo.net

--- Comment #1 from Carsten Blüggel <chilli@posteo.net> ---
DMD64 D Compiler v2.078.0 and
LDC - the LLVM D compiler (1.7.0):   based on DMD v2.077.1 and LLVM 5.0.1:

I can't reproduce the issue reported; for me, Your code prints:
&s.x = 0x7ffd86c2a118
&x = 0x7ffd86c2a118

Equal addresses, independent from with/without -dip1000, didn't need to comment out anything.

--
January 21, 2018
https://issues.dlang.org/show_bug.cgi?id=17318

--- Comment #2 from hsteoh@quickfur.ath.cx ---
It could be because the compiler now is smarter about constructing return values in-place rather than moving the struct afterwards.

Nevertheless, the actual problem still persists: the delegate is closing over a local variable that resides on the stack. This should have been prevented by -dip1000, but it doesn't.

Here's slightly more elaborate example that exposes the bug on latest dmd git master:
---------
import std.stdio;

struct S {
        int x;
        void delegate() dg;
        this(int dummy) {
                dg = () {
                        writefln("&x = %#x", &x);
                        x++;
                };
        }
}

auto makeS() {
        auto s = S(1);
        return s.dg; // <-- N.B. escaping reference to s
}

void smoke(void delegate() dg) {
        int z = 789;

        writeln(z);
        dg();
        writeln(z);
}

void main() {
        auto dg = makeS();
        smoke(dg);
}
---------

--
January 21, 2018
https://issues.dlang.org/show_bug.cgi?id=17318

--- Comment #3 from hsteoh@quickfur.ath.cx ---
P.S. On my computer, the value of z changes after the call to dg(), which is
wrong because z is a local variable in smoke() that the delegate should not be
able to access.

--
March 03, 2018
https://issues.dlang.org/show_bug.cgi?id=17318

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |safe
                 CC|                            |bugzilla@digitalmars.com

--
March 11, 2018
https://issues.dlang.org/show_bug.cgi?id=17318

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #4 from Walter Bright <bugzilla@digitalmars.com> ---


*** This issue has been marked as a duplicate of issue 18576 ***

--