🐛 Fix out-of-bounds reads in stream string readers

The null and wide-string readers tested the terminator before the
bounds check, peeked one character past the window, and used a
lock-relative position against the absolute memory base. They also
advanced past a terminator that was never read, desyncing the parser
on malformed input. Reworked them to stay in bounds and to build the
result in linear time.
This commit is contained in:
2026-11-15 22:06:50 +01:00
parent f381647149
commit 585fd92611
+75 -31
View File
@@ -115,61 +115,105 @@ end;
function TEnhancedMemoryStream.ReadStringBigUniNull: WideString; function TEnhancedMemoryStream.ReadStringBigUniNull: WideString;
var var
buffer: PWideChar; buffer: PWideChar;
length: Integer; i, j, maxChars: Integer;
begin begin
Result := ''; Result := '';
buffer := Pointer(PtrInt(Memory) + Position); buffer := Pointer(PtrInt(FMemory) + FPosition);
length := 0; maxChars := (FSize - (FPosition - FLockOffset)) div 2;
while (buffer[length] <> #0) and (length < (Size - Position)) do if maxChars <= 0 then Exit;
SetLength(Result, maxChars * 2);
i := 0;
j := 0;
while (i < maxChars) and (buffer[i] <> #0) do
begin begin
if (SwapWideChar(buffer[length]) = #10) and (SwapWideChar(buffer[length - 1]) <> #13) then if (SwapWideChar(buffer[i]) = #10) and ((i = 0) or (SwapWideChar(buffer[i - 1]) <> #13)) then
Result := Result + #13; begin
Result := Result + SwapWideChar(buffer[length]); Inc(j);
if (SwapWideChar(buffer[length]) = #13) and (SwapWideChar(buffer[length + 1]) <> #10) then Result[j] := #13;
Result := Result + #10;
inc(length);
end; end;
Position := Position + (Length + 1) * 2; Inc(j);
Result[j] := SwapWideChar(buffer[i]);
if (SwapWideChar(buffer[i]) = #13) and ((i + 1 >= maxChars) or (SwapWideChar(buffer[i + 1]) <> #10)) then
begin
Inc(j);
Result[j] := #10;
end;
Inc(i);
end;
SetLength(Result, j);
if i < maxChars then
FPosition := FPosition + (i + 1) * 2
else
FPosition := FPosition + i * 2;
end; end;
function TEnhancedMemoryStream.ReadStringFixed(ALength: Integer): string; function TEnhancedMemoryStream.ReadStringFixed(ALength: Integer): string;
var var
buffer: PChar; buffer: PChar;
length: Integer; i, j: Integer;
begin begin
Result := ''; Result := '';
buffer := Pointer(PtrInt(FMemory) + FPosition); buffer := Pointer(PtrInt(FMemory) + FPosition);
length := 0; if ALength <= 0 then Exit;
while (length < ALength) and (length < (FSize - (FPosition - FLockOffset))) do if ALength > (FSize - (FPosition - FLockOffset)) then
ALength := FSize - (FPosition - FLockOffset);
if ALength <= 0 then Exit;
SetLength(Result, ALength * 2);
i := 0;
j := 0;
while i < ALength do
begin begin
if (buffer[length] = #10) and (buffer[length - 1] <> #13) then if (buffer[i] = #10) and ((i = 0) or (buffer[i - 1] <> #13)) then
Result := Result + #13; begin
Result := Result + buffer[length]; Inc(j);
if (buffer[length] = #13) and (buffer[length + 1] <> #10) then Result[j] := #13;
Result := Result + #10;
inc(length);
end; end;
FPosition := FPosition + length + 1; Inc(j);
Result[j] := buffer[i];
if (buffer[i] = #13) and ((i + 1 >= ALength) or (buffer[i + 1] <> #10)) then
begin
Inc(j);
Result[j] := #10;
end;
Inc(i);
end;
SetLength(Result, j);
FPosition := FPosition + i;
end; end;
function TEnhancedMemoryStream.ReadStringNull: string; function TEnhancedMemoryStream.ReadStringNull: string;
var var
buffer: PByteArray; buffer: PByteArray;
length: Integer; i, j, maxLen: Integer;
begin begin
Result := ''; Result := '';
buffer := Pointer(PtrInt(FMemory) + FPosition); buffer := Pointer(PtrInt(FMemory) + FPosition);
length := 0; maxLen := FSize - (FPosition - FLockOffset);
while (buffer^[length] <> 0) and (length < (FSize - (FPosition - FLockOffset))) do if maxLen <= 0 then Exit;
SetLength(Result, maxLen * 2);
i := 0;
j := 0;
while (i < maxLen) and (buffer^[i] <> 0) do
begin begin
if (buffer^[length] = 10) and (buffer^[length - 1] <> 13) then if (buffer^[i] = 10) and ((i = 0) or (buffer^[i - 1] <> 13)) then
Result := Result + #13; begin
Result := Result + Char(buffer^[length]); Inc(j);
if (buffer^[length] = 13) and (buffer^[length + 1] <> 10) then Result[j] := #13;
Result := Result + #10;
inc(length);
end; end;
FPosition := FPosition + length + 1; Inc(j);
Result[j] := Char(buffer^[i]);
if (buffer^[i] = 13) and ((i + 1 >= maxLen) or (buffer^[i + 1] <> 10)) then
begin
Inc(j);
Result[j] := #10;
end;
Inc(i);
end;
SetLength(Result, j);
if i < maxLen then
FPosition := FPosition + i + 1
else
FPosition := FPosition + i;
end; end;
function TEnhancedMemoryStream.ReadWord: Word; function TEnhancedMemoryStream.ReadWord: Word;