Delphi allows dereferencing a pointer to a [0..0] static array and accessing it with indices > 0, however 0035671 breaks this.
Original Reporter info from Mantis: Akira1364
-
Reporter name:
Original Reporter info from Mantis: Akira1364
- Reporter name:
Description:
Please note: Personally, I think the particular pattern I'm describing here is both stupid and nonsensical, and I've never been particularly sure why anyone would write something that way, however I thought I'd point this out as there's (unfortunately IMO) a lot of code out there that does it.
That said, what I mean is, in Delphi, the following example compiles fine:
program Example;
{$ifdef FPC}
{$mode Delphi}
uses SysUtils;
{$else}
{$AppType Console}
uses System.SysUtils;
{$endif}
type
TMyPoint = record
X: Single;
Y: Single;
end;
TMyPoints = array[0..0] of TMyPoint;
PMyPoints = ^TMyPoints;
var
P: PMyPoints;
I: NativeUInt;
begin
GetMem(P, 5 * SizeOf(TMyPoint));
for I := 0 to 4 do begin
with P^[I] do begin
X := 12.0;
Y := 24.0;
end;
end;
for I := 0 to 4 do begin
with P^[I] do
WriteLn(Format('[X: %f, Y: %f]', [X, Y]));
end;
FreeMem(P, 5 * SizeOf(TMyPoint));
end.
Again, it clearly does not really make sense that this works, because by doing "P^[I]" you are explicitly accessing a static array that explicitly does not have valid indices higher than zero, but for reasons that might not even be intentional, Delphi has always allowed this and does not even warn about it.
FPC has always at least warned about it (as it should!) but after 0035671 now unavoidably errors out in {$mode Delphi}.
Personally I'm not concerned if the FPC team does not want to change this behavior further, as I think it's good to discourage writing that kind of thing in the first place (and I'd even honestly like to see {$mode ObjFPC} error out on it as well!), however, there are probably some people out there who will complain, so like I said I figured I would point it out.
Steps to reproduce:
Compile the provided example program.
Mantis conversion info:
- Mantis ID: 35753
- OS: Windows
- OS Build: 10
- Build: 42272
- Platform: x86_64
- Version: 3.3.1
- Fixed in version: 3.3.1
- Fixed in revision: 42275 (#c038e4c3)
- Monitored by: » Akira1364 (Akira1364)