{"record":{"id":"26d29df634386062","repo":"dotnet/wpf","slug":"sr-offsetnegative","errorCode":null,"errorMessage":"SR.OffsetNegative","messagePattern":"SR\\.OffsetNegative","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs","lineNumber":111,"sourceCode":"\n        /// <summary>\n        /// VerifyStreamReadArgs\n        /// </summary>\n        /// <param name=\"s\">stream</param>\n        /// <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","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs#L93-L129","documentation":"VerifyStreamReadArgs throws ArgumentOutOfRangeException(nameof(offset), SR.OffsetNegative) when a Stream.Read call is made with a negative offset into the destination buffer. The offset must be a valid non-negative index within the buffer.","triggerScenarios":"Passing a negative offset parameter to a packaging Stream.Read call, often from an uninitialized int or a miscalculated position.","commonSituations":"Arithmetic underflow when computing the offset; copying a negative saved offset variable; defaulting offset to an error sentinel like -1 and passing it through.","solutions":["Ensure offset is >= 0 before calling Read; clamp or validate the computed offset.","Fix the offset calculation (e.g. guard against underflow when subtracting).","Replace sentinel -1 values with an explicit error path instead of calling Read."],"exampleFix":"// before\nstream.Read(buffer, offset, count); // offset could be -1\n// after\nif (offset < 0) throw new ArgumentException(nameof(offset));\nstream.Read(buffer, offset, count);","handlingStrategy":"validation","validationCode":"if (offset < 0) throw new ArgumentOutOfRangeException(nameof(offset));","typeGuard":"bool ValidReadArgs(int offset, int count) => offset >= 0 && count >= 0 && offset + count <= buffer.Length;","tryCatchPattern":"try { stream.Read(buffer, offset, count); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"offset\") { /* fix offset computation */ }","preventionTips":["Validate offset >= 0 before any Stream.Read.","Guard offset arithmetic against underflow.","Avoid using -1 as an offset sentinel."],"tags":["stream","argument","offset","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"}