🐛 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;
|
function TFifoStream.Read(var Buffer; ACount: Longint): Longint;
|
||||||
begin
|
begin
|
||||||
|
if ACount < 0 then
|
||||||
|
raise EStreamError.Create('Invalid read count');
|
||||||
Result := 0;
|
Result := 0;
|
||||||
If (FSize > 0) and (FPosition - FLockOffset < FSize) then
|
If (FSize > 0) and (FPosition - FLockOffset < FSize) then
|
||||||
begin
|
begin
|
||||||
@@ -243,7 +245,10 @@ function TFifoStream.Write(const Buffer; ACount: Longint): Longint;
|
|||||||
var
|
var
|
||||||
NewPos: Longint;
|
NewPos: Longint;
|
||||||
begin
|
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
|
if ACount = 0 then
|
||||||
Exit(0);
|
Exit(0);
|
||||||
NewPos := FPosition + ACount;
|
NewPos := FPosition + ACount;
|
||||||
@@ -265,6 +270,8 @@ begin
|
|||||||
soFromEnd : FPosition := FSize + AOffset + FLockOffset;
|
soFromEnd : FPosition := FSize + AOffset + FLockOffset;
|
||||||
soFromCurrent : FPosition := FPosition + AOffset;
|
soFromCurrent : FPosition := FPosition + AOffset;
|
||||||
end;
|
end;
|
||||||
|
if (FPosition < FLockOffset) or (FPosition > FSize + FLockOffset) then
|
||||||
|
raise EStreamError.Create('Invalid seek position');
|
||||||
Result := FPosition - FLockOffset;
|
Result := FPosition - FLockOffset;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@@ -328,6 +335,8 @@ var
|
|||||||
newCapacity, newSize: Longint;
|
newCapacity, newSize: Longint;
|
||||||
queue, newMemory: Pointer;
|
queue, newMemory: Pointer;
|
||||||
begin
|
begin
|
||||||
|
if ACount < 0 then
|
||||||
|
raise EStreamError.Create('Invalid dequeue count');
|
||||||
Unlock;
|
Unlock;
|
||||||
if ACount >= FSize then
|
if ACount >= FSize then
|
||||||
begin
|
begin
|
||||||
@@ -343,8 +352,6 @@ begin
|
|||||||
begin
|
begin
|
||||||
newMemory := GetMem(newCapacity);
|
newMemory := GetMem(newCapacity);
|
||||||
System.Move(queue^, newMemory^, newSize);
|
System.Move(queue^, newMemory^, newSize);
|
||||||
if (newMemory = nil) and (newCapacity > 0) then
|
|
||||||
raise EStreamError.Create(SMemoryStreamError);
|
|
||||||
FreeMem(FMemory);
|
FreeMem(FMemory);
|
||||||
FMemory := newMemory;
|
FMemory := newMemory;
|
||||||
FCapacity := newCapacity;
|
FCapacity := newCapacity;
|
||||||
@@ -362,6 +369,8 @@ procedure TFifoStream.Enqueue(const Buffer; ACount: Longint);
|
|||||||
var
|
var
|
||||||
oldPos: Int64;
|
oldPos: Int64;
|
||||||
begin
|
begin
|
||||||
|
if ACount < 0 then
|
||||||
|
raise EStreamError.Create('Invalid enqueue count');
|
||||||
Unlock;
|
Unlock;
|
||||||
oldPos := FPosition;
|
oldPos := FPosition;
|
||||||
FPosition := FSize;
|
FPosition := FSize;
|
||||||
|
|||||||
Reference in New Issue
Block a user