{"record":{"id":"68bbd798d0db1c84","repo":"dotnet/wpf","slug":"sr-readbuffertoosmall","errorCode":null,"errorMessage":"SR.ReadBufferTooSmall","messagePattern":"SR\\.ReadBufferTooSmall","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs","lineNumber":123,"sourceCode":"                throw new NotSupportedException(SR.ReadNotSupported);\n\n            ArgumentNullException.ThrowIfNull(buffer);\n\n            if (offset < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(offset), SR.OffsetNegative);\n            }\n\n            if (count < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(count), SR.ReadCountNegative);\n            }\n\n            checked     // catch any integer overflows\n            {\n                if (offset + count > buffer.Length)\n                {\n                    throw new ArgumentException(SR.ReadBufferTooSmall, nameof(buffer));\n                }\n            }\n        }\n\n        /// <summary>\n        /// VerifyStreamWriteArgs\n        /// </summary>\n        /// <param name=\"s\"></param>\n        /// <param name=\"buffer\"></param>\n        /// <param name=\"offset\"></param>\n        /// <param name=\"count\"></param>\n        /// <remarks>common argument verification for Stream.Write</remarks>\n        internal static void VerifyStreamWriteArgs(Stream s, byte[] buffer, int offset, int count)\n        {\n            if (!s.CanWrite)\n                throw new NotSupportedException(SR.WriteNotSupported);\n\n            ArgumentNullException.ThrowIfNull(buffer);","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs#L105-L141","documentation":"VerifyStreamReadArgs in PackagingUtilities validates arguments for Stream.Read calls in WPF packaging code. When offset + count exceeds buffer.Length, the read would overflow the buffer, so an ArgumentException is thrown. The checked block also guards against integer overflow when computing offset + count.","triggerScenarios":"Calling a packaging Stream.Read (e.g. on a ZipPackage part stream) with a buffer that is smaller than offset + count, e.g. buffer.Length=10 with offset=5, count=10.","commonSituations":"Hand-rolled stream copy loops where the offset was set from a previous partial read but the buffer was not grown; copying code that assumes offset is relative to a sub-range of the buffer.","solutions":["Ensure buffer.Length >= offset + count before calling Read, or allocate a larger buffer.","If reading from a current position, pass offset=0 and use the full buffer length as count.","Round-trip: compute the safe count as buffer.Length - offset and clamp the requested count to it."],"exampleFix":"// before\nstream.Read(buffer, offset, count);\n// after\ncount = Math.Min(count, buffer.Length - offset);\nif (count < 0) throw new ArgumentOutOfRangeException(nameof(count));\nstream.Read(buffer, offset, count);","handlingStrategy":"validation","validationCode":"if (buffer == null) throw new ArgumentNullException(nameof(buffer));\nif (offset < 0 || count < 0 || offset + count > buffer.Length)\n    throw new ArgumentOutOfRangeException(nameof(count), \"offset+count exceeds buffer length\");","typeGuard":"static bool CanReadInto(byte[] buffer, int offset, int count) =>\n    buffer != null && offset >= 0 && count >= 0 && (long)offset + count <= buffer.Length;","tryCatchPattern":"try { stream.Read(buffer, offset, count); }\ncatch (ArgumentException ex) when (ex.ParamName == \"buffer\") { /* fix bounds or reallocate buffer */ }","preventionTips":["Always compute count as buffer.Length - offset before reading.","Use ArraySegment<byte> or Span<byte> so bounds are enforced by the type.","Never reuse an offset from a previous read without re-validating against the buffer."],"tags":["stream","argument-validation","wpf","packaging"],"backgroundTag":"argument-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"}