Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2023-06

Wrong assignment detected only run time without warning

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Giuliano Colla

I made a stupid error. My code:

TFileObj = Class(TObject)
[snip]

     Size: Integer;
     [snip]
     end;

....
procedure TForm1.FillDir(FileData : TSearchRec);
...

     FFIleObj.Size := FileData.Size;

...

but TSearchRec structure is:

   TRawbyteSearchRec = Record
     [snip]
     Size : Int64;
     [snip]

IOW I assign an int64 to an integer.

But when compiling I don't get any warning about that, and when the program runs there's no problem until the FileData.Size fits into an integer. When FileData.Size gets too large if compiled with -Co I get a range exception, if compiled without I just get wrong value for Size.

Is that the intended behavior of compiler?

Platform Linux CentOs 7 - fpc 3.2.2

Giuliano

Peter B
to Giuliano Colla
Giuliano Colla wrote
Is that the intended behavior of compiler?

Yes. Assigning a 64bit integer to a 32bit one is fine as long as the range is within bounds. Range of a variable can only be checked at run time.

Giuliano Colla
to Peter B

Il 16/06/23 13:36, Peter B via fpc-pascal ha scritto:

Yes. Assigning a 64bit integer to a 32bit one is fine as long as the range is within bounds. Range of a variable can only be checked at run time.

I can understand that. But the compiler generates quite a number of warning for potential loss of information. In that case a little warning wouldn't be excessive, I think.

Tomas Hajny
to Giuliano Colla
Giuliano Colla wrote 9 lines
Il 16/06/23 13:36, Peter B via fpc-pascal ha scritto: > Yes. Assigning a 64bit integer to a 32bit one is fine as long as the > range is within bounds. > Range of a variable can only be checked at run time. I can understand that. But the compiler generates quite a number of warning for potential loss of information. In that case a little warning wouldn't be excessive, I think.

If you compile with -CO, you should get at least a hint (if you enable displaying them, of course):

---
{$MODE OBJFPC}
uses
  SysUtils;
var
   ISize: Integer;
   SR: TSearchRec;
begin
  FindFirst ('XYZ', faAnyFile, SR);
  ISize := SR.Size;
  WriteLn (ISize);
end.
===

D:\TEMP>fpc -CO -vwih t.pas
Hint: Start of reading config file ...\fpc.cfg
Hint: End of reading config file ...\fpc.cfg
Free Pascal Compiler version 3.2.2 [2021/05/15] for i386 Copyright (c) 1993-2021 by Florian Klaempfl and others Target OS: Win32 for i386
Compiling t.pas
t.pas(11,8) Hint: Type size mismatch, possible loss of data / range check error
Linking t.exe
13 lines compiled, 0.1 sec, 68800 bytes code, 4276 bytes data 3 hint(s) issued

Tomas

Giuliano Colla
to Tomas Hajny

Il 16/06/23 15:02, Tomas Hajny via fpc-pascal ha scritto:

If you compile with -CO, you should get at least a hint (if you enable displaying them, of course):

I believed that -Co was enough. I'll add -CO to the debug options.

Thanks.

Giuliano