{"record":{"id":"6244966acc493ec9","repo":"dotnet/wpf","slug":"sr-invalidbufferlength","errorCode":null,"errorMessage":"SR.InvalidBufferLength","messagePattern":"SR\\.InvalidBufferLength","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/Ink/BitStream.cs","lineNumber":56,"sourceCode":"            ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(startIndex, buffer.Length);\n\n            _byteArray = buffer;\n            _byteArrayIndex = startIndex;\n            _bufferLengthInBits = (uint)(buffer.Length - startIndex) * (uint)Native.BitsPerByte;\n        }\n\n        /// <summary>\n        /// Create a new BitStreamReader to unpack the bits in a buffer of bytes\n        /// and enforce a maximum buffer read length\n        /// </summary>\n        /// <param name=\"buffer\">Buffer of bytes</param>\n        /// <param name=\"bufferLengthInBits\">Maximum number of bytes to read from the buffer</param>\n        internal BitStreamReader(byte[] buffer, uint bufferLengthInBits)\n            : this(buffer)\n        {\n            if (bufferLengthInBits > (buffer.Length * Native.BitsPerByte))\n            {\n                throw new ArgumentOutOfRangeException(nameof(bufferLengthInBits), SR.InvalidBufferLength);\n            }\n\n            _bufferLengthInBits = bufferLengthInBits;\n        }\n\n        /// <summary>\n        /// Read a specified number of bits from the stream into a long\n        /// </summary>\n        internal long ReadUInt64(int countOfBits)\n        {\n            // we only support 1-64 bits currently, not multiple bytes, and not 0 bits\n            if (countOfBits > Native.BitsPerLong || countOfBits <= 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(countOfBits), countOfBits, SR.CountOfBitsOutOfRange);\n            }\n            long retVal = 0;\n            while (countOfBits > 0)\n            {","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/Ink/BitStream.cs#L38-L74","documentation":"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).","triggerScenarios":"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.","commonSituations":"Parsing ISF (Ink Serialized Format) data where a corrupted or truncated payload declares a bit length larger than the actual data buffer.","solutions":["Pass Math.Min(bufferLengthInBits, (uint)(buffer.Length * 8)) as the bit length.","Verify the source data is complete before constructing the reader (check the payload length against the declared bit count).","Catch ArgumentOutOfRangeException around construction and treat the payload as corrupt."],"exampleFix":"// before\nvar reader = new BitStreamReader(buffer, declaredBits);\n// after\nuint maxBits = (uint)(buffer.Length * 8);\nvar reader = new BitStreamReader(buffer, Math.Min(declaredBits, maxBits));","handlingStrategy":"validation","validationCode":"uint maxBits = (uint)(buffer.Length * 8);\nif (bufferLengthInBits > maxBits)\n    bufferLengthInBits = maxBits; // or reject the payload","typeGuard":"static bool IsValidBitLength(byte[] buffer, uint bits) =>\n    buffer != null && bits <= (uint)buffer.Length * 8;","tryCatchPattern":"try { var reader = new BitStreamReader(buffer, bits); }\ncatch (ArgumentOutOfRangeException) { /* payload declares more bits than data holds: treat as corrupt */ }","preventionTips":["Cross-check declared bit lengths against actual payload size when parsing ISF.","Truncate declared lengths to the available data instead of trusting headers.","Treat oversized declarations as a sign of truncated/corrupt input."],"tags":["ink","bit-stream","argument-validation","isf"],"backgroundTag":"value-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}