Strange codegen for null open array
Original Reporter info from Mantis: SergeAnvarov @SergeAnvarov
-
Reporter name: Serge Anvarov
Original Reporter info from Mantis: SergeAnvarov @SergeAnvarov
- Reporter name: Serge Anvarov
Description:
Compiler accept null array as input for function with open array parameter. But generated strange code.
Code in Steps To Reproduce.
-
If uncomment Case1, then program do access error (as expected). But even with range checking not detected incorrect index, because compare (in asm code) unsigned values, so 0 <= Cardinal(-1).
-
If uncomment Case2 (Case1 stay commented) then I expected the same as Case1, but it will be run OK. No error! Compiler allocate new (not null) array on stack with first value 0 and length equal zero (i.e. High=-1) instead of having to pass a nil.
Steps to reproduce:
program Project1;
{$APPTYPE CONSOLE}
function SumX(const Arr: array of Integer): Integer;
begin
Result := Arr[Low(Arr)];
end;
var
P: Pointer;
begin
P := nil;
// Writeln(SumX(TBoundArray(P))); // Case 1
// Writeln(SumX(TBoundArray(nil))); // Case 2
Readln;
end.
Additional information:
By the way, Delphi (tested on Delphi7 and Delphi 10.1 Berlin. For 10.1 replace Integer with NativeInt for compile) do:
- Case1 - range check error (
R+) or access violation (
R-}
- Case2 - not compiled with error "Constants cannot be used as open array arguments".
Mantis conversion info:
- Mantis ID: 31273
- OS: Windows
- OS Build: 7
- Platform: Windows
- Version: 3.0.0
- Fixed in version: 3.1.1
- Fixed in revision: 37885 (#dcac6b9c)