Recent

Author Topic: Assign int64 to int32 shows no warnings at compilation time.  (Read 1673 times)

dmitryb

  • New member
  • *
  • Posts: 32
Assign int64 to int32 shows no warnings at compilation time.
« on: September 16, 2018, 09:45:26 pm »
The compilation of the following code does not show any warnings.

Code: Pascal  [Select]
  1. procedure TForm1.Button9Click(Sender: TObject);
  2. var
  3.   d64: Int64;
  4.   d32: Int32;
  5. begin
  6.   d64 := Int64.MaxValue;
  7.   d32 := d64;    
  8. end;

Is it possible to have warnings when the assignment has a potential overflow?

Xor-el

  • Sr. Member
  • ****
  • Posts: 273
Re: Assign int64 to int32 shows no warnings at compilation time.
« Reply #1 on: September 16, 2018, 10:41:15 pm »
did you enable range and overflow check?

dmitryb

  • New member
  • *
  • Posts: 32
Re: Assign int64 to int32 shows no warnings at compilation time.
« Reply #2 on: September 16, 2018, 10:44:00 pm »
Yes, but it only affects the runtime, does not it?

Xor-el

  • Sr. Member
  • ****
  • Posts: 273
Re: Assign int64 to int32 shows no warnings at compilation time.
« Reply #3 on: September 16, 2018, 11:35:33 pm »
Yes, but it only affects the runtime, does not it?

yes..

Graeme

  • Hero Member
  • *****
  • Posts: 1430
    • Graeme on the web
Re: Assign int64 to int32 shows no warnings at compilation time.
« Reply #4 on: December 18, 2018, 10:50:18 pm »
Is it possible to have warnings when the assignment has a potential overflow?

Yes it can. FPC has support for warning the developer of such mistakes. Simply add the dedicated -CO compiler parameter. Below it shows the compiler warning. In the test project I assigned a variable of type Int64 to a variable of type Int32.

Code: Text  [Select]
  1. $ fpc -CO -vh overflow.pas
  2. Hint: Start of reading config file /home/graemeg/.fpc.cfg
  3. Hint: End of reading config file /home/graemeg/.fpc.cfg
  4. Free Pascal Compiler version 3.0.4+dfsg-18ubuntu2 [2018/08/29] for x86_64
  5. Copyright (c) 1993-2017 by Florian Klaempfl and others
  6. Target OS: Linux for x86-64
  7. Compiling overflow.pas
  8. overflow.pas(11,7) Hint: Type size mismatch, possible loss of data / range check error
  9. Linking overflow
  10. /usr/bin/ld.bfd: warning: link.res contains output sections; did you forget -T?
  11. 14 lines compiled, 0.0 sec
  12. 3 hint(s) issued
  13.  
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

dmitryb

  • New member
  • *
  • Posts: 32
Re: Assign int64 to int32 shows no warnings at compilation time.
« Reply #5 on: December 18, 2018, 11:14:48 pm »
Hello
I have "Type size mismatch, possible loss of data / range check error" Hint checked in the Compiler Options - Messages.
But Compiler doesn't show this Hint.


Xor-el

  • Sr. Member
  • ****
  • Posts: 273
Re: Assign int64 to int32 shows no warnings at compilation time.
« Reply #6 on: December 19, 2018, 01:28:32 am »
Hello
I have "Type size mismatch, possible loss of data / range check error" Hint checked in the Compiler Options - Messages.
But Compiler doesn't show this Hint.

you are doing it wrong.
follow the numbers in my attached image below.

dmitryb

  • New member
  • *
  • Posts: 32
Re: Assign int64 to int32 shows no warnings at compilation time.
« Reply #7 on: December 20, 2018, 02:50:08 pm »
Ok. Thanks.