Bug 17423 – pointer assignment to `this` in member function is not accounted for
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-05-23T23:25:59Z
Last change time
2018-06-27T01:55:17Z
Keywords
accepts-invalid, safe
Assigned to
No Owner
Creator
Eyal
Comments
Comment #0 by eyal — 2017-05-23T23:25:59Z
@safe:
unittest {
struct OpApply {
int delegate(int) @safe myDlg;
int opApply(int delegate(int) @safe dlg) {
myDlg = dlg;
return 0;
}
}
struct Foo {
OpApply o;
int i;
this(int x) {
o = OpApply();
foreach(_; o) {
i = 0;
}
i = x;
}
int call(int delegate(int) @safe dlg) {
dlg(0);
return i;
}
}
auto foo1 = Foo(1);
auto foo2 = Foo(2);
import std.stdio;
writeln(foo2.call(foo1.o.myDlg));
}
Crashes with a seg-fault.
This is apparently due to abuse of opApply, letting the delegate escape its scope, being considered @safe ?
Comment #1 by eyal — 2017-05-23T23:31:51Z
I think foreach on delegates (opApply or delegates) should require the delegate parameter to be marked "scope" since it really must not escape.
Comment #2 by dlang-bugzilla — 2017-05-24T12:43:34Z
Wow, that's pretty evil :)
opApply is showing its age here.
Comment #3 by bugzilla — 2017-05-29T04:47:58Z
When I compile and run it, it prints:
2
and does not crash. What compiler/system are you using?
Comment #4 by eyal — 2017-05-29T05:49:37Z
dmd --version && dmd -unittest -main -run test17423.d
DMD64 D Compiler v2.074.0
Copyright (c) 1999-2017 by Digital Mars written by Walter Bright
/tmp/dmd_rundz8iAQ(_D4core7runtime18runModuleUnitTestsUZ19unittestSegvHandlerUNbiPS4core3sys5posix6signal9siginfo_tPvZv+0x38)[0x443804]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x11390)[0x7f76b6ce1390]
Error: program killed by signal 11