[fpc-pascal] Assign int64 to int32 shows no warnings at compilation time
Graeme Geldenhuys
mailinglists at geldenhuys.co.uk
Tue Dec 18 18:13:21 CET 2018
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
More information about the fpc-pascal
mailing list