Link to home
Start Free TrialLog in
Avatar of dataconsult01
dataconsult01

asked on

When Range checking is turned off and some code is reading, (not writing) outside an array bounds can this produce access violations?

Hi,

I got a simple question, if range checking is turned off in the compiler options and some code in a program is reading (not writing) beyond an array's  or string bounds, for example array[index] where index is one higher than the length of the array, can only that read operation produce access violations ?

I think it can but i'm not 100% certain. For example when the end of the allocated array's memory is located just at the edge / border of our programs allocted memory and just near it sits memory from another application. Or wouldn't this in general produce an access violation.

The reason i'm asking is that range checking had been turned for years in our code and i just turned it on in a debug version for me to test with to find out that there are a few places were our code reads 1 element beyond the arrays index. If range checking is turned on you'll get  a range check exception here but if it's turned off it seems to work without a problem. I know writing there will produce memory corruption but i'm just wondering if this can produce access violations at some point if the memory is just being read. I'd like to know since we are in the process of tracing an annoying access violation that happens with clients, which we can't reproduce and has no stack trace and the memory address of the EIP and accessed memory seems to lie beyond our programs memory limits.I wonder if we should focus on the those kind of range check errors or not to trace down that access violation, since the range checking system only mentions them when we actually go out of range. So there might be more code that does it which we haven't seen yet. (Just to be clear, the places where it did happen i already changed the code to prevent it from happening, but there might be other places where it happens)
ASKER CERTIFIED SOLUTION
Avatar of Thommy
Thommy
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Compile below test app both, with and without range checking, and check program behavior...

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

var
  t: array of string;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  t[0]:='123';
  t[1]:='456';
  t[2]:='xyz';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMessage('array size = '+inttostr(Length(t)));
  ShowMessage(t[0]);
  ShowMessage(t[1]);
  ShowMessage(t[2]);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  setlength(t,2);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  setlength(t,0);
end;

end.

Open in new window

SOLUTION
Avatar of jimyX
jimyX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial