Reputation: 530
The reason I say this, is that recently. Old library code that has been in our project for many years, has recently began generating range checks. In the past, this code built and ran without issue.
Here's an example scenario that's like one that occurred today.
function DoStringStuff(const S: string): string;
var
i: Integer;
len: Integer;
begin
...
while (Result[i] <> #32)
and (i <= len) do
begin
Result[i] := UpperCase(Result[i])[1];
inc(i);
end;
...
end;
When i
is >len
a range check occurs. I fixed it by moving the length check to the beginning. However, I find it strange that it worked at all given the order that's it's evaluating things, where it will ultimately attempt to evaluate an index beyond the string's range.
Any ideas and or suggestions are welcome.
Upvotes: 1
Views: 150
Reputation: 612993
That code was always broken and if it ever worked then that was purely by chance. There's no compiler switch that will find such a defect at compile time.
In all versions of Delphi, when range checks are enabled, run time errors will be generated when you access beyond the end of the string. If your code code ran without run time errors before then there are but two explanations:
Upvotes: 2