From 1498437e26b4508a4872790c7eb2474847e5b4a7 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Sat, 14 Nov 2026 22:32:26 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Validate=20fifo=20stream=20bound?= =?UTF-8?q?s=20and=20locking?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- UStreamHelper.pas | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/UStreamHelper.pas b/UStreamHelper.pas index 1b18100..ee57e66 100644 --- a/UStreamHelper.pas +++ b/UStreamHelper.pas @@ -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;