[fpc-devel] Dangerous optimization in CASE..OF

Florian Klämpfl florian at freepascal.org
Sun Jul 2 19:58:17 CEST 2017


Am 02.07.2017 um 19:51 schrieb Martok:
> Booleans are not enums in Delphi (not even ordinals), 

They are:
http://docwiki.embarcadero.com/Libraries/XE5/en/System.Boolean

> but their own little
> thing. "if boolean_expr" is always a jz/jnz, no matter what. 

Yes. This is an optimization which is invalid as well if I follow your argumentation. Boolean(3)<>true.

> They are defined as
> 0=FALSE and "everything else"=TRUE

No, see link above.

> 
> However:
> 
> var
>   b : boolean;
> begin
>   b:=boolean(3);
>   if b = True then
>     writeln(true)
>   else if b = False then
>     writeln(false)
>   else
>     writeln(ord(b));
> end.
> 
> That writes 3, 

Yes. What I wanted to point out: also delphi does optimizations on enums which fails if one feeds
invalid values.

> which is why your should never compare on the boolean lexicals.
> Some Winapi functions returning longbool rely on that.

No, longbool is something different (even bytebool is).

> 
> Wait, that was a trick question, wasn't it?

In the sense to point out that also delphi assumes enumeration variables contain always valid values.



More information about the fpc-devel mailing list