{"record":{"id":"786d98b92b27ecae","repo":"dotnet/wpf","slug":"sr-endofstreamreached","errorCode":null,"errorMessage":"SR.EndOfStreamReached","messagePattern":"SR\\.EndOfStreamReached","errorType":"exception","errorClass":"System.IO.EndOfStreamException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/Ink/BitStream.cs","lineNumber":230,"sourceCode":"        {\n            byte b = ReadByte(1);\n            return ((b & 1) == 1);\n        }\n\n        /// <summary>\n        /// Read a specified number of bits from the stream into a single byte\n        /// </summary>\n        /// <param name=\"countOfBits\">The number of bits to unpack</param>\n        /// <returns>A single byte that contains up to 8 packed bits</returns>\n        /// <remarks>For example, if 2 bits are read from the stream, then a full byte\n        /// will be created with the least significant bits set to the 2 unpacked bits\n        /// from the stream</remarks>\n        internal byte ReadByte(int countOfBits)\n        {\n            // if the end of the stream has been reached, then throw an exception\n            if (EndOfStream)\n            {\n                throw new System.IO.EndOfStreamException(SR.EndOfStreamReached);\n            }\n\n            // we only support 1-8 bits currently, not multiple bytes, and not 0 bits\n            if (countOfBits > Native.BitsPerByte || countOfBits <= 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(countOfBits), countOfBits, SR.CountOfBitsOutOfRange);\n            }\n\n            if (countOfBits > _bufferLengthInBits)\n            {\n                throw new ArgumentOutOfRangeException(nameof(countOfBits), countOfBits, SR.CountOfBitsGreatThanRemainingBits);\n            }\n\n            _bufferLengthInBits -= (uint)countOfBits;\n\n            // initialize return byte to 0 before reading from the cache\n            byte returnByte = 0;\n","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/Ink/BitStream.cs#L212-L248","documentation":"BitStream.ReadByte attempts to read 1-8 bits from the underlying stream and throws EndOfStreamException(SR.EndOfStreamReached) when the stream is already exhausted (EndOfStream is true). The library signals that the caller asked for more bit data than the stream contains rather than returning a partial/zero value.","triggerScenarios":"Calling ReadByte after all bytes in the stream have been consumed. Callers include GetDataFromReader, Uncompress, LoadPackets, and the indexer (b); truncated or mis-lengthed ISF/stroke data makes decoding overrun the buffer.","commonSituations":"Parsing a truncated or corrupted ISF file (incomplete download, bad save); a decoder loop that iterates a packet count larger than the data present.","solutions":["Check BitStream.EndOfStream before each ReadByte call and stop decoding when true.","Validate that the ISF/stream data is complete (correct total length) before decoding.","Catch EndOfStreamException around ISF decoding and treat the input as truncated/corrupt.","Fix the loop bounds so the number of reads matches the actual byte/bit length of the stream."],"exampleFix":"// before\nwhile (i < packetCount)\n    packets[i] = bitStream.ReadByte(8); // may overrun a short stream\n// after\nwhile (i < packetCount && !bitStream.EndOfStream)\n    packets[i] = bitStream.ReadByte(8);\nif (i < packetCount)\n    throw new InvalidDataException(\"ISF stream truncated before all packets were read\");","handlingStrategy":"validation","validationCode":"if (bitStream.EndOfStream)\n    return null; // or stop decoding\nbyte v = bitStream.ReadByte(countOfBits);","typeGuard":"static bool CanRead(BitStream s) => !s.EndOfStream;","tryCatchPattern":"try\n{\n    byte v = bitStream.ReadByte(8);\n}\ncatch (EndOfStreamException)\n{\n    // stream exhausted: treat ISF data as truncated\n    throw new InvalidDataException(\"ISF stream ended before all data was read\");\n}","preventionTips":["Check EndOfStream before every read in decode loops.","Verify total stream length matches expected packet counts before decoding.","Stop iterating when EndOfStream becomes true rather than trusting declared counts.","Treat EndOfStreamException during ISF decode as data corruption, not a retryable state."],"tags":["end-of-stream","truncated-data","ink-parsing"],"backgroundTag":"unexpected-end-of-stream","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}