Thread overview
[Issue 5270] New: Using a scope delegate allows memory corruption in safe mode
Jan 24, 2012
Walter Bright
Jan 24, 2012
timon.gehr@gmx.ch
November 24, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5270

           Summary: Using a scope delegate allows memory corruption in
                    safe mode
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid, spec
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bugzilla@kyllingen.net


--- Comment #0 from Lars T. Kyllingstad <bugzilla@kyllingen.net> 2010-11-24 09:04:42 PST ---
The following program compiles and runs without error.  Memory corruption happens in bar() because the context for the delegate stored in globalDg never gets copied to the heap, due to the 'scope' storage class being used in call().


@safe:

    void delegate() globalDg;
    void call(scope void delegate() @safe dg)
    {
        dg();

        // Don't tell anyone, but I'm saving this for later ;)
        globalDg = dg;
    }


    void foo()
    {
        int i;
        void izero() { i = 0; }
        call(&izero);
        assert (i == 0);
    }


    void bar()
    {
        int x = 123;

        // Simply calling some function cannot possibly
        // do anything to x...
        globalDg();

        // ...or can it?
        assert (x == 0);
    }


    void main()
    {
        foo();
        bar();
    }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 24, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=5270


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |WORKSFORME


--- Comment #1 from Walter Bright <bugzilla@digitalmars.com> 2012-01-23 23:48:02 PST ---
This now compiles & runs successfully.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 24, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=5270


timon.gehr@gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
                 CC|                            |timon.gehr@gmx.ch
         Resolution|WORKSFORME                  |


--- Comment #2 from timon.gehr@gmx.ch 2012-01-24 07:38:16 PST ---
The issue is that "it compiles and runs without error". (the second assertion asserts that there is memory corruption) The compiler has to either:

- enforce the 'scope' storage class in @safe mode by flow-analysis. - not perform the scope delegate optimization in @safe mode.

Change the second assertion to 'assert (x == 123);' to see the error.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------