FormatFloat possible improvement lazarus / 64-FPC development ---------------------------------------------------------------------- bytebites · 2025-03-08 21:20 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Function FormatFloat(Const Format : String; Value : Extended; Const FormatSettings: TFormatSettings) : String; Var  buf : Array[0..1024] of Char;  Len: Integer; Begin  Len:=FloatToTextFmt(PChar(@Buf[0]),Valu e,PChar(Format),FormatSettings);  Buf[Len]:=#0;  Result:=StrPas(Pchar(@Buf[0]));End;  Currently FormatFloat function uses StrPas to create result, is it possible to use SetString instead since the length is known? ---------------------------------------------------------------------- ASerge · 2025-03-09 13:09 I suggest making it more safe: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function FormatFloat(const Format: string; Value: Extended; const FormatSettings: TFormatSettings): string;   procedure FormatCommon(Buf: PChar);  var    Len: SizeInt;  begin    Len := FloatToTextFmt(@Buf[0], Value, PChar(Format), FormatSettings);    SetString(Result, Buf, Len);  end;   procedure FormatDynamicBuf;  var    Buf: PChar;  begin    Buf := GetMem(Length(Format) + 100);    try    FormatCommon(Buf);    finally      FreeMem(Buf);    end;  end; var  Buf: array[Byte] of Char;begin  if Length(Format) < 100 then    FormatCommon(Buf)  else    FormatDynamicBuf;end; A simple test where the original gives an AV: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  LongFormat: string;begin  Memo1.Text := FormatFloat('0000', 0.0, DefaultFormatSettings);  LongFormat := StringOfChar('0', 2000);  Memo1.Append(FormatFloat(LongFormat, 0.0, DefaultFormatSettings));end; ---------------------------------------------------------------------- AlexTP · 2025-03-09 14:38 @bytebites I suggested the same in GitLab issue, without reading this topic. https://gitlab.com/freepascal.org/fpc/source/-/issues/41181