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.
Comment #1 by slavo5150 — 2019-08-22T13:54:21Z
If narrowed this issue down to this line in the DMD source code: https://github.com/dlang/dmd/blob/0459e34e20a0631a021550431dfe2411ebe16d3f/src/dmd/escape.d#L1465 When `Foo` is a template, `tf.isscope` is `true`, but when `Foo` is not a template `tf.isscope` is `false`. `isscope` is set to *true` at https://github.com/dlang/dmd/blob/0459e34e20a0631a021550431dfe2411ebe16d3f/src/dmd/semantic3.d#L1316 It is set to *true` because `funcdecl.vthis.storage_class` is `STC.maybescope`. That's as far as I got.
Comment #2 by slavo5150 — 2019-08-23T01:35:42Z
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.
Comment #3 by slavo5150 — 2019-08-23T06:55:41Z
Comment #4 by dlang-bot — 2019-09-01T08:14:48Z
dlang/dmd pull request #10334 "Fix Issue 19965 - [DIP1000] Template allows to escape internal pointer" was merged into master: - e5cc7453b4173d9408ad1efd115f74c720caaa4b by Mike: Fix Issue 19965 - [DIP1000] Template allows to escape internal pointer https://github.com/dlang/dmd/pull/10334