dotnet/wpf · error · ArgumentOutOfRangeException

SR.InvalidBufferLength

Error message

SR.InvalidBufferLength

What it means

The BitStreamReader constructor validates that bufferLengthInBits does not exceed the buffer's capacity (buffer.Length * 8 bits). Asking for more bits than the buffer holds would cause out-of-bounds reads during bit extraction, so it throws ArgumentOutOfRangeException(SR.InvalidBufferLength).

Solutions

  1. Pass Math.Min(bufferLengthInBits, (uint)(buffer.Length * 8)) as the bit length.
  2. Verify the source data is complete before constructing the reader (check the payload length against the declared bit count).
  3. Catch ArgumentOutOfRangeException around construction and treat the payload as corrupt.

Example fix

// before
var reader = new BitStreamReader(buffer, declaredBits);
// after
uint maxBits = (uint)(buffer.Length * 8);
var reader = new BitStreamReader(buffer, Math.Min(declaredBits, maxBits));
Defensive patterns

Strategy: validation

Validate before calling

uint maxBits = (uint)(buffer.Length * 8);
if (bufferLengthInBits > maxBits)
    bufferLengthInBits = maxBits; // or reject the payload

Type guard

static bool IsValidBitLength(byte[] buffer, uint bits) =>
    buffer != null && bits <= (uint)buffer.Length * 8;

Try / catch

try { var reader = new BitStreamReader(buffer, bits); }
catch (ArgumentOutOfRangeException) { /* payload declares more bits than data holds: treat as corrupt */ }

Prevention

When it happens

Trigger: Constructing BitStreamReader with a bit length taken from an external structure (e.g. an ink stroke packet description) that claims more bits than the supplied byte array contains.

Common situations: Parsing ISF (Ink Serialized Format) data where a corrupted or truncated payload declares a bit length larger than the actual data buffer.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/6244966acc493ec9. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/Ink/BitStream.cs:56

            ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(startIndex, buffer.Length);

            _byteArray = buffer;
            _byteArrayIndex = startIndex;
            _bufferLengthInBits = (uint)(buffer.Length - startIndex) * (uint)Native.BitsPerByte;
        }

        /// <summary>
        /// Create a new BitStreamReader to unpack the bits in a buffer of bytes
        /// and enforce a maximum buffer read length
        /// </summary>
        /// <param name="buffer">Buffer of bytes</param>
        /// <param name="bufferLengthInBits">Maximum number of bytes to read from the buffer</param>
        internal BitStreamReader(byte[] buffer, uint bufferLengthInBits)
            : this(buffer)
        {
            if (bufferLengthInBits > (buffer.Length * Native.BitsPerByte))
            {
                throw new ArgumentOutOfRangeException(nameof(bufferLengthInBits), SR.InvalidBufferLength);
            }

            _bufferLengthInBits = bufferLengthInBits;
        }

        /// <summary>
        /// Read a specified number of bits from the stream into a long
        /// </summary>
        internal long ReadUInt64(int countOfBits)
        {
            // we only support 1-64 bits currently, not multiple bytes, and not 0 bits
            if (countOfBits > Native.BitsPerLong || countOfBits <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(countOfBits), countOfBits, SR.CountOfBitsOutOfRange);
            }
            long retVal = 0;
            while (countOfBits > 0)
            {

View on GitHub (pinned to 81131a70a4)