[Issue 20809] return statement might access memory from destructed temporary

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri May 8 17:12:23 UTC 2020


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

--- Comment #3 from hsteoh at quickfur.ath.cx ---
Here, let me do it for you:

------
import std.stdio;
@safe:
struct S
{
    @safe:
    int a;
    ~this()
    {
        a = 0;
    }

    ref val()
    {
        return a;
    }
}

S bar()
{
    return S(2);
}

int foo()
{
    return bar.val; // The return value of val is saved locally as ref int and
then the destructor of S is called (set the local cache to 0). Now the ref
value is dereferenced and returned
}

void main()
{
    assert(foo() == 2); // foo() is 0
}
------

Just please keep in mind next time to post the entire body of code in the bug
so that it will never get lost in the future.

--


More information about the Digitalmars-d-bugs mailing list