{"record":{"id":"c764ba90990407be","repo":"JamesNK/Newtonsoft.Json","slug":"unable-to-read-beyond-the-end-of-the-stream","errorCode":null,"errorMessage":"Unable to read beyond the end of the stream.","messagePattern":"Unable to read beyond the end of the stream\\.","errorType":"exception","errorClass":"EndOfStreamException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Bson/BsonReader.cs","lineNumber":690,"sourceCode":"            EnsureBuffers();\n\n            StringBuilder builder = null;\n\n            int totalBytesRead = 0;\n\n            // used in case of left over multibyte characters in the buffer\n            int offset = 0;\n            do\n            {\n                int count = ((length - totalBytesRead) > MaxCharBytesSize - offset)\n                    ? MaxCharBytesSize - offset\n                    : length - totalBytesRead;\n\n                int byteCount = _reader.Read(_byteBuffer, offset, count);\n\n                if (byteCount == 0)\n                {\n                    throw new EndOfStreamException(\"Unable to read beyond the end of the stream.\");\n                }\n\n                totalBytesRead += byteCount;\n\n                // Above, byteCount is how many bytes we read this time.\n                // Below, byteCount is how many bytes are in the _byteBuffer.\n                byteCount += offset;\n\n                if (byteCount == length)\n                {\n                    // pref optimization to avoid reading into a string builder\n                    // first iteration and all bytes read then return string directly\n                    int charCount = Encoding.UTF8.GetChars(_byteBuffer, 0, byteCount, _charBuffer, 0);\n                    return new string(_charBuffer, 0, charCount);\n                }\n                else\n                {\n                    int lastFullCharStop = GetLastFullCharStop(byteCount - 1);","sourceCodeStart":672,"sourceCodeEnd":708,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Bson/BsonReader.cs#L672-L708","documentation":"Thrown by BsonReader.ReadString when the underlying stream returns zero bytes before the declared string length has been fully read. It is an EndOfStreamException meaning the BSON document declared more data than the stream actually contains. This almost always indicates a truncated or corrupt BSON payload.","triggerScenarios":"Reading a BSON document whose length-prefixed string (or C# string read path) declares N bytes, but the stream ends before N bytes are available. Also triggered when the document's top-level length prefix is wrong, causing the reader to over-read into nothing.","commonSituations":"Truncated network read that delivered only part of the BSON payload. A length prefix that was written with a wrong endianness or wrong value. Feeding a non-BSON stream that happens to start with plausible length bytes. Reading from a MemoryStream whose Capacity was misreported.","solutions":["Verify the byte length of the stream matches the BSON document's internal length prefix before deserialization.","Re-fetch or re-transmit the payload if it was truncated over the network; ensure the transport delivers the complete buffer.","Wrap the read in a try/catch for EndOfStreamException and treat it as corrupt-data for the caller.","Validate the first 4 bytes (document length) against the actual stream length as an integrity pre-check."],"exampleFix":"// before: trusting a network stream directly\nusing var reader = new BsonReader(networkStream);\nvar obj = serializer.Deserialize(reader, typeof(Foo));\n\n// after: buffer fully and validate length first\nbyte[] data = ReadFully(networkStream);\nint docLen = BitConverter.ToInt32(data, 0);\nif (data.Length < docLen) throw new InvalidDataException(\"truncated BSON\");\nusing var reader = new BsonReader(new MemoryStream(data));","handlingStrategy":"validation","validationCode":"byte[] data = ReadFully(stream);\nint docLen = BitConverter.ToInt32(data, 0);\nif (docLen <= 0 || data.Length < docLen)\n    throw new InvalidDataException($\"BSON declares {docLen} bytes but stream has {data.Length}\");","typeGuard":"static bool StreamContainsFullDocument(byte[] data)\n{\n    if (data.Length < 4) return false;\n    int len = BitConverter.ToInt32(data, 0);\n    return len > 0 && data.Length >= len;\n}","tryCatchPattern":"try { var v = serializer.Deserialize(bsonReader, type); }\ncatch (System.IO.EndOfStreamException ex)\n{\n    throw new InvalidDataException(\"BSON payload is truncated or corrupt\", ex);\n}","preventionTips":["Buffer the full payload before deserializing so truncation is detected up front.","Validate the document length prefix against the actual byte count.","Treat EndOfStreamException during BSON read as corrupt-data, not a transient error."],"tags":["bson","deserialization","end-of-stream","corrupt-data"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}