{"record":{"id":"9e488b7430e33beb","repo":"dotnet/wpf","slug":"sr-countofbitsoutofrange","errorCode":null,"errorMessage":"SR.CountOfBitsOutOfRange","messagePattern":"SR\\.CountOfBitsOutOfRange","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/Ink/BitStream.cs","lineNumber":70,"sourceCode":"            : 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            {\n                int countToRead = (int)Native.BitsPerByte;\n                if (countOfBits < 8)\n                {\n                    countToRead = countOfBits;\n                }\n                //make room\n                retVal <<= countToRead;\n                byte b = ReadByte(countToRead);\n                retVal |= (long)b;\n                countOfBits -= countToRead;\n            }\n            return retVal;\n        }\n","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/Ink/BitStream.cs#L52-L88","documentation":"BitStreamReader.ReadUInt64 only supports reading 1-64 bits per call (Native.BitsPerLong). A countOfBits outside 1..64 throws ArgumentOutOfRangeException(SR.CountOfBitsOutOfRange). Multi-quadword reads and 0-bit reads are intentionally unsupported.","triggerScenarios":"Calling ReadUInt64(0), ReadUInt64(65), or with a bit count computed from external ISF data that exceeds 64.","commonSituations":"Decoding packet property bit widths from corrupted ISF where the declared width exceeds the supported maximum.","solutions":["Clamp countOfBits to 1..64, or split reads larger than 64 bits into multiple calls.","Validate the bit width before calling; skip or reject properties with unsupported widths.","Catch ArgumentOutOfRangeException around ReadUInt64 and treat the stream as corrupt."],"exampleFix":"// before\nlong v = reader.ReadUInt64(bitWidth);\n// after\nif (bitWidth < 1 || bitWidth > 64) throw new InvalidDataException(\"unsupported bit width\");\nlong v = reader.ReadUInt64(bitWidth);","handlingStrategy":"validation","validationCode":"if (countOfBits < 1 || countOfBits > 64)\n    throw new InvalidDataException($\"Bit width {countOfBits} outside supported 1-64 range.\");","typeGuard":"static bool IsValidUInt64BitCount(int bits) => bits >= 1 && bits <= 64;","tryCatchPattern":"try { long v = reader.ReadUInt64(bits); }\ncatch (ArgumentOutOfRangeException) { /* unsupported width: skip property or abort decode */ }","preventionTips":["Route widths >64 to multiple reads or a different decoder.","Sanitize bit widths decoded from ISF before use.","Guard against 0-bit reads, which are explicitly unsupported."],"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"}