www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5270] New: Using a scope delegate allows memory corruption in safe mode

reply d-bugmail puremagic.com writes:
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



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: -------
Nov 24 2010
next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Jan 23 2012
prev sibling parent d-bugmail puremagic.com writes:
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                  |



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: -------
Jan 24 2012