🐛 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;
var
buffer: PWideChar;
length: Integer;
i, j, maxChars: Integer;
begin
Result := '';
buffer := Pointer(PtrInt(Memory) + Position);
length := 0;
while (buffer[length] <> #0) and (length < (Size - Position)) do
buffer := Pointer(PtrInt(FMemory) + FPosition);
maxChars := (FSize - (FPosition - FLockOffset)) div 2;
if maxChars <= 0 then Exit;
SetLength(Result, maxChars * 2);
i := 0;
j := 0;
while (i < maxChars) and (buffer[i] <> #0) do
begin
if (SwapWideChar(buffer[length]) = #10) and (SwapWideChar(buffer[length - 1]) <> #13) then
Result := Result + #13;
Result := Result + SwapWideChar(buffer[length]);
if (SwapWideChar(buffer[length]) = #13) and (SwapWideChar(buffer[length + 1]) <> #10) then
Result := Result + #10;
inc(length);
if (SwapWideChar(buffer[i]) = #10) and ((i = 0) or (SwapWideChar(buffer[i - 1]) <> #13)) then
begin
Inc(j);
Result[j] := #13;
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;
function TEnhancedMemoryStream.ReadStringFixed(ALength: Integer): string;
var
buffer: PChar;
length: Integer;
i, j: Integer;
begin
Result := '';
buffer := Pointer(PtrInt(FMemory) + FPosition);
length := 0;
while (length < ALength) and (length < (FSize - (FPosition - FLockOffset))) do
if ALength <= 0 then Exit;
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
if (buffer[length] = #10) and (buffer[length - 1] <> #13) then
Result := Result + #13;
Result := Result + buffer[length];
if (buffer[length] = #13) and (buffer[length + 1] <> #10) then
Result := Result + #10;
inc(length);
if (buffer[i] = #10) and ((i = 0) or (buffer[i - 1] <> #13)) then
begin
Inc(j);
Result[j] := #13;
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;
function TEnhancedMemoryStream.ReadStringNull: string;
var
buffer: PByteArray;
length: Integer;
i, j, maxLen: Integer;
begin
Result := '';
buffer := Pointer(PtrInt(FMemory) + FPosition);
length := 0;
while (buffer^[length] <> 0) and (length < (FSize - (FPosition - FLockOffset))) do
maxLen := FSize - (FPosition - FLockOffset);
if maxLen <= 0 then Exit;
SetLength(Result, maxLen * 2);
i := 0;
j := 0;
while (i < maxLen) and (buffer^[i] <> 0) do
begin
if (buffer^[length] = 10) and (buffer^[length - 1] <> 13) then
Result := Result + #13;
Result := Result + Char(buffer^[length]);
if (buffer^[length] = 13) and (buffer^[length + 1] <> 10) then
Result := Result + #10;
inc(length);
if (buffer^[i] = 10) and ((i = 0) or (buffer^[i - 1] <> 13)) then
begin
Inc(j);
Result[j] := #13;
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;
function TEnhancedMemoryStream.ReadWord: Word;