Bug 19965 – [DIP1000] Template allows to escape internal pointer
Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-06-14T12:12:17Z
Last change time
2019-09-04T13:16:02Z
Keywords
safe
Assigned to
No Owner
Creator
Jacob Carlborg
Comments
Comment #0 by doob — 2019-06-14T12:12:17Z
The following code compiles successfully with DIP1000 enabled:
struct Buffer
{
int[10] data;
int[] getData() @safe return
{
return data[];
}
}
struct Foo()
{
Buffer buffer;
int[] toArray() @safe return
{
return buffer.getData;
}
}
int[] a;
void main() @safe
{
Foo!() f;
a = f.toArray;
}
In the above example, a pointer to `data` (through the dynamic array returned by `getData`) is escaped to `a`. If `Foo` is not a template the code fails to compile, as expected.
The problem appears to be with `scope` inference. In `escape.d` there are many places where `STC.maybescope` is removed after it has been determined that `scope` cannot be inferred. There is even a function `notMaybeScope` to do this.
I believe all that needs to be done is to call `notMaybeScope(v)` in the right place and for the right conditions so `scope` is no longer inferred for this scenario.