🐛 Validate fifo stream bounds and locking
Seek accepted positions outside the queue, allowing reads before the buffer, and negative counts reached Move with garbage sizes. Writing to a locked stream silently dropped the lock window instead of failing, which corrupts packet parsing.
This commit is contained in:
+12
-3
@@ -229,6 +229,8 @@ end;
|
||||
|
||||
function TFifoStream.Read(var Buffer; ACount: Longint): Longint;
|
||||
begin
|
||||
if ACount < 0 then
|
||||
raise EStreamError.Create('Invalid read count');
|
||||
Result := 0;
|
||||
If (FSize > 0) and (FPosition - FLockOffset < FSize) then
|
||||
begin
|
||||
@@ -243,7 +245,10 @@ function TFifoStream.Write(const Buffer; ACount: Longint): Longint;
|
||||
var
|
||||
NewPos: Longint;
|
||||
begin
|
||||
Unlock;
|
||||
if ACount < 0 then
|
||||
raise EStreamError.Create('Invalid write count');
|
||||
if (FLockOffset <> 0) or (FRealSize <> 0) then
|
||||
raise EStreamError.Create('Cannot write to a locked fifo stream');
|
||||
if ACount = 0 then
|
||||
Exit(0);
|
||||
NewPos := FPosition + ACount;
|
||||
@@ -265,6 +270,8 @@ begin
|
||||
soFromEnd : FPosition := FSize + AOffset + FLockOffset;
|
||||
soFromCurrent : FPosition := FPosition + AOffset;
|
||||
end;
|
||||
if (FPosition < FLockOffset) or (FPosition > FSize + FLockOffset) then
|
||||
raise EStreamError.Create('Invalid seek position');
|
||||
Result := FPosition - FLockOffset;
|
||||
end;
|
||||
|
||||
@@ -328,6 +335,8 @@ var
|
||||
newCapacity, newSize: Longint;
|
||||
queue, newMemory: Pointer;
|
||||
begin
|
||||
if ACount < 0 then
|
||||
raise EStreamError.Create('Invalid dequeue count');
|
||||
Unlock;
|
||||
if ACount >= FSize then
|
||||
begin
|
||||
@@ -343,8 +352,6 @@ begin
|
||||
begin
|
||||
newMemory := GetMem(newCapacity);
|
||||
System.Move(queue^, newMemory^, newSize);
|
||||
if (newMemory = nil) and (newCapacity > 0) then
|
||||
raise EStreamError.Create(SMemoryStreamError);
|
||||
FreeMem(FMemory);
|
||||
FMemory := newMemory;
|
||||
FCapacity := newCapacity;
|
||||
@@ -362,6 +369,8 @@ procedure TFifoStream.Enqueue(const Buffer; ACount: Longint);
|
||||
var
|
||||
oldPos: Int64;
|
||||
begin
|
||||
if ACount < 0 then
|
||||
raise EStreamError.Create('Invalid enqueue count');
|
||||
Unlock;
|
||||
oldPos := FPosition;
|
||||
FPosition := FSize;
|
||||
|
||||
Reference in New Issue
Block a user