[Issue 9537] auto ref returns a reference its own stack

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Nov 23 14:13:14 PST 2013


https://d.puremagic.com/issues/show_bug.cgi?id=9537



--- Comment #3 from Zach the Mystic <reachzach at gmail.com> 2013-11-23 14:13:10 PST ---
(In reply to comment #1)
> import std.typecons;
> ref foo(T)(T t)
> {
>     return t[0];
> }
> 
> void main()
> {
>     int* p = &foo(tuple(1, 2));
> }
> 
> 
> The bug is escaping a reference to a parameter.

Specifically, a parameter which is on the local stack. If "tuple(1,2)" were
replaced with a heap-allocated or global parameter, or even something further
down on the stack, it's address would still technically be safe to take.

It's the ref safety issue. The last consensus I knew about was that the
compiler is supposed to do a best-effort static guess as to where the return
value is located. In cases where it's too hard to know for sure, it's supposed
to put a runtime assert (toggled off by -noboundscheck) to make sure the
returned reference isn't on the dangerous part of the stack. The ability to use
the PIMPL idiom in D ensures that there will always be some cases where the
compiler can't detect the safety of the returned reference without a runtime
check.

I don't know how far implementation of this has gotten.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list