synopse / mORMot

Synopse mORMot 1 ORM/SOA/MVC framework - Please upgrade to mORMot 2 !
https://synopse.info
785 stars 323 forks source link

Endless recursive calls in SynCommons::FileSize #426

Closed mingodad closed 2 years ago

mingodad commented 2 years ago

Converting the SQLite3/Samples/12 - SynDB Explorer/DBSynLZ.dpr to Lazarus and building it segfaults due to a endless recursive loop here:

function FileSize(F: THandle): Int64;
var res: Int64Rec absolute result;
begin
  result := 0;
  if PtrInt(F)>0 then
    res.Lo := FileSize(F); { *Converted from GetFileSize* } // from WinAPI or SynKylix/SynFPCLinux
end; 

Changing this line seem to fix the problem:

res.Lo := FileSize(F); { *Converted from GetFileSize* } // from WinAPI or SynKylix/SynFPCLinux

by:

res.Lo := GetFileSize(F, nil); { *Converted from GetFileSize* } // from WinAPI or SynKylix/SynFPCLinux
synopse commented 2 years ago

Your source seems old and outdated, or has been modified from the original.

Current code is :

function FileSize(F: THandle): Int64;
var res: Int64Rec absolute result;
begin
  result := 0;
  if PtrInt(F)>0 then
    res.Lo := GetFileSize(F,@res.Hi); // from WinAPI or SynKylix/SynFPCLinux
end;
mingodad commented 2 years ago

Yes, it's strange because I just did a fossil clone and fossil open a few days ago (3 or 4), I'm puzzled, but after your reply I did a check on my repository and can see that the Lazarus tool when converted the SQLite3/Samples/12 - SynDB Explorer/DBSynLZ.dpr has changed that line.

Sorry it was my fault and thank you again for your help.

fossil status
repository:   /home/mingo/dev/lazarus/mORMot.fossil
local-root:   /home/mingo/dev/lazarus/mORMot/
config-db:    /home/mingo/.fossil
checkout:     d34ccfc62edf0c594ad3afae25c36fa4b1d8c330 2022-02-28 08:01:23 UTC
parent:       91e09aae31fcbc74ac72ca141aa66c5edd2422c3 2022-02-03 11:41:09 UTC
tags:         trunk
comment:      {6366} updated static SQLite3 engine to latest 3.38.0 revision (user: ab)
MISSING    SQLite3/SQLite3UI.RES
MISSING    SQLite3/SQLite3UILogin.RES
EDITED     SQLite3/Samples/01 - In Memory ORM/Project01.res
EDITED     SQLite3/Samples/04 - HTTP Client-Server/Project04Client.res
EDITED     SQLite3/Samples/04 - HTTP Client-Server/Project04Server.res
EDITED     SQLite3/Samples/12 - SynDB Explorer/DBSynLZ.dpr
EDITED     SQLite3/Samples/12 - SynDB Explorer/SynDBExplorer.dpr
EDITED     SQLite3/Samples/12 - SynDB Explorer/SynDBExplorerMain.pas
EDITED     SQLite3/Samples/ThirdPartyDemos/Ondrej/SynTaskDialog4Lazarus/SynTaskDialog.pas
EDITED     SQLite3/mORMotUIEdit.pas
EDITED     SQLite3/mORMotUILogin.pas
EDITED     SQLite3/mORMoti18n.pas
EDITED     SynCommons.pas
EDITED     SynFPCMetaFile.pas
EDITED     SynTaskDialog.pas

Here is the output of fossil diff:

....
Index: SynCommons.pas
==================================================================
--- SynCommons.pas
+++ SynCommons.pas
@@ -1,10 +1,14 @@
 /// common functions used by most Synopse projects
 // - this unit is a part of the freeware Synopse mORMot framework,
 // licensed under a MPL/GPL/LGPL tri-license; version 1.18
 unit SynCommons;

+{$IFDEF FPC}
+  {$MODE Delphi}
+{$ENDIF}
+
 (*
     This file is part of Synopse framework.

     Synopse framework. Copyright (C) 2022 Arnaud Bouchez
       Synopse Informatique - https://synopse.info
@@ -96,11 +100,11 @@
   {$endif HASINLINE}
 {$endif LVCL}
 {$ifndef NOVARIANTS}
   Variants,
 {$endif NOVARIANTS}
-  SynLZ, // needed for TSynMapFile .mab format
+  SynLZ, FileUtil, // needed for TSynMapFile .mab format
   SysUtils;

 const
   /// the corresponding version of the freeware Synopse framework
@@ -30602,11 +30606,11 @@
         SetLength(result,Size+Read); // in-place resize
         MoveFast(tmp,PByteArray(result)^[Size],Read);
         inc(Size,Read);
       until false;
     end else begin
-      Size := GetFileSize(F,nil);
+      Size := FileSize(F); { *Converted from GetFileSize* }
       if Size>0 then begin
         SetLength(result,Size);
         P := pointer(result);
         repeat
           Chunk := Size;
@@ -30854,11 +30858,11 @@
     res: Int64Rec absolute result;
 begin
   result := 0;
   f := FileOpen(FileName,fmOpenRead or fmShareDenyNone);
   if PtrInt(f)>0 then begin
-    res.Lo := GetFileSize(f,@res.Hi); // from SynKylix/SynFPCLinux
+    res.Lo := FileSize(f); { *Converted from GetFileSize* } // from SynKylix/SynFPCLinux
     FileClose(f);
   end;
 end;
 {$endif MSWINDOWS}

@@ -30865,11 +30869,11 @@
 function FileSize(F: THandle): Int64;
 var res: Int64Rec absolute result;
 begin
   result := 0;
   if PtrInt(F)>0 then
-    res.Lo := GetFileSize(F,@res.Hi); // from WinAPI or SynKylix/SynFPCLinux
+    res.Lo := GetFileSize(F, nil); { *Converted from GetFileSize* } // from WinAPI or SynKylix/SynFPCLinux
 end;

 function FileInfoByHandle(aFileHandle: THandle; out FileId, FileSize,
   LastWriteAccess, FileCreateDateTime: Int64): Boolean;
 var
...
synopse commented 2 years ago

Don't let the Lazarus conversion wizard change your code or your .res files. Just let it create the .lpi with the proper kind of application (e.g. console), then stop.

Or - even better - reuse an existing .lpi with some manual search&replace.