aliasing expressions and identifiers

Nick Treleaven via Digitalmars-d digitalmars-d at puremagic.com
Mon May 30 07:18:38 PDT 2016


On Monday, 30 May 2016 at 10:55:57 UTC, Marc Schütz wrote:
> On Sunday, 29 May 2016 at 14:27:51 UTC, Nick Treleaven wrote:
>> What about if the RCArray (of ref count 1) is assigned to a 
>> different one after the local ref is initialised? That is what 
>> we're discussing -it's your example above(!)
>
> Exactly, and then one of the two suggested approaches will have 
> to be used to prevent the use-after-free. But that's something 
> that needs to happen on assignment, not when the reference is 
> created.

Well my solution does work, disallowing the problematic local 
refs. But it wouldn't be great for generic code or consistency 
with the @rc DIP. For the latter, we can add a temporary RC 
object to keep the referenced memory alive, but we only need to 
do this when the local ref is initialized from a function both 
(1) returning ref and (2) with a parameter marked return.

>> I think we should just prevent front from escaping.
>
> It doesn't necessarily need to escape for the problem to occur. 
> Well, it does in this example, but it can be trivially 
> rewritten:
>
>     auto tmp = stdin.byLine;
>     auto lines = tmp.array;
>
> Here, `lines` contains references to the buffer owned by `tmp`, 
> but doesn't escape (assuming `array` takes its argument by 
> `scope` or however the final solution will look like).

tmp and stdin.byLine are of type ByLine, whose front could be 
scope/return to prevent escaping. Above array() does escape 
ByLine.front so can't mark its argument with scope - the compiler 
would error.



More information about the Digitalmars-d mailing list