{"record":{"id":"a7865096c32032bd","repo":"dotnet/wpf","slug":"sr-readcountnegative","errorCode":null,"errorMessage":"SR.ReadCountNegative","messagePattern":"SR\\.ReadCountNegative","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs","lineNumber":116,"sourceCode":"        /// <param name=\"buffer\">buffer</param>\n        /// <param name=\"offset\">offset</param>\n        /// <param name=\"count\">count</param>\n        /// <remarks>Common argument verification for Stream.Read()</remarks>\n        internal static void VerifyStreamReadArgs(Stream s, byte[] buffer, int offset, int count)\n        {\n            if (!s.CanRead)\n                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>","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs#L98-L134","documentation":"This is a standard argument guard in PackagingUtilities.VerifyStreamReadArgs, a shared validation helper for Stream.Read implementations: it throws ArgumentOutOfRangeException when the count parameter is negative. It validates the buffer, offset and count supplied by a stream caller before the read is attempted.","triggerScenarios":"Passing a negative count parameter to a packaging Stream.Read call, e.g. from a length computation that produced a negative value.","commonSituations":"Subtracting a larger value from a length (remaining-bytes math gone wrong); uninitialized size variables; arithmetic on long-to-int casts that overflow negative.","solutions":["Validate count >= 0 before calling Read; clamp to Math.Max(0, remaining).","Fix the remaining-bytes calculation that produced the negative count.","Use checked arithmetic to catch overflow earlier and handle it explicitly."],"exampleFix":"// before\nint count = total - read; // can go negative\nstream.Read(buffer, offset, count);\n// after\nint count = Math.Max(0, total - read);\nif (count > 0) stream.Read(buffer, offset, count);","handlingStrategy":"validation","validationCode":"int count = Math.Max(0, Math.Min(total - read, buffer.Length - offset));\nif (count > 0) stream.Read(buffer, offset, count);","typeGuard":"bool ValidCount(int count) => count >= 0;","tryCatchPattern":"try { stream.Read(buffer, offset, count); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"count\") { /* clamp count */ }","preventionTips":["Clamp count with Math.Max(0, remaining).","Use checked arithmetic on length computations.","Skip Read entirely when count is 0 or negative."],"tags":["stream","argument","count","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"}