Bug 15996 – @safe allows escaping of ptrs to variables going out of scope

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-05-06T07:52:00Z
Last change time
2017-05-01T08:10:06Z
Keywords
accepts-invalid, pull, safe
Assigned to
nobody
Creator
eyal.lotem

Attachments

IDFilenameSummaryContent-TypeSize
1595crash.dReproduces the undefined behaviorapplication/x-dsrc216
1596simplified.dAn even simpler reproduction of the bugapplication/x-dsrc206

Comments

Comment #0 by eyal.lotem — 2016-05-06T07:52:54Z
Created attachment 1595 Reproduces the undefined behavior This code compiles and produces Undefined Behavior: @safe: import std.stdio; struct T { int y; } auto foo() { int *x; T t; t.y = 12345; x = &t.y; return x; } unittest { auto x = foo(); writeln("Hello world"); assert(*x == 12345); } It seems that the escape analysis checks if the pointed element is itself directly declared on the stack, instead of checking whether it is contained in something that is declared on the stack.
Comment #1 by eyal.lotem — 2016-05-06T07:53:56Z
Please ignore the attachment, it is a less simplified version of the reproduction.
Comment #2 by eyal.lotem — 2016-05-06T07:56:11Z
Created attachment 1596 An even simpler reproduction of the bug
Comment #3 by bugzilla — 2016-06-07T07:32:58Z
The attachment: --------------------------- safe: import std.stdio; struct T { int y; } auto foo() { auto t = T(12345); auto x = &t.y; return x; } unittest { auto x = foo(); writeln("Hello world"); assert(*x == 12345); }
Comment #4 by bugzilla — 2016-08-25T07:14:31Z
(In reply to Walter Bright from comment #3) > The attachment: > --------------------------- > > safe: > > import std.stdio; > > struct T { int y; } > > auto foo() { > auto t = T(12345); > auto x = &t.y; > return x; // line 10 > } > > unittest { > auto x = foo(); > writeln("Hello world"); > assert(*x == 12345); > } With: https://github.com/dlang/dmd/pull/5972 now yields: test3.d(10): Error: scope variable x may not be returned so this is resolved once 5972 is pulled.
Comment #5 by nick — 2017-05-01T08:10:06Z
Fixed as per comment 4, dmd 2.074 with -dip1000.