🐛 Guard fixed-length stream reads against short data
ReadStringFixed returned uninitialized memory on short reads and crashed on negative lengths from untrusted input.
This commit is contained in:
+7
-2
@@ -468,10 +468,15 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
function TStreamWrapper.ReadStringFixed(ALength: Integer): string;
|
function TStreamWrapper.ReadStringFixed(ALength: Integer): string;
|
||||||
|
var
|
||||||
|
bytesRead: Integer;
|
||||||
begin
|
begin
|
||||||
if not Assigned(FStream) then Exit('');
|
if (not Assigned(FStream)) or (ALength <= 0) then Exit('');
|
||||||
SetLength(Result, ALength);
|
SetLength(Result, ALength);
|
||||||
FStream.Read(PChar(Result)^, ALength);
|
FillChar(PChar(Result)^, ALength, 0);
|
||||||
|
bytesRead := FStream.Read(PChar(Result)^, ALength);
|
||||||
|
if bytesRead < ALength then
|
||||||
|
SetLength(Result, bytesRead);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TStreamWrapper.WriteBoolean(AValue: Boolean);
|
procedure TStreamWrapper.WriteBoolean(AValue: Boolean);
|
||||||
|
|||||||
Reference in New Issue
Block a user