{"record":{"id":"dc8379ec1d4a1a15","repo":"stride3d/stride","slug":"can-t-read-beyond-end-of-stream","errorCode":null,"errorMessage":"Can't read beyond end of stream.","messagePattern":"Can't read beyond end of stream\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Serialization/Serialization/LZ4/LZ4Stream.cs","lineNumber":278,"sourceCode":"        innerStreamPosition += compressedLength;\n\n        bufferOffset = 0;\n    }\n\n    /// <summary>Reads the next chunk from stream.</summary>\n    /// <returns><c>true</c> if next has been read, or <c>false</c> if it is legitimate end of file.</returns>\n    /// <exception cref=\"IOException\">The end of the stream was unexpectedly reached.</exception>\n    private bool AcquireNextChunk()\n    {\n        do\n        {\n            if (!TryReadVarInt(out var varint)) return false;\n            var flags = (ChunkFlags)varint;\n            var isCompressed = (flags & ChunkFlags.Compressed) != 0;\n\n            var originalLength = (int)ReadVarInt();\n            var compressedLength = isCompressed ? (int)ReadVarInt() : originalLength;\n            if (compressedLength > originalLength) throw new IOException(\"Can't read beyond end of stream.\"); // corrupted\n\n            if (compressedDataBuffer == null || compressedDataBuffer.Length < compressedLength)\n                compressedDataBuffer = new byte[compressedLength];\n            var chunk = ReadBlock(compressedDataBuffer, 0, compressedLength);\n\n            if (chunk != compressedLength) throw new IOException(\"Can't read beyond end of stream.\"); // currupted\n\n            if (!isCompressed)\n            {\n                // swap the buffers\n                (compressedDataBuffer, dataBuffer) = (dataBuffer, compressedDataBuffer);\n                bufferLength = compressedLength;\n            }\n            else\n            {\n                if (dataBuffer == null || dataBuffer.Length < originalLength)\n                    dataBuffer = new byte[originalLength];\n                var passes = (int)flags >> 2;","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Serialization/Serialization/LZ4/LZ4Stream.cs#L260-L296","documentation":"AcquireNextChunk validates chunk metadata: compressedLength must not exceed originalLength, since LZ4 block compression never expands data. A chunk claiming a compressed size larger than its original size indicates a corrupted or malformed stream, so the library throws IOException.","triggerScenarios":"Reading an LZ4Stream whose chunk header was corrupted (bit flips, bad write) so compressedLength > originalLength; reading a file produced by an incompatible writer.","commonSituations":"Disk corruption or partial overwrite of compressed assets; hand-rolled producers writing chunk headers incorrectly; concatenating streams incorrectly so the reader lands on garbage bytes.","solutions":["Regenerate/re-download the compressed file; treat it as corrupted","Verify the chunk header layout matches the LZ4Stream format (flags, originalLength, compressedLength order)","Add a checksum/integrity check on stored compressed files","Ensure no seeking/offset misalignment when wrapping the inner stream"],"exampleFix":"// before\nusing var s = new LZ4Stream(File.OpenRead(path)); // throws on corrupt header\n// after\nif (!HasValidChecksum(path)) throw new InvalidDataException(\"corrupt lz4 file\");\nusing var s = new LZ4Stream(File.OpenRead(path));","handlingStrategy":"validation","validationCode":"// before opening: verify integrity of stored file\nif (!VerifyChecksum(path)) throw new InvalidDataException(\"LZ4 asset failed integrity check\");","typeGuard":null,"tryCatchPattern":"try { using var s = new LZ4Stream(File.OpenRead(path)); ... }\ncatch (IOException ex) when (ex.Message.Contains(\"beyond end of stream\")) { // corrupted stream: redownload/regenerate\n}","preventionTips":["Store checksums alongside compressed assets and verify before reading","Avoid manual edits or byte-level manipulation of LZ4 files","Handle storage/disk errors that can corrupt files"],"tags":["io","compression","lz4","corruption"],"backgroundTag":"checksum-mismatch","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}