Optimizer leads to wrong short boolean code generation
Original Reporter info from Mantis: Pierre @PierreMuller
-
Reporter name: Pierre Muller
Original Reporter info from Mantis: Pierre @PierreMuller
- Reporter name: Pierre Muller
Description:
Reported by Tobias Giesen on fpc-pascal mailinglist, 2021/05/30
Hello,
it seems that the newest 32-bit FPC sometimes creates complete Boolean Evaluation
rather than partial, which causes my application to crash. My context is like this:
type BOOL=LongBool;
PBOOL=^LongBool;
function DoSomething(const Cancel:PBOOL=nil);
begin
if Assigned(Cancel) and Cancel^ then
Exit;
end;
This crashes because Cancel and Cancel^ are always evaluated, even if Cancel is nil.
It works fine in 64-bit.
Is this a known problem?
Cheers,
Tobias
I modified this to a simple test, which shows that x86_64 3.2.2 release compiler
also generates wrong code for this simple source!
Steps to reproduce:
muller@gcc10:/pas/check$ fpc -gl -O2 test-complete-boolean.pp -al/pas/check$ ./test-complete-boolean
Free Pascal Compiler version 3.2.2 [2021/05/16] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Note: Switching assembler to default source writing assembler
Target OS: Linux for x86-64
Compiling test-complete-boolean.pp
Assembling program
Linking test-complete-boolean
26 lines compiled, 0.2 sec
1 note(s) issued
muller@gcc10:
First try
2nd try
Runtime error 216 at $00000000004001E2
$00000000004001E2 DOSOMETHING, line 8 of test-complete-boolean.pp
Additional information:
muller@gcc10:~/pas/check$ cat test-complete-boolean.pp
{$mode objfpc}
type BOOL=LongBool;
PBOOL=^LongBool;
procedure DoSomething(const Cancel:PBOOL=nil);
begin
if Assigned(Cancel) and Cancel^ then
Exit;
writeln('Cancel is false');
end;
var
b : BOOL;
pb : PBOOL;
begin
b:=true;
pb:=@b;
Writeln('First try');
DoSomething(pb);
pb:=nil;
Writeln('2nd try');
DoSomething(pb);
Writeln('3rd try');
DoSomething;
end.
Mantis conversion info:
- Mantis ID: 38973
- OS: At least MacOS and Linux
- Version: 3.2.2