www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19521] New: safe typesafe_variadic_functions could cause

https://issues.dlang.org/show_bug.cgi?id=19521

          Issue ID: 19521
           Summary:  safe typesafe_variadic_functions could cause memory
                    corruption
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: wyrlon gmx.net

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;
}

--
Dec 29 2018