Sign in Join

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

Mailing lists / fpc-pascal / 2018-12

Assign int64 to int32 shows no warnings at compilation time

6 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Graeme Geldenhuys

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

Reply by email

Graeme Geldenhuys
to Graeme Geldenhuys
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

Reply by email

Jonas Maebe
to Graeme Geldenhuys: Just as a comparison, here is the same code, but in Java.…
Graeme Geldenhuys wrote 19 lines
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

Reply by email

Graeme Geldenhuys
to Jonas Maebe
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

Reply by email

Graeme Geldenhuys
to Jonas Maebe: Compile with -CO -vh. Be prepared for lots of hints. Jonas
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

Reply by email