Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi code fails at 64 bits access violation

Tags:

delphi

This Delphi code works when compiled for 32 bits, but gives an access violation when compiled for 64 bits. Is there a problem with the code, or is there a compiler bug?

{$APPTYPE CONSOLE}

uses
  SysUtils;

const
  MaxSize = 2; // nothing special about this value, could equally be 1

type
  TArraySize = 1..MaxSize;

procedure Main;
var
  size: TArraySize;
  arr: array [-MaxSize..MaxSize] of Integer;
begin
  FillChar(arr, SizeOf(arr), 0); // zero initialize
  size := MaxSize;
  Writeln(arr[-size]);
end;

begin
  try
    Main;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.
like image 999
user1238784 Avatar asked Sep 21 '18 12:09

user1238784


1 Answers

This is a compiler bug. The compiler does not handle

arr[-size]

correctly, presumably because size is a subrange type.

You can workaround the bug by forcing the compiler to perform the arithmetic in an Integer context.

arr[-Integer(size)]

You should submit a bug report to Embarcadero's Quality Portal.

Update

I tested this in XE7. According to a comment, the defect appears to have been fixed at least in Seattle.

like image 141
David Heffernan Avatar answered Nov 08 '22 14:11

David Heffernan