Assign int64 to int32 shows no warnings at compilation time

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Assign int64 to int32 shows no warnings at compilation time

Graeme Geldenhuys-6
Hi,

Is there a FPC command line parameter to enable warnings or errors for
the following situation at compile time? All logic tells me that
detecting this an runtime only is WAY to late in the game!

Irrespective of the value of d64, I really think it aught to give you a
compiler warning to say that you might run into data loss.

I know Delphi can't do this (no surprise), but I really expected
something better from FPC.

=======================================
program overflow;

{$mode objfpc}{$H+}


var
  d32: integer;
  d64: Int64;
begin
  d64 := 9223372036854775806;
  d32 := d64;   // <---- No compiler warning here, yet clearly
                //       a programming mistake

  writeln(d64);
  writeln(d32);
end.
=======================================


The compiler's default behaviour:

$ fpc overflow.pas
Hint: End of reading config file /home/graemeg/.fpc.cfg
Free Pascal Compiler version 3.0.4+dfsg-18ubuntu2 [2018/08/29] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling overflow.pas
Linking overflow
/usr/bin/ld.bfd: warning: link.res contains output sections; did you
forget -T?
14 lines compiled, 0.1 sec
1 hint(s) issued


$ ./overflow
9223372036854775806
-2


Now enable runtime range checking and overflow support:

$ fpc -Criot overflow.pas
Hint: End of reading config file /home/graemeg/.fpc.cfg
Free Pascal Compiler version 3.0.4+dfsg-18ubuntu2 [2018/08/29] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling overflow.pas
Linking overflow
/usr/bin/ld.bfd: warning: link.res contains output sections; did you
forget -T?
14 lines compiled, 0.1 sec
1 hint(s) issued


$ ./overflow
Runtime error 201 at $000000000040020B
  $000000000040020B
  $000000000040018C



A crash at runtime only is way to late.


Regards,
  Graeme

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Assign int64 to int32 shows no warnings at compilation time

Graeme Geldenhuys-6
On 18/12/2018 17:13, Graeme Geldenhuys wrote:
> Is there a FPC command line parameter to enable warnings or errors for
> the following situation at compile time? All logic tells me that
> detecting this an runtime only is WAY to late in the game!


Just as a comparison, here is the same code, but in Java.


===========================================
public class Overflow {

    public static void main(String[] args) {

        int d32;
        long d64;

        d32 = Long.MAX_VALUE;

        System.out.println(d32);
        System.out.println(d64);

    }

}
==========================================


$ javac Overflow.java
Overflow.java:8: error: incompatible types: possible lossy conversion
from long to int
        d32 = Long.MAX_VALUE;
                  ^
1 error




The error is raised at compile time, which seems a LOT more logical to
me. After all, we don't want our programs to crash on site at a client -
then we look bad. :-/ I'd rather fix the code long before the client
sees it. ;-)


Regards,
  Graeme

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Assign int64 to int32 shows no warnings at compilation time

Jonas Maebe-3
In reply to this post by Graeme Geldenhuys-6
On 18/12/18 18:13, Graeme Geldenhuys wrote:
> Is there a FPC command line parameter to enable warnings or errors for
> the following situation at compile time?

Compile with -CO -vh. Be prepared for lots of hints.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Assign int64 to int32 shows no warnings at compilation time

Jonas Maebe-3
In reply to this post by Graeme Geldenhuys-6
On 18/12/18 18:27, Graeme Geldenhuys wrote:

> Just as a comparison, here is the same code, but in Java.
>
> ===========================================
> public class Overflow {
>
>      public static void main(String[] args) {
>
>          int d32;
>          long d64;
>
>          d32 = Long.MAX_VALUE;
>
>          System.out.println(d32);
>          System.out.println(d64);
>
>      }
>
> }
> ==========================================

You are assigning an int64 constant to a 32 bit variable here. In your
Pascal program, you are assigning a 64 bit *variable* to a 32 bit variable.

If you compile the equivalent Pascal program, you will always get a
warning, and you will get an error if you compile with range checks.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Assign int64 to int32 shows no warnings at compilation time

Graeme Geldenhuys-6
On 18/12/2018 17:52, Jonas Maebe wrote:
> You are assigning an int64 constant to a 32 bit variable here. In your
> Pascal program, you are assigning a 64 bit *variable* to a 32 bit variable.

It makes no difference. MAX_VALUE holds the largest value of a Int64.

  https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html#MAX_VALUE



public class Overflow {

    public static void main(String[] args) {

        int d32;
        long d64;

        d64 = Long.MAX_VALUE;
        d32 = d64;

        System.out.println(d32);
        System.out.println(d64);

    }

}


The same result at compile time:


$ javac Overflow.java
Overflow.java:9: error: incompatible types: possible lossy conversion
from long to int
        d32 = d64;
              ^
1 error



Regards,
  Graeme

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Assign int64 to int32 shows no warnings at compilation time

Graeme Geldenhuys-6
In reply to this post by Jonas Maebe-3
On 18/12/2018 17:49, Jonas Maebe wrote:
> Compile with -CO -vh. Be prepared for lots of hints.

Fantastic, thanks. That is what I was looking for.


$ fpc -CO -vh overflow.pas
Hint: Start of reading config file /home/graemeg/.fpc.cfg
Hint: End of reading config file /home/graemeg/.fpc.cfg
Free Pascal Compiler version 3.0.4+dfsg-18ubuntu2 [2018/08/29] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling overflow.pas
overflow.pas(11,7) Hint: Type size mismatch, possible loss of data /
range check error
Linking overflow
/usr/bin/ld.bfd: warning: link.res contains output sections; did you
forget -T?
14 lines compiled, 0.0 sec
3 hint(s) issued



See, I knew FPC was better than Delphi! :-)


Regards,
  Graeme

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal