Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2019-10

user error or bug? bitpacked array - neighbouring values overwrite each other

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Martin

To initialize an array, negative values can not be specified (see error below).

Type casting to positive works,  if you have the exact bit-size-matching unsigned type.
If the typecast is to big, it also gives a range check.

But with $R- values that are to big, are truncated.

Except in bitpacked arrays.
- no range check error (that may be due to the sub range type ??)
- values to big, overwrite the neighbouring element

I think the latter is a bug. *If* the compiler accepts the value, then
it should protect neighbouring elements.

So is this a bug?
What should happen?

Doing
gcBitPackTinyNegArray[1] := cardinal(-1);
at runtime seems to work fine. (gives a compile time warning too / $R-)

program test;
{$R-}
type
   TTinyNegRange = -2..3;
   TBitPackTinyNegArray  = bitpacked array [0..3] of TTinyNegRange;
   TTestArray  = bitpacked array [0..3] of byte;
const
   // Warning: range check error while evaluating constants (4294967294
must be between 0 and 255)
   // Works with $R-
//  gcBitPackTinyNegArray   : TTestArray = (2, cardinal(-2), 0,
cardinal(-1));

  //Error: range check error while evaluating constants (-2 must be between 0 and 18446744073709551615)//
  //Also range check, even with $R-
//  gcBitPackTinyNegArray : TBitPackTinyNegArray = (2, -2, 0, -1);

  // No Error / WRONG Value, prints: 2, -2, -1, -1  // 0 is replaced by -1   // same with $R-
  gcBitPackTinyNegArray : TBitPackTinyNegArray = (2, cardinal(-2), 0, cardinal(-1));

   // works
//  gcBitPackTinyNegArray : TBitPackTinyNegArray = (2, 6, 0, cardinal(-1));
//  gcBitPackTinyNegArray : TBitPackTinyNegArray = (2, 6, 0, 7);
begin
writeln(gcBitPackTinyNegArray[0]);
writeln(gcBitPackTinyNegArray[1]);
writeln(gcBitPackTinyNegArray[2]);
writeln(gcBitPackTinyNegArray[3]);
readln();
end.

Reply by email

Martin Frb
to Martin
Martin wrote 10 lines
program test; type   TTinyNegRange = -2..3;   TBitPackTinyNegArray  = bitpacked array [0..3] of TTinyNegRange;   TTestArray  = bitpacked array [0..3] of byte; const   // No Error / WRONG Value, prints: 2, -2, -1, -1  // 0 is replaced by -1   gcBitPackTinyNegArray : TBitPackTinyNegArray = (2, cardinal(-2), 0, cardinal(-1));

possible related https://bugs.freepascal.org/view.php?id=36156

Reply by email