Skip to content

Erroneous iteration of the for..to loop when the upper bound is sizeuint(0)-1

The following program runs incorrectly.

program project1;

{$mode delphi}

{$OPTIMIZATION DFA}
{$OPTIMIZATION FORLOOP}

type
  TRec = record
    EntryCount : sizeuint; { see TInterfaceTable.EntryCount }
  end;
  PRec = ^TRec;

function TestLoopUnsigned(Rec: PRec): Integer;
var
  i: Integer;
begin
  Result := 0;
  for i := 0 to Rec^.EntryCount - 1 do
    Inc(Result);
end;

var
  Rec: TRec;
  N: SizeInt;
begin
  Rec.EntryCount := 0;
  N := TestLoopUnsigned(@Rec);
  if N <> Rec.EntryCount then
    Halt(1); // ERROR!
end.

This is a regression error.

Edited by Sergey Larin
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information