Bug 19521 – @safe typesafe_variadic_functions could cause memory corruption

Status
RESOLVED
Resolution
WORKSFORME
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2018-12-29T13:29:42Z
Last change time
2023-04-25T13:56:46Z
Keywords
safe
Assigned to
No Owner
Creator
Daniel

Comments

Comment #0 by wyrlon — 2018-12-29T13:29:42Z
The below snippet runs and works fine, but likely only because the compiler doesn't do the optimisation which the spec explicitly allows. "An implementation may construct the object or array instance on the stack. Therefore, it is an error to refer to that instance after the variadic function has returned" https://dlang.org/spec/function.html#typesafe_variadic_functions There are many possible fixes 1) Allow DIP25 style return annotation even if there is no 'ref'. 2) Compilation error 3) Change spec to disallow optimisation void main() { auto x1 = fun(1); auto x2 = fun(2); import std.stdio; writeln(x1.a, x2.a); } @safe: class C { public: int a; this(int a) { this.a = a; } } C fun(/* return */ C c...) { return c; }
Comment #1 by razvan.nitu1305 — 2023-04-25T13:56:46Z
Compiling the code now yields: test.d(21): Deprecation: scope parameter `c` may not be returned By using the compiler switch -dip1000 you get: test.d(21): Error: scope parameter `c` may not be returned So this seems to have been fixed. Closing as WORKSFORME.