QL-Win/QuickLook · error · EndOfStreamException

The stream has fewer bytes available than requested.

Error message

The stream has fewer bytes available than requested.

What it means

Thrown by Stream.GetBytes when stream.Read returns fewer bytes than requested (bytesRead < count). After skipping to the target offset it tries to read `count` bytes into the buffer; a short read means the stream ended early, so the section/structure being extracted is incomplete. EndOfStreamException signals truncation.

Source

Thrown at QuickLook.Plugin/QuickLook.Plugin.PEViewer/PEImageParser/ImageSection.cs:99

        if (bytesToSkip < 0)
        {
            throw new ArgumentException("Offset is before the current position in the stream.");
        }

        // Skip bytes until reaching the target offset
        while (bytesToSkip > 0)
        {
            int bytesSkipped = (int)Math.Min(bytesToSkip, int.MaxValue);
            stream.Read(new byte[bytesSkipped], 0, bytesSkipped);
            bytesToSkip -= bytesSkipped;
        }

        // Read the required number of bytes
        int bytesRead = stream.Read(buffer, 0, count);
        if (bytesRead < count)
        {
            throw new EndOfStreamException("The stream has fewer bytes available than requested.");
        }

        return buffer;
    }
}

View on GitHub (pinned to cb5d9c429c)

Solutions

  1. Verify the file/stream is complete (length check) before parsing; re-download truncated files.
  2. Cap `count` to the remaining bytes: Math.Min(count, stream.Length - index) and tolerate partial reads when possible.
  3. Ensure decompression/network code delivers the full stream before PE parsing begins.
  4. Catch EndOfStreamException and report 'truncated PE' rather than continuing with partial data.

Example fix

// before
int bytesRead = stream.Read(buffer, 0, count);
if (bytesRead < count)
    throw new EndOfStreamException("The stream has fewer bytes available than requested.");

// after — read fully, tolerate exact EOF only when expected
int total = 0;
while (total < count)
{
    int n = stream.Read(buffer, total, count - total);
    if (n == 0) throw new EndOfStreamException(...);
    total += n;
}
Defensive patterns

Strategy: validation

Validate before calling

if (stream.CanSeek && index + count > stream.Length) count = (int)(stream.Length - index);
// then read in a loop until count or EOF

Try / catch

try { bytes = stream.GetBytes(index, count); }
catch (EndOfStreamException) { /* truncated; report incomplete PE */ }

Prevention

When it happens

Trigger: A PE section's declared size (or any requested byte count) extends past the actual end of data in the stream; Read returns fewer bytes because EOF was reached mid-read.

Common situations: A truncated PE/stream (same root as [32]/[33]); a section SizeOfRawData larger than the remaining bytes; a network/decompressed stream that ended before delivering the full image; a computed `count` that overflows past EOF.

Related errors


AI-assisted analysis of QL-Win/QuickLook@cb5d9c429c (2026-08-13). Data as JSON: /api/errors/f00805950d5b356f. Report an issue: GitHub.