{"record":{"id":"6c8e22170a72c4cc","repo":"dotnet/wpf","slug":"a-read-or-write-operation-references-a-location-outside-the","errorCode":null,"errorMessage":"A read or write operation references a location outside the bounds of the buffer provided.","messagePattern":"A read or write operation references a location outside the bounds of the buffer provided\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/NetStream.cs","lineNumber":141,"sourceCode":"        {\n            CheckDisposed();\n#if DEBUG\n            if (System.IO.Packaging.PackWebRequestFactory._traceSwitch.Enabled)\n                System.Diagnostics.Trace.TraceInformation(\"NetStream.Read() offset:{0} length:{1}\", _position, count );\n#endif\n\n            PackagingUtilities.VerifyStreamReadArgs(this, buffer, offset, count);\n\n            // quick exit\n            if (count == 0)\n                return count;\n\n            int bytesRead = 0;\n\n            checked\n            {\n                if (offset + count > buffer.Length)\n                    throw new ArgumentException(SR.IOBufferOverflow, nameof(buffer));\n\n                // make sure some data is in the stream - block until it is\n                int bytesAvailable = GetData(new Block(_position, count));\n                count = Math.Min(bytesAvailable, count);    // don't return more than they requested, and don't return more than is available\n\n                // read into the buffer and return (if any data is available)\n                if (count > 0)\n                {\n                    try\n                    {\n                        _tempFileMutex.WaitOne();\n\n                        lock (PackagingUtilities.IsolatedStorageFileLock)\n                        {\n                            _tempFileStream.Seek(_position, SeekOrigin.Begin);      // align the temp stream with our logical position\n                            bytesRead = _tempFileStream.Read(buffer, offset, count);     // read from the temp file\n                        }\n                    }","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/NetStream.cs#L123-L159","documentation":"NetStream.Read validates that offset+count does not exceed the caller-supplied buffer length inside a checked block, throwing ArgumentException (SR.IOBufferOverflow) when the read would run past the end of the buffer. This library throws it to prevent writing more bytes into 'buffer' than it can hold.","triggerScenarios":"Calling Read(byte[] buffer, int offset, int count) where offset + count > buffer.Length, e.g. passing an offset near the end of a small buffer with a large count.","commonSituations":"Computing count from stream Length instead of remaining buffer space; reusing a large count value across multiple reads into progressively smaller buffers; off-by-one when reserving room for a terminator byte.","solutions":["Pass count <= buffer.Length - offset","Compute the read size as Math.Min(desired, buffer.Length - offset)","Check the buffer slice before calling Read; use ArraySegment or Span-based overloads where available"],"exampleFix":"// before\nstream.Read(buffer, buffer.Length - 2, 64);\n// after\nint toRead = Math.Min(64, buffer.Length - (buffer.Length - 2));\nstream.Read(buffer, buffer.Length - 2, toRead);","handlingStrategy":"validation","validationCode":"int toRead = Math.Min(desired, buffer.Length - offset);\nif (toRead < 0) throw new ArgumentException(\"offset beyond buffer end\");\nint read = stream.Read(buffer, offset, toRead);","typeGuard":"static bool IsValidReadRange(byte[] buffer, int offset, int count) => buffer != null && offset >= 0 && count >= 0 && offset + count <= buffer.Length;","tryCatchPattern":"try { bytesRead = stream.Read(buffer, offset, count); } catch (ArgumentException ex) when (ex.ParamName == \"buffer\") { log.Error(\"Read exceeds buffer bounds\", ex); }","preventionTips":["Always compute count as buffer.Length - offset when reading to capacity","Centralize read helpers instead of hand-computing offsets at call sites","Prefer Span<T>/Memory<T> overloads that make bounds explicit"],"tags":["packaging","stream","buffer-overflow","argument-validation"],"backgroundTag":"index-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"}