[Issue 17504] Passing templated varargs to array varargs base class constructor segfaults

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Jun 14 10:13:07 PDT 2017


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

ag0aep6g at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |ag0aep6g at gmail.com
         Resolution|---                         |INVALID

--- Comment #1 from ag0aep6g at gmail.com ---
(In reply to Marco de Wild from comment #0)
> this(MyType[] objs...)
> {
>     _objs = objs;
> } 

That's invalid code. The elements of `objs` are on the stack. `objs` is only
valid during the constructor call. Later, `_objs` will contain garbage
pointers. Dereferencing them then leads to a segfault.

The spec says:

"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

I'm closing this as invalid. Just reopen if I'm missing something.

However, the code should be rejected when you make the constructor `@safe`. It
isn't. There's already an open issue for that: issue 5212.

--


More information about the Digitalmars-d-bugs mailing list