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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Nov 18 10:46:51 UTC 2021


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

--- Comment #10 from moonlightsentinel at disroot.org ---
(In reply to Dennis from comment #8)
> This is important for making Phobos's tempCString @safe:
> 
> ```
>     // $(RED WARNING): $(RED Incorrect usage)
>     auto pInvalid1 = str.tempCString().ptr;
>     const char* pInvalid2 = str.tempCString();
>     // Both pointers refer to invalid memory here as
>     // returned values aren't assigned to a variable and
>     // both primary expressions are ended.
> ```


That usage won't be affected by fixing this bug? The issue in the example
arises because the pointer is extracted due to implicit conversion while the
temporary buffer is destructed at the end of the statement.

That could only work if the temporary was kept alive over multiple statements
(which breaks other language rules w.r.t. temporaries)

--


More information about the Digitalmars-d-bugs mailing list