[Issue 17318] Delegates allow escaping reference to stack variable
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun Jan 21 00:57:54 UTC 2018
https://issues.dlang.org/show_bug.cgi?id=17318
--- Comment #2 from hsteoh at 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);
}
---------
--
More information about the Digitalmars-d-bugs
mailing list