Bug 17370 – [scope] Escaping scope pointers possible via struct GC allocation
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-05-05T08:33:00Z
Last change time
2017-10-16T09:57:27Z
Keywords
safe
Assigned to
No Owner
Creator
Mathias Lang
Comments
Comment #0 by mathias.lang — 2017-05-05T08:33:00Z
The following compiles and runs with 2.074.0 and v2.075.0-devel-5cfc8d982 (latest master):
```
void main () @safe
{
int* ptr = fwd();
assert(ptr !is null);
}
int* fwd () @safe
{
int i;
return new Struct(&i).oops; // Leaving out `new` correctly detects escaping
}
struct Struct
{
int* oops;
}
```
Comment #1 by petar.p.kirov — 2017-08-22T14:40:13Z
The issue is with only with the auto generated constructor. If you write one yourself, you get:
void main () @safe
{
int* ptr = fwd();
assert(ptr !is null);
}
int* fwd () @safe
{
int i;
return new Struct(&i).oops;
}
struct Struct
{
int* oops;
this(int* p) @safe { oops = p; }
}
scope_bug17370.d(10): Error: reference to local variable i assigned to non-scope parameter p calling scope_bug17370.Struct.this