SubtitleEdit/subtitleedit · error · ArgumentException

Stream is too small

Error message

Stream is too small

What it means

Thrown by the WaveHeader2 constructor when the supplied Stream does not contain at least ConstantHeaderSize (20) bytes of a RIFF/WAVE header. The reader seeks to position 0 and attempts a single Read into a 20-byte stackalloc buffer; if fewer bytes come back the stream cannot represent a valid WAV file, so parsing aborts before any header field is interpreted. This is an ArgumentException, not an end-of-stream IOException, because the precondition (a minimally-sized stream) was violated by the caller.

Source

Thrown at src/ui/Logic/Media/WaveToVisualizer2.cs:76

    public string DataId { get; private set; }

    /// <summary>
    /// Size of sound data
    /// </summary>
    public uint DataChunkSize { get; private set; }

    public int DataStartPosition { get; private set; }

    public WaveHeader2(Stream stream)
    {
        stream.Position = 0;

        // Read constant header
        Span<byte> buffer = stackalloc byte[ConstantHeaderSize];
        int bytesRead = stream.Read(buffer);
        if (bytesRead < buffer.Length)
        {
            throw new ArgumentException("Stream is too small");
        }

        // Parse constant header - use Span slicing to avoid array indexing
        ChunkId = Encoding.UTF8.GetString(buffer.Slice(0, 4));
        ChunkSize = BitConverter.ToUInt32(buffer.Slice(4));
        Format = Encoding.UTF8.GetString(buffer.Slice(8, 4));
        FmtId = Encoding.UTF8.GetString(buffer.Slice(12, 4));
        FmtChunkSize = BitConverter.ToInt32(buffer.Slice(16));

        // Read fmt chunk - allocate only if needed (usually 16-18 bytes, max ~40)
        Span<byte> fmtBuffer = FmtChunkSize <= 128
            ? stackalloc byte[FmtChunkSize]
            : new byte[FmtChunkSize];
        _ = stream.Read(fmtBuffer);

        // Parse fmt chunk
        AudioFormat = BitConverter.ToInt16(fmtBuffer);
        NumberOfChannels = BitConverter.ToInt16(fmtBuffer.Slice(2));

View on GitHub (pinned to 17a9f07487)

Solutions

  1. Verify the file extension and magic bytes (bytes 0-3 must be 'RIFF' and 8-11 'WAVE') before constructing WaveHeader2.
  2. Check stream.Length >= 44 (minimal WAV) before calling the constructor.
  3. If the stream may be incomplete, read fully into a MemoryStream first so the length is authoritative.
  4. Validate that the input is actually a PCM WAV rather than another audio container.

Example fix

// before
var header = new WaveHeader2(fileStream);

// after
if (fileStream.Length < 44) throw new InvalidDataException("Not a valid WAV file: too small");
var header = new WaveHeader2(fileStream);
Defensive patterns

Strategy: validation

Validate before calling

// before constructing WaveHeader2
if (stream == null) throw new ArgumentNullException(nameof(stream));
if (!stream.CanRead) throw new ArgumentException("Stream is not readable");
if (stream.Length < 44) throw new InvalidDataException("Stream is too small to be a WAV file");
stream.Position = 0;
Span<byte> magic = stackalloc byte[12];
stream.Read(magic);
stream.Position = 0;
if (Encoding.ASCII.GetString(magic.Slice(0,4)) != "RIFF" || Encoding.ASCII.GetString(magic.Slice(8,4)) != "WAVE")
    throw new InvalidDataException("Not a RIFF/WAVE stream");

Type guard

static bool IsValidWavStream(Stream s) => s != null && s.CanRead && s.Length >= 44;

Try / catch

try { var header = new WaveHeader2(stream); }
catch (ArgumentException ex) when (ex.Message == "Stream is too small") { /* prompt user for a valid WAV */ }

Prevention

When it happens

Trigger: Constructing new WaveHeader2(stream) where stream is empty, contains only a few bytes, points at a non-WAV file, or wraps a truncated/corrupt buffer. Also triggered when stream.Read returns a short count on the first call (e.g. a NetworkStream or CryptoStream with insufficient buffered data).

Common situations: Opening a .wav that is actually a text/MP3/FLAC file; a zero-byte placeholder file; a download that was cut off; passing a MemoryStream built from a non-audio byte array; reading a stream whose CanRead is true but which yields fewer bytes than Length suggests.

Related errors


AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13). Data as JSON: /api/errors/21796099456ab603. Report an issue: GitHub.