THRIFT-4323 range check errors or NPE in edge cases
Client: Delphi
Patch: Jens Geyer
diff --git a/lib/delphi/src/Thrift.Stream.pas b/lib/delphi/src/Thrift.Stream.pas
index 1d357c3..b6e0cbf 100644
--- a/lib/delphi/src/Thrift.Stream.pas
+++ b/lib/delphi/src/Thrift.Stream.pas
@@ -197,7 +197,9 @@
function TThriftStreamImpl.Read(var buffer: TBytes; offset, count: Integer): Integer;
begin
- Result := Read( @buffer[0], Length(buffer), offset, count);
+ if Length(buffer) > 0
+ then Result := Read( @buffer[0], Length(buffer), offset, count)
+ else Result := 0;
end;
function TThriftStreamImpl.Read( const pBuf : Pointer; const buflen : Integer; offset: Integer; count: Integer): Integer;
@@ -208,7 +210,8 @@
procedure TThriftStreamImpl.Write(const buffer: TBytes; offset, count: Integer);
begin
- Write( @buffer[0], offset, count);
+ if Length(buffer) > 0
+ then Write( @buffer[0], offset, count);
end;
procedure TThriftStreamImpl.Write( const pBuf : Pointer; offset: Integer; count: Integer);
diff --git a/lib/delphi/src/Thrift.Transport.pas b/lib/delphi/src/Thrift.Transport.pas
index d20eb2f..52b617b 100644
--- a/lib/delphi/src/Thrift.Transport.pas
+++ b/lib/delphi/src/Thrift.Transport.pas
@@ -414,22 +414,28 @@
function TTransportImpl.Read(var buf: TBytes; off: Integer; len: Integer): Integer;
begin
- result := Read( @buf[0], Length(buf), off, len);
+ if Length(buf) > 0
+ then result := Read( @buf[0], Length(buf), off, len)
+ else result := 0;
end;
function TTransportImpl.ReadAll(var buf: TBytes; off: Integer; len: Integer): Integer;
begin
- result := ReadAll( @buf[0], Length(buf), off, len);
+ if Length(buf) > 0
+ then result := ReadAll( @buf[0], Length(buf), off, len)
+ else result := 0;
end;
procedure TTransportImpl.Write( const buf: TBytes);
begin
- Write( @buf[0], 0, Length(buf));
+ if Length(buf) > 0
+ then Write( @buf[0], 0, Length(buf));
end;
procedure TTransportImpl.Write( const buf: TBytes; off: Integer; len: Integer);
begin
- Write( @buf[0], off, len);
+ if Length(buf) > 0
+ then Write( @buf[0], off, len);
end;
function TTransportImpl.ReadAll(const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer): Integer;
@@ -1282,7 +1288,8 @@
FTransport.ReadAll( buff, 0, size );
FReadBuffer.Free;
FReadBuffer := TMemoryStream.Create;
- FReadBuffer.Write( Pointer(@buff[0])^, size );
+ if Length(buff) > 0
+ then FReadBuffer.Write( Pointer(@buff[0])^, size );
FReadBuffer.Position := 0;
end;