D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 17423 - pointer assignment to `this` in member function is not accounted for
Summary: pointer assignment to `this` in member function is not accounted for
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid, safe
Depends on:
Blocks:
 
Reported: 2017-05-23 23:25 UTC by Eyal
Modified: 2018-06-27 01:55 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Eyal 2017-05-23 23:25:59 UTC
@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 Eyal 2017-05-23 23:31:51 UTC
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 Vladimir Panteleev 2017-05-24 12:43:34 UTC
Wow, that's pretty evil :)

opApply is showing its age here.
Comment 3 Walter Bright 2017-05-29 04:47:58 UTC
When I compile and run it, it prints:

2

and does not crash. What compiler/system are you using?
Comment 4 Eyal 2017-05-29 05:49:37 UTC
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
Comment 5 Seb 2017-05-29 07:53:16 UTC
DPaste and I can confirm the segfault: https://dpaste.dzfl.pl/c678a3ce7bf2
Comment 6 Walter Bright 2018-03-11 08:37:13 UTC
The opApply is actually working correctly. The bug is that the dlg parameter was incorrectly inferred as `scope`.
Comment 7 Walter Bright 2018-03-11 08:37:27 UTC
https://github.com/dlang/dmd/pull/7999
Comment 8 Walter Bright 2018-03-11 20:51:11 UTC
(In reply to Walter Bright from comment #7)
> https://github.com/dlang/dmd/pull/7999

For this to work, -dip1000 must be used.
Comment 9 github-bugzilla 2018-03-18 00:57:51 UTC
Commits pushed to master at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/9fbf0611d2ef4174597470834cb6e356f791468a
fix Issue 17423 - pointer assignment to  in member function is not accounted for

https://github.com/dlang/dmd/commit/eee17da2ffd89093a2de2f7310ca01520cd49a64
Merge pull request #7999 from WalterBright/fix17423

fix Issue 17423 - pointer assignment to  in member function is not ac…
merged-on-behalf-of: Mike Franklin <JinShil@users.noreply.github.com>
Comment 10 Walter Bright 2018-06-27 01:55:17 UTC
This example actually works now, see https://github.com/dlang/dmd/pull/8408