Thread overview
[Issue 5541] New: Disallow escaping of references to stack-allocated memory
Feb 01, 2012
yebblies
Feb 09, 2012
timon.gehr@gmx.ch
Jan 17, 2013
yebblies
February 07, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5541

           Summary: Disallow escaping of references to stack-allocated
                    memory
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2011-02-07 13:05:29 PST ---
This is D2 code:


struct Foo {
    int x;
}
Foo* bar() {
    Foo f = Foo(1);
    return &f;
}
void main() {}


DMD 2.051 raises a compile-time error:
test.d(6): Error: escaping reference to local f


But this code compiles and runs with no errors with DMD 2.051. I think DMD has to statically disallow code like this too:


struct Foo {
    int x;
}
Foo* bar() {
    return &(Foo(1));
}
void main() {}

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


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies@gmail.com
         Resolution|                            |INVALID


--- Comment #1 from yebblies <yebblies@gmail.com> 2012-02-01 15:25:08 EST ---
(In reply to comment #0)
> But this code compiles and runs with no errors with DMD 2.051. I think DMD has to statically disallow code like this too:
> 
> 
> struct Foo {
>     int x;
> }
> Foo* bar() {
>     return &(Foo(1));
> }
> void main() {}

Believe it or not, this is valid code.  In D struct literals are currently lvalues, and not allocated on the stack at all.  See issue 5889.

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


timon.gehr@gmx.ch changed:

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


--- Comment #2 from timon.gehr@gmx.ch 2012-02-09 15:21:53 PST ---
This is not about struct literals being lvalues, Walter states this is by
design anyway.
Furthermore, struct literals are certainly allocated on the stack and what
bearophile shows is not valid code.

struct Foo {
    int x;
}
Foo* bar() {
    return &(Foo(1));
}
void qux(ref Foo f,int x){
    writeln(f.x);
}
void main() {
    Foo* x = bar();
    qux(*bar(),2);
}

compiling and running for 32 bit prints garbage, and looking at the disassembly shows that the literal is indeed allocated on the stack.

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


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |WORKSFORME


--- Comment #3 from yebblies <yebblies@gmail.com> 2013-01-17 12:36:55 EST ---
None of this compiles any more with 2.062 head

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