www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5541] New: Disallow escaping of references to stack-allocated memory

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



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: -------
Feb 07 2011
next sibling parent d-bugmail puremagic.com writes:
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




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



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: -------
Feb 09 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5541


yebblies <yebblies gmail.com> changed:

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



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: -------
Jan 16 2013