{"record":{"id":"ef784698f2b44b7d","repo":"dotnet/wpf","slug":"buffer-size-is-too-small-to-accommodate-the-specified","errorCode":null,"errorMessage":"Buffer size is too small to accommodate the specified parameters.","messagePattern":"Buffer size is too small to accommodate the specified parameters\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs","lineNumber":279,"sourceCode":"            // count has to be positive number\n            if (0 > count)\n            {\n                throw new ArgumentOutOfRangeException(nameof(count),\n                                                      SR.ReadCountNegative);\n            }\n\n            // offset has to be a positive number\n            if (0 > offset)\n            {\n                throw new ArgumentOutOfRangeException(nameof(offset),\n                                                      SR.BufferOffsetNegative);\n            }\n\n            // make sure that we have a buffer that matches number of bytes we need to read \n            // since all values are > 0, there is no chance of overflow\n            if (!((buffer.Length > 0) && ((buffer.Length - offset) >= count)))\n            {\n                throw new ArgumentException(SR.BufferTooSmall, nameof(buffer));\n            }\n            \n            // offset == 0 is the normal case\n            if (0 == offset)\n            {\n                _securitySuppressedIStream.Read(buffer, count, out read);\n            }\n            // offset involved.  Must be positive\n            else if (0 < offset)\n            {\n                // Read into local array and then copy it into the given buffer at\n                //  the specified offset.\n                byte[] localBuffer = new byte[count];\n\n                _securitySuppressedIStream.Read(localBuffer, count, out read);\n\n                if (read > 0)\n                {","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs#L261-L297","documentation":"ByteStream.Read requires the supplied buffer to be non-empty and large enough to hold count bytes starting at offset: (buffer.Length > 0) && ((buffer.Length - offset) >= count). Otherwise it throws ArgumentException(SR.BufferTooSmall) naming the buffer parameter, before any IStream read occurs.","triggerScenarios":"Calling Read with buffer.Length < offset + count, e.g. buffer.Length == 0 with count > 0, or reading exactly the remaining chunk after the last chunk partially filled the buffer.","commonSituations":"Chunked-read loops that forget to shrink count for the final partial chunk; reusing small scratch buffers with oversized counts.","solutions":["Clamp count: count = Math.Min(count, buffer.Length - offset) before calling Read.","Ensure offset + count <= buffer.Length by construction in copy loops.","Allocate a buffer at least count bytes long when a full read is intended."],"exampleFix":"// before\nstream.Read(buf, offset, requestedCount); // ArgumentException if too small\n// after\nint count = Math.Min(requestedCount, buf.Length - offset);\nif (count > 0) stream.Read(buf, offset, count);","handlingStrategy":"validation","validationCode":"if (buf.Length == 0 || count > buf.Length - offset) count = Math.Max(0, buf.Length - offset);","typeGuard":null,"tryCatchPattern":"try { stream.Read(buf, offset, count); }\ncatch (ArgumentException ex) when (ex.ParamName == \"buffer\") { count = buf.Length - offset; stream.Read(buf, offset, count); }","preventionTips":["Always clamp count to buffer.Length - offset","Verify offset + count <= buffer.Length before reading","Prefer ArraySegment or Span-based helpers to keep bounds consistent"],"tags":["argument","buffer","validation","read"],"backgroundTag":"invalid-argument-value","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"}