Recent

Author Topic: very minor bug in FPC v3.0.4  (Read 639 times)

440bx

  • Hero Member
  • *****
  • Posts: 2000
very minor bug in FPC v3.0.4
« on: May 19, 2020, 07:50:06 am »
Hello,

when an array range is a boolean range with only one element, e.g, [false..false] or [true..true] and a statement specifies an out of range bound, the compiler does not emit a warning (it does when the range is a different ordinal type, e.g, integer.)

The little test program below demonstrates this:
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2.  
  3. program BooleanRangeChecks;
  4.  
  5. type
  6.   TARRAY_BOOLEAN_TYPE = array[true..true] of integer;
  7.   TARRAY_INTEGER_TYPE = array[1   ..   5] of integer;
  8.  
  9. var
  10.   BooleanArray     : TARRAY_BOOLEAN_TYPE;
  11.   IntegerArray     : TARRAY_INTEGER_TYPE;
  12.  
  13.   IntegerVariable  : integer;
  14.  
  15.  
  16. begin
  17.   IntegerVariable     := 1;
  18.  
  19.   { emits warning about index out of range    }
  20.  
  21.   IntegerArray[10]    := IntegerVariable;
  22.  
  23.   { no warning about index being out of range }
  24.   { or use "false" instead of an expression   }
  25.  
  26.   BooleanArray[1 > 2] := IntegerVariable;
  27.  
  28.  
  29.   writeln(IntegerArray[low(IntegerArray)]);
  30.   writeln(BooleanArray[low(BooleanArray)]);
  31. end.                                    
« Last Edit: May 19, 2020, 07:52:02 am by 440bx »
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 10492
Re: very minor bug in FPC v3.0.4
« Reply #1 on: May 19, 2020, 08:06:22 am »
Very major bug in programmer: add {$R+} and the compiler plays nice and won't even compile that code.
In general  there is a reason that the default is more relaxed than you seem to expect here. Hard Casts... seriously? what do you expect the compiler to do?
When you ask a question that is actually answered in the documentation, you are either lazy or a moron.

440bx

  • Hero Member
  • *****
  • Posts: 2000
Re: very minor bug in FPC v3.0.4
« Reply #2 on: May 19, 2020, 08:17:24 am »
Very major bug in programmer: add {$R+} and the compiler plays nice and won't even compile that code.
You really should make an effort not to talk about things you know nothing about.  As I hope you know, $R+ is for runtime checks.  There is no need for runtime checks because the indexes are constants... read that again... the indexes are constant expressions and/or constant literals.  No runtime checks are necessary to ensure they are within the bounds.

In general  there is a reason that the default is more relaxed than you seem to expect here. Hard Casts... seriously? what do you expect the compiler to do?
First, unless you are hallucinating, there are no hard casts in the example I posted.  Second, I expect the compiler to do strong type checking as the Pascal language requires.

The expression evaluator should verify the array bounds whenever the index is a constant whether the index is an integer or boolean type.

FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 10492
Re: very minor bug in FPC v3.0.4
« Reply #3 on: May 19, 2020, 08:51:08 am »
The expression evaluator should verify the array bounds whenever the index is a constant whether the index is an integer or boolean type.
Well, then, (again) your own code:
Code: Pascal  [Select][+][-]
  1. {$ifdef mswindows}{$APPTYPE CONSOLE}{$endif}{$R+}
  2.  
  3. program BooleanRangeChecks;
  4.  
  5. type
  6.   TARRAY_BOOLEAN_TYPE = array[true..true] of integer;
  7.   TARRAY_INTEGER_TYPE = array[1   ..   5] of integer;
  8.  
  9. var
  10.   BooleanArray     : TARRAY_BOOLEAN_TYPE;
  11.   IntegerArray     : TARRAY_INTEGER_TYPE;
  12.  
  13.   IntegerVariable  : integer;
  14.  
  15.  
  16. begin
  17.   IntegerVariable     := 1;
  18.  
  19.   { emits warning about index out of range    }
  20.  
  21.   IntegerArray[10]    := IntegerVariable;
  22.  
  23.   { no warning about index being out of range }
  24.   { or use "false" instead of an expression   }
  25.   BooleanArray[1 > 2] := IntegerVariable;
  26.   writeln(IntegerArray[low(IntegerArray)]);
  27.   writeln(BooleanArray[low(BooleanArray)]);
  28. end.
             

Results in:
Code: Bash  [Select][+][-]
  1. BooleanRangeChecks.pas(21,16) Error: (4109) Range check error while evaluating constants (10 must be between 1 and 5)
Silly... >:D
Outright annoying.
« Last Edit: May 19, 2020, 09:05:00 am by Thaddy »
When you ask a question that is actually answered in the documentation, you are either lazy or a moron.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2156
  • Compiler Developer
Re: very minor bug in FPC v3.0.4
« Reply #4 on: May 19, 2020, 09:25:01 am »
Results in:
Code: Bash  [Select][+][-]
  1. BooleanRangeChecks.pas(21,16) Error: (4109) Range check error while evaluating constants (10 must be between 1 and 5)
Silly... >:D
Outright annoying.

*sigh* As usual you're missing the point. 440bx complains about the missing check on the Boolean array, not the Integer one. He said himself that the Integer one generates a warning (which is the same warning that is turned into an error with $R+). If you comment the offending line 21 then the program will compile without any error despite there being a range error with the Boolean array.

@440bx: please report as a bug.

440bx

  • Hero Member
  • *****
  • Posts: 2000
Re: very minor bug in FPC v3.0.4
« Reply #5 on: May 19, 2020, 09:29:17 am »
Silly... >:D
Outright annoying.
This is good... now you're pulling out your trademark grumpy.  Surprising there is only one... are you on a grumpy-diet ?

When you're done with that, comment out the line that reads "IntegerArray[10]    := IntegerVariable;" and recompile.

Put on your glasses, find a telescope, a microscope or whatever you might need to notice that the compiler neither emits a warning nor an error for the BooleanArray index which is out of range.  After you've noticed that (seek assistance if necessary), re-read the OP.  The point was to show that while FPC does give a warning for arrays indexed by integer types it does _not_ output any warning (or error for that matter) if the array is indexed by a boolean type and there is only one element in the array (either false..false or true..true)

Only one (1) grumpy !?... that's disappointing... I expect more grumpies in the next post... you're normally very grumpy-generous...   I suspect that while your post count is high, your number of grumpies is probably higher. 

try compiling this...
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$R+}
  3.  
  4. program BooleanRangeChecks;
  5.  
  6. type
  7.   TARRAY_BOOLEAN_TYPE = array[true..true] of integer;
  8.   TARRAY_INTEGER_TYPE = array[1   ..   5] of integer;
  9.  
  10. var
  11.   BooleanArray     : TARRAY_BOOLEAN_TYPE;
  12.   IntegerArray     : TARRAY_INTEGER_TYPE;
  13.  
  14.   IntegerVariable  : integer;
  15.  
  16.  
  17. begin
  18.   IntegerVariable     := 1;
  19.  
  20.   { no warning about index being out of range }
  21.   { or use "false" instead of an expression   }
  22.  
  23.   BooleanArray[1 > 2] := IntegerVariable;
  24.  
  25.   writeln(BooleanArray[low(BooleanArray)]);
  26. end.
  27.  



*sigh* As usual you're missing the point. 440bx complains about the missing check on the Boolean array, not the Integer one. He said himself that the Integer one generates a warning (which is the same warning that is turned into an error with $R+). If you comment the offending line 21 then the program will compile without any error despite there being a range error with the Boolean array.

@440bx: please report as a bug.
It's refreshing to see a mind functioning properly. :)


ETA
Ticket number : 0037112
« Last Edit: May 19, 2020, 09:37:17 am by 440bx »
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

 

TinyPortal © 2005-2018