{"record":{"id":"bf4f2144a673ca08","repo":"dotnet/wpf","slug":"sr-writebuffertoosmall","errorCode":null,"errorMessage":"SR.WriteBufferTooSmall","messagePattern":"SR\\.WriteBufferTooSmall","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs","lineNumber":156,"sourceCode":"            if (!s.CanWrite)\n                throw new NotSupportedException(SR.WriteNotSupported);\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.WriteCountNegative);\n            }\n\n            checked\n            {\n                if (offset + count > buffer.Length)\n                    throw new ArgumentException(SR.WriteBufferTooSmall, nameof(buffer));\n            }\n        }\n\n        /// <summary>\n        /// Read utility that is guaranteed to return the number of bytes requested\n        /// if they are available.\n        /// </summary>\n        /// <param name=\"stream\">stream to read from</param>\n        /// <param name=\"buffer\">buffer to read into</param>\n        /// <param name=\"offset\">offset in buffer to write to</param>\n        /// <param name=\"count\">bytes to read</param>\n        /// <returns>bytes read</returns>\n        /// <remarks>Normal Stream.Read does not guarantee how many bytes it will\n        /// return.  This one does.</remarks>\n        internal static int ReliableRead(Stream stream, byte[] buffer, int offset, int count)\n        {\n            return ReliableRead(stream, buffer, offset, count, count);\n        }","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs#L138-L174","documentation":"VerifyStreamWriteArgs throws ArgumentException(SR.WriteBufferTooSmall) when offset + count exceeds buffer.Length, i.e. the write would read past the end of the source buffer. Evaluated inside checked to also trap integer overflow of offset + count.","triggerScenarios":"Calling Write on a packaging stream with buffer.Length=8, offset=4, count=8 (needs 12 bytes); or offset/count values so large their sum overflows int.","commonSituations":"Mixing up count as an end-index instead of a length; using a destination-buffer length as count while the source buffer is smaller.","solutions":["Ensure buffer.Length >= offset + count; shrink count to buffer.Length - offset.","Pass the actual number of valid bytes in the buffer as count, not the buffer's capacity.","Add a pre-write assertion/bounds check in the copy helper that computes these values."],"exampleFix":"// before\nstream.Write(buffer, offset, buffer.Length);\n// after\nint count = buffer.Length - offset;\nif (count <= 0) return;\nstream.Write(buffer, offset, count);","handlingStrategy":"validation","validationCode":"if ((long)offset + count > buffer.Length)\n    throw new ArgumentException(\"offset+count exceeds buffer length\", nameof(buffer));","typeGuard":"static bool FitsInBuffer(byte[] buffer, int offset, int count) =>\n    buffer != null && offset >= 0 && count >= 0 && (long)offset + count <= buffer.Length;","tryCatchPattern":"try { stream.Write(buffer, offset, count); }\ncatch (ArgumentException ex) when (ex.ParamName == \"buffer\") { /* clamp count = buffer.Length - offset */ }","preventionTips":["Cast to long when checking offset + count to avoid int overflow.","Pass the number of valid bytes, not the buffer capacity, as count.","Centralize buffer bounds checking in one copy helper."],"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-22T01:17:13.364Z"}