{"record":{"id":"9915db3fe53250b3","repo":"dotnet/wpf","slug":"count-of-bytes-to-read-cannot-be-negative","errorCode":null,"errorMessage":"Count of bytes to read cannot be negative.","messagePattern":"Count of bytes to read cannot be negative\\.","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs","lineNumber":264,"sourceCode":"            CheckDisposedStatus();\n\n            if (!CanRead)\n            {\n                throw new NotSupportedException(SR.ReadNotSupported);\n            }\n            \n            int read = 0;\n\n            // optimization: if we are being asked to read zero bytes, be done.\n            if (count == 0)\n            {\n                return read;\n            }\n            \n            // 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","sourceCodeStart":246,"sourceCodeEnd":282,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs#L246-L282","documentation":"ByteStream.Read validates that count (bytes to read) is non-negative and throws ArgumentOutOfRangeException(SR.ReadCountNegative) when count < 0. Zero is explicitly allowed and returns immediately (optimization path).","triggerScenarios":"Calling Read(buffer, offset, count) with a negative count, usually from buffer-size arithmetic like (int)(end - start) where end < start, or an int overflow/underflow.","commonSituations":"Chunked copy loops with miscomputed remaining bytes; interop code passing sizes as unsigned that wrap to negative ints.","solutions":["Validate count >= 0 before calling Read, clamping negative remainders to 0.","Fix the arithmetic computing count (check start/end ordering).","Use larger types (long) for intermediate size math to avoid overflow before the cast to int."],"exampleFix":"// before\nstream.Read(buf, 0, (int)(end - start)); // throws when end < start\n// after\nint count = (int)Math.Max(0, end - start);\nstream.Read(buf, 0, count);","handlingStrategy":"validation","validationCode":"if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));","typeGuard":null,"tryCatchPattern":"try { stream.Read(buf, 0, count); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"count\") { /* recompute count */ }","preventionTips":["Clamp computed counts with Math.Max(0, ...)","Use long arithmetic for sizes and validate before casting to int","Test chunk loops with final partial chunks"],"tags":["argument-out-of-range","read","validation"],"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"}