www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11044] New: Escaping references to lazy argument are allowed

http://d.puremagic.com/issues/show_bug.cgi?id=11044

           Summary: Escaping references to lazy argument are allowed
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: maxim maxim-fomin.ru


--- Comment #0 from Maxim Fomin <maxim maxim-fomin.ru> 2013-09-15 13:17:04 PDT
---
Currently lazy parameter implies creating delegate which references local
variable. Allowing to escape lazy argument without allocating local variable at
heap leads to leaked pointers to some stack zone which is a memory error.

extern(C) int printf(const char*,...)  safe;

auto foo(lazy double i)  safe
{
   return { return i; } ;
}

auto bar()  safe
{
   double i = 4.0;
   return foo(i);
}

void baz()  safe
{
   double[2] i = 3.14; // this value overwrites 4.0
                       // replace with char[16] val = 'f'; 
                       // to print deterministic garbage
}

void main()  safe
{
   auto x = bar();
   baz();
   printf("%f\n", x()); 
}

This prints 3.14 which is wrong. Potentially compiler can recognize that lazy
argument escapes and allocate i = 4.0 on heap, but due to separate compilation
model, it cannot do this in general case, so the code should be rejected.

This is a more complex variation of known 'ref-ref' bug which consists in
returning reference to local object which has gone out of scope:

ref int foo(ref int i) { return i; }
ref int bar() { int val; return foo(i); }

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