On 07/07/2011 11:43, Mattias Gaertner wrote:

You don't need the future. You can already create a crash using const shortstring:


Indeed, thanks for the example.

And with this example there is another potential issue arising from not honouring the contract of const.

The behaviour of such code may change at random depending on fpc-version or optimization. Future versions of fpc could cahce the result of "h<>''" (as this expression is constant too), and re-use it in the 2nd IF. So mis-honouring the rules of how to use "const" does not only lead to potential crashes, but it also leads to code that has no defined behaviour, and may lead to completely random results.

This means the current behaviour with ansistring is absolutely right. It is only a consequence of the behaviour already observed with other types.



{$mode objfpc}{$H+}
uses Classes;
var
  s: shortstring = '';
  o: TStringList = nil;

procedure DoSomething(const h: shortstring);
begin
  if h<>'' then
    o:=TStringList.Create; // not called, because s=h=''
  s:='A';
  if h<>'' then
    writeln(o.Count); // called, because now s=h='A' => crash
end;

begin
  DoSomething(s);
end.

>[...]


Mattias


_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel

_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply via email to