{"record":{"id":"8897c03f97e7a5b9","repo":"stride3d/stride","slug":"can-t-write-beyond-end-of-stream","errorCode":null,"errorMessage":"Can't write beyond end of stream.","messagePattern":"Can't write beyond end of stream\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.IO/VirtualFileStream.cs","lineNumber":216,"sourceCode":"    /// <inheritdoc/>\n    public override void SetLength(long value)\n    {\n        if (endPosition != -1)\n        {\n            throw new NotSupportedException(\"Can't resize VirtualFileStream if endPosition is not -1.\");\n        }\n\n        InternalStream.SetLength(value);\n    }\n\n    /// <inheritdoc/>\n    /// <exception cref=\"IOException\">The remaining capacity in the stream\n    /// is not large enough to write the specified <paramref name=\"buffer\"/>.</exception>\n    public override void Write(byte[] buffer, int offset, int count)\n    {\n        if (endPosition != -1 && count > endPosition - InternalStream.Position)\n        {\n            throw new IOException(\"Can't write beyond end of stream.\");\n        }\n\n        InternalStream.Write(buffer, offset, count);\n    }\n\n    /// <inheritdoc/>\n    /// <exception cref=\"IOException\">The remaining capacity in the stream\n    /// is not large enough to write the specified <paramref name=\"buffer\"/>.</exception>\n    public override void Write(ReadOnlySpan<byte> buffer)\n    {\n        if (endPosition != -1 && buffer.Length > endPosition - InternalStream.Position)\n        {\n            throw new IOException(\"Can't write beyond end of stream.\");\n        }\n\n        InternalStream.Write(buffer);\n    }\n","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.IO/VirtualFileStream.cs#L198-L234","documentation":"VirtualFileStream.Write (byte[] overload) enforces the slice's endPosition: if the requested count would push the internal stream's position past the end of the bounded region, an IOException is thrown instead of silently overwriting data that belongs after the slice.","triggerScenarios":"Writing a buffer whose count exceeds endPosition - current position on a bounded VirtualFileStream (endPosition != -1).","commonSituations":"Buffered writers flushing buffers larger than the remaining slice capacity; copy loops with a fixed chunk size that overruns the slice on the last iteration.","solutions":["Cap the write count to the remaining capacity: Math.Min(count, (int)(endPosition - InternalStream.Position))","Extend the slice's endPosition when constructing the stream if more capacity is needed","Write to an unbounded stream or the parent stream for larger payloads","Split writes into chunks that fit the remaining capacity"],"exampleFix":"// before\nvirtualStream.Write(buffer, 0, buffer.Length); // IOException if it overruns\n// after\nvar remaining = end - virtualStream.Position;\nvar toWrite = (int)Math.Min(buffer.Length, remaining);\nvirtualStream.Write(buffer, 0, toWrite);","handlingStrategy":"validation","validationCode":"long remaining = end - stream.Position;\nint toWrite = (int)Math.Min(count, Math.Max(0, remaining));\nif (toWrite > 0) stream.Write(buffer, offset, toWrite);","typeGuard":null,"tryCatchPattern":"try { stream.Write(buffer, offset, count); } catch (IOException ex) when (ex.Message == \"Can't write beyond end of stream.\") { /* truncate or grow slice */ }","preventionTips":["Track remaining capacity in writer loops","Chunk large writes by remaining capacity","Size slices large enough for the payload up front"],"tags":["stream","write","bounds"],"backgroundTag":"file-size-limit-exceeded","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"}