{"record":{"id":"f8854e028692f17f","repo":"dotnet/wpf","slug":"buffer-offset-cannot-be-negative","errorCode":null,"errorMessage":"Buffer offset cannot be negative.","messagePattern":"Buffer offset 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":271,"sourceCode":"            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\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            {","sourceCodeStart":253,"sourceCodeEnd":289,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs#L253-L289","documentation":"ByteStream.Read validates that the buffer offset is non-negative, throwing ArgumentOutOfRangeException(SR.BufferOffsetNegative) when offset < 0. This mirrors the standard Stream contract but is checked explicitly before the IStream call.","triggerScenarios":"Calling Read(buffer, offset, count) with a negative offset, typically from cursor arithmetic like offset += consumed where consumed was negative, or passing -1 as a sentinel.","commonSituations":"Manual buffer management in parsers with off-by-one or underflowing positions; sentinel values leaking into real calls.","solutions":["Validate offset >= 0 before calling Read.","Fix the position/cursor arithmetic producing the negative offset.","Replace sentinel -1 offsets with explicit flags."],"exampleFix":"// before\nstream.Read(buf, pos, len); // pos may underflow to negative\n// after\nif (pos < 0) throw new InvalidOperationException(\"Parser position underflow\");\nstream.Read(buf, pos, len);","handlingStrategy":"validation","validationCode":"if (offset < 0) throw new ArgumentOutOfRangeException(nameof(offset));","typeGuard":null,"tryCatchPattern":"try { stream.Read(buf, offset, count); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"offset\") { /* fix cursor arithmetic */ }","preventionTips":["Validate buffer cursors before each Read","Avoid -1 sentinels in offset variables","Assert invariants (offset >= 0) in parser loops"],"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"}