Frank Heckenbach wrote:
I noticed an incorrect handling of string parameters in a few RTS routines which might cause incorrect results when reading from strings where the actual parameters are expressions (not simple variables).
`ReadStr' and `Val' might be affected, though I haven't been able to reproduce an actual problem. The rarely used `StringTFDD_Reset' is affected, that's where I found the problem. (fjf1099.pas)
It looks that the problem is deeper. The following program fails both with and without your patch:
program rstr1(Output); type ii = integer value 0; tip = ^ii; var ipv1 : tip; iv2 : integer; s : string(20);
function ip1: tip; var tmp : tip; begin s := 'dead beef'; ip1 := ipv1; end; begin s:='666 123'; new(ipv1); readstr(s, ip1^, iv2); if (ipv1^ = 666) and (iv2 = 123) then writeln('OK') end .
AFAICS if the first parameter to `readstr' is not a plain variable, or if any other parameter may have side effects, then we need to copy the string parameter. When passing string expressions as const parameters we create a temparary variable, but is is not clear if the temparary will live long enough (in fjf1099.pas the temporary is destroyed too early, but I do not see why other similar cases work).