{"record":{"id":"11893ac2eb745837","repo":"dotnet/wpf","slug":"sr-writecountnegative","errorCode":null,"errorMessage":"SR.WriteCountNegative","messagePattern":"SR\\.WriteCountNegative","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs","lineNumber":150,"sourceCode":"        /// <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);\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>","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs#L132-L168","documentation":"VerifyStreamWriteArgs rejects a negative count with ArgumentOutOfRangeException(SR.WriteCountNegative). Stream.Write requires count >= 0; the WPF packaging helpers enforce this explicitly before the checked bounds check.","triggerScenarios":"Passing a byte count computed as a difference (e.g. length - position) that went negative, or forwarding an unvalidated length from another API to a packaging stream Write.","commonSituations":"Size arithmetic on truncated files where remaining = total - read underflows; passing -1 as a sentinel for 'all bytes'.","solutions":["Compute count as Math.Max(0, total - read) or validate count >= 0 before writing.","Use (int)(stream.Length - stream.Position) with a non-negativity check when writing the remainder.","Replace sentinel negative values with an explicit branch for 'write everything'."],"exampleFix":"// before\nint count = (int)(totalSize - written);\nstream.Write(buffer, 0, count);\n// after\nint count = (int)Math.Max(0, totalSize - written);\nstream.Write(buffer, 0, count);","handlingStrategy":"validation","validationCode":"if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));","typeGuard":"static bool IsValidCount(int count) => count >= 0;","tryCatchPattern":"try { stream.Write(buffer, offset, count); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"count\") { /* clamp count to >= 0 */ }","preventionTips":["Compute remaining sizes with Math.Max(0, total - done).","Avoid -1 or negative sentinels for 'write everything'.","Validate lengths parsed from files before passing them to stream APIs."],"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"}