Recent

Author Topic: likely bug in FPC v3.2.2 handling of booleans  (Read 5356 times)

440bx

  • Hero Member
  • *****
  • Posts: 5182
likely bug in FPC v3.2.2 handling of booleans
« on: January 28, 2025, 07:10:37 am »
Hello,

Consider the following program:
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC }
  2.  
  3. program BooleanBug;
  4.  
  5. function TestBoolean() : boolean;
  6. var
  7.   b8     : Boolean   = FALSE;
  8.   b16    : Boolean16 = FALSE;
  9.  
  10.   BooleanArray : bitpacked array[0..1] of boolean = (FALSE, FALSE);
  11.  
  12. begin
  13.   ord(b8)  := 4;                // true
  14.   ord(b16) := 8;                // true
  15.  
  16.   writeln('ord(b8)  = ', ord(b8));
  17.   writeln('ord(b16) = ', ord(b16));
  18.   writeln;
  19.  
  20.   { ------------------------------------------------------------------------- }
  21.  
  22.   // assign the variables to the bitpacked elements
  23.   // both, b8 and b16 are supposedly true but, when assigned to a bitpacked
  24.   // boolean, one of them is no longer true.
  25.  
  26.   BooleanArray[0] := b8;
  27.   writeln('b8  = ', b8,  ' ord(b8)  = ', ord(b8),  ' BooleanArray[0] = ', BooleanArray[0]);
  28.   BooleanArray[1] := b16;
  29.   writeln('b16 = ', b16, ' ord(b16) = ', ord(b16), ' BooleanArray[1] = ', BooleanArray[1]);
  30.   writeln;
  31.  
  32.   result := true;
  33. end;
  34.  
  35. begin
  36.   TestBoolean();
  37. end.
  38.  
The output shows that the first array element is FALSE and the second array element is TRUE.  Both elements should be the same, not one TRUE and the other one FALSE.

Since the compiler considers them both to be TRUE, it follows that the array elements should also both be TRUE.

Comments welcome.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 16813
  • Ceterum censeo Trump esse delendam
Re: likely bug in FPC v3.2.2 handling of booleans
« Reply #1 on: January 28, 2025, 08:28:29 am »
Code: Pascal  [Select][+][-]
  1.   ord(b8)  := 4;                // true
  2.   ord(b16) := 8;                // true
Really? Both must be false.
It is legal code but it is not an assignment to b8 or b16.
For that you need to do something like:
Code: Pascal  [Select][+][-]
  1.   b8  := Sizeof(b8) = 1;
  2.   b16 := SizeOf(b16) = 2;
It needs to resolve to either zero or one as per the definition of a pascal boolean.
Ord() is an intrinsic function, so creates an intermediate result.
It is not "in-place"!!!
https://www.freepascal.org/docs-html/rtl/system/ord.html

Sorry, no bug.

To verify that, add:
Code: Pascal  [Select][+][-]
  1.    writeln(low(b8),high(b8));
  2.    writeln(low(b16),high(b16));

« Last Edit: January 28, 2025, 08:41:31 am by Thaddy »
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

440bx

  • Hero Member
  • *****
  • Posts: 5182
Re: likely bug in FPC v3.2.2 handling of booleans
« Reply #2 on: January 28, 2025, 08:40:17 am »
Sorry,
Yes, it is obvious you are rather sorry.

Now, if it isn't too much to expect from you, compile and run the program. 
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 16813
  • Ceterum censeo Trump esse delendam
Re: likely bug in FPC v3.2.2 handling of booleans
« Reply #3 on: January 28, 2025, 08:42:39 am »
I did, hence I spotted that... Had to take an extra coffee.
Code: Pascal  [Select][+][-]
  1.    writeln(low(b8),high(b8));
  2.    writeln(low(b16),high(b16));

So you still think it is a bug? ROFL.

« Last Edit: January 28, 2025, 08:47:57 am by Thaddy »
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

Thaddy

  • Hero Member
  • *****
  • Posts: 16813
  • Ceterum censeo Trump esse delendam
Re: likely bug in FPC v3.2.2 handling of booleans
« Reply #4 on: January 28, 2025, 08:50:17 am »
Is it too much to trust me I am almost always right? In the cases I am not it gets blown out of proportions by some. Not a bug.
ord is not a cast, which you seem to think.
What the hell did you expect from this:
Code: Pascal  [Select][+][-]
  1.   ord(b8)  := 4;                // true
  2.   ord(b16) := 8;                // true
   silly. Back to coffee.
Have you been out of sleep(0) lately?  ::) ;D

Otherwise, why do you expect the result to be different?
You know how to use a debugger....
« Last Edit: January 28, 2025, 09:01:16 am by Thaddy »
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

cdbc

  • Hero Member
  • *****
  • Posts: 2104
    • http://www.cdbc.dk
Re: likely bug in FPC v3.2.2 handling of booleans
« Reply #5 on: January 28, 2025, 09:01:00 am »
Hi
Hmmmm...
Could it be, that you're mistaken 'boolean' for 'bool' or 'longbool'?!?
I mean, a 'boolean' in pascal is an enumerated type of (false,true) ~ ord(0..1),
whereas 'bool & longbool' are not and with these, 0 = false and 'anything but 0' = true.
Just saying...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Thaddy

  • Hero Member
  • *****
  • Posts: 16813
  • Ceterum censeo Trump esse delendam
Re: likely bug in FPC v3.2.2 handling of booleans
« Reply #6 on: January 28, 2025, 09:04:22 am »
No Benny, they are both Pascal booleans defined as range 0..1 - not C bool - but with a different size.
I thought the same, though, but the issue is simply that he seems to think Ord() is a cast, whereas it is of course a function.

https://www.youtube.com/watch?v=kRwCPUEND1U
« Last Edit: January 28, 2025, 09:12:51 am by Thaddy »
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11040
  • Debugger - SynEdit - and more
    • wiki
Re: likely bug in FPC v3.2.2 handling of booleans
« Reply #7 on: January 28, 2025, 09:12:25 am »
https://www.freepascal.org/docs-html/ref/refsu4.html

They are both zero or one. The values 4 and 8 are out of range.

If you assign (to an enum, or subrange value) an out of range value, then the behaviour of the program will be undefined.
So, the code you presented may (depending on compiler version, optimization and other settings) give different results.

Thaddy

  • Hero Member
  • *****
  • Posts: 16813
  • Ceterum censeo Trump esse delendam
Re: likely bug in FPC v3.2.2 handling of booleans
« Reply #8 on: January 28, 2025, 09:14:11 am »
Martin, he thinks ord() behaves like a cast... that's all.
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

DragoRosso

  • Guest
Re: likely bug in FPC v3.2.2 handling of booleans
« Reply #9 on: January 28, 2025, 09:26:20 am »
https://www.freepascal.org/docs-html/ref/refsu4.html

They are both zero or one. The values 4 and 8 are out of range.

If you assign (to an enum, or subrange value) an out of range value, then the behaviour of the program will be undefined.
So, the code you presented may (depending on compiler version, optimization and other settings) give different results.

In the document you refers it's said that 
Quote
Free Pascal also supports the ByteBool, WordBool, LongBool and QWordBool types. These are of type Byte, Word, Longint or Int64, but are again assignment compatible with a Boolean.

These means that if you assign to boolean any value in those ranges, that should be OK, and must be OK.

Or I understand wrong ?
By the way, I faces to those problems some times ago and by that time I always use WordBool, expecially if other languages, exporting of values (like writing in DB or in a file) are involved.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11040
  • Debugger - SynEdit - and more
    • wiki
Re: likely bug in FPC v3.2.2 handling of booleans
« Reply #10 on: January 28, 2025, 09:35:31 am »
https://www.freepascal.org/docs-html/ref/refsu4.html

In the document you refers it's said that 
Quote
Free Pascal also supports the ByteBool, WordBool, LongBool and QWordBool types. These are of type Byte, Word, Longint or Int64, but are again assignment compatible with a Boolean.

These means that if you assign to boolean any value in those ranges, that should be OK, and must be OK.

Yes, that is right.

tetrastes

  • Hero Member
  • *****
  • Posts: 640
Re: likely bug in FPC v3.2.2 handling of booleans
« Reply #11 on: January 28, 2025, 10:03:11 am »
but the issue is simply that he seems to think Ord() is a cast, whereas it is of course a function.
Then why
Code: Pascal  [Select][+][-]
  1. ord(b8)  := 4;
is compiled at all?

You cannot do that with a function:
Code: Pascal  [Select][+][-]
  1. function BoolToInt(b: boolean): longint;
  2. begin
  3.     Result := ord(b);
  4. end;
  5. . . .
  6.  
  7. BoolToInt(b8) := 4;
  8.  
is not compiled with "Error: Argument cannot be assigned to".

tetrastes

  • Hero Member
  • *****
  • Posts: 640
Re: likely bug in FPC v3.2.2 handling of booleans
« Reply #12 on: January 28, 2025, 10:35:00 am »
The output shows that the first array element is FALSE and the second array element is TRUE.  Both elements should be the same, not one TRUE and the other one FALSE.

Since the compiler considers them both to be TRUE, it follows that the array elements should also both be TRUE.

Comments welcome.

The question is how boolean is bitpacked?
Change
Code: Pascal  [Select][+][-]
  1. ord(b8) := 5;
and see the result.
Or
Code: Pascal  [Select][+][-]
  1. BooleanArray : bitpacked array[0..1] of Boolean16 = (FALSE, FALSE);

Thaddy

  • Hero Member
  • *****
  • Posts: 16813
  • Ceterum censeo Trump esse delendam
Re: likely bug in FPC v3.2.2 handling of booleans
« Reply #13 on: January 28, 2025, 10:42:54 am »
It has nothing to do with bitpacked but the use of ord()
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

tetrastes

  • Hero Member
  • *****
  • Posts: 640
Re: likely bug in FPC v3.2.2 handling of booleans
« Reply #14 on: January 28, 2025, 10:51:30 am »
The results for bitpacked and not bitpacked arrays are different.

 

TinyPortal © 2005-2018