TFile.ReadAllBytes is broken in fixes 3.2

TFile.ReadAllBytes (in System.IOUtils) returns TBytes filled with zero's.

It's implemented as
(See: https://gitlab.com/freepascal.org/fpc/source/-/blob/fixes_3_2/packages/vcl-compat/src/system.ioutils.pp?ref_type=heads)

class function TFile.ReadAllBytes(const aPath: string): TBytes;

begin
  Result:=[];
  With OpenRead(aPath) do
    try
       SetLength(Result,Size);
       ReadBuffer(Result,0);  << second param should be "Size"
    finally
      Free;
    end;
end;

This is fixed in main in 9eb77599

I think it's not been merged to fixes branch.

Furhter investigation shows that fixing the issue as described in the code example then leads to memory corruption and immediate AV when accessing the result.
This seems to be caused by the compiler not picking the correct overload (as explained by Maichael on ML, a fix that won't be backported to fixes branch.)

The correct fix for ReadAllBytes would be:

class function TFile.ReadAllBytes(const aPath: string): TBytes;

begin
  Result:=[];
  With OpenRead(aPath) do
    try
       SetLength(Result,Size);
       ReadBuffer(Result[0],Size); 
    finally
      Free;
    end;
end;

If so desired I can create a patch.

Notice though that I did test this fix by copying the entire unit to a new unit and testing it stand-alone. I was too lazy to patch 3.2.4RC1 and rebuild it from sources.

Notice also that as a result of this issue, TFile.ReadAllText will always retrun an empty string.

Edited by Bart
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information