[fpc-devel] Capturing addresses

Marc Weustink marc at dommelstein.nl
Mon Nov 11 10:39:17 CET 2019


Jonas Maebe wrote:
> Hi,
> 
> Does anyone know what the accepted/excepted behaviour is regarding the
> capture of addresses of var/out/const-by-address/constref parameters?
> 
> For example:
> 
> var
>    g: longint;
>    p: plongint;
> 
> procedure test(var l: longint);
> begin
>    p:=@l;
> end;
> 
> begin
>    test(g);
> end.
> 
> After test() executes, p now contains the address of g (the '@' operator
> does not return the address of g's address on the stack; it returns the
> actual address of g). This means that g's address has been captured by
> test(). This can obviously lead to wrong/dangerous situations, e.g. if g
> was not a global variable, but a local variable of another procedure.
> That said, it is a legal expression.
> 
> I would like to teach the compiler to be able to assume that addresses
> of variables are not captured merely because they are passed by
> reference. There will also be a switch to toggle this assumption,
> because fortunately it the behaviour is completely defined by the callee
> side (so if you know that a unit does this, compiling that unit
> appropriately is sufficient to ensure it will always work correctly).
> 
> The question is: should the compiler by default assume that such
> addresses are not captured, or that they are captured? Does anyone know
> if a lot of code exists that does this?

I've a lot of (interfacing) code where the address of a var is used, 
where the resulting pointer used locally. I don't think I've code which 
stores that pointer somewhere outside the procedure.

Marc


More information about the fpc-devel mailing list