{"record":{"id":"751ad44539c44960","repo":"dotnet/wpf","slug":"argumentoutofrangeexception-nameof-offset-xamlstream","errorCode":null,"errorMessage":"ArgumentOutOfRangeException(nameof(offset))","messagePattern":"ArgumentOutOfRangeException\\(nameof\\(offset\\)\\)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlStream.cs","lineNumber":162,"sourceCode":"            switch(loc)\n            {\n                case SeekOrigin.Begin:\n                    WritePosition = (int) offset;\n                    break;\n                case SeekOrigin.Current:\n                    WritePosition = (int) (WritePosition + offset);\n                    break;\n                case SeekOrigin.End:\n                    throw new NotSupportedException(SR.ParserWriterNoSeekEnd);\n                default:\n                    throw new ArgumentException(SR.ParserWriterUnknownOrigin);\n            }\n\n            if( (!( WritePosition <= WriteLength ))\n                ||\n                (!( WritePosition >= ReadLength  )) )\n            {\n                throw new ArgumentOutOfRangeException( nameof(offset));\n            }\n\n            return WritePosition;\n        }\n\n\n        /// <summary>\n        /// Called by the Writer to indicate its okay for the reader to see\n        /// the file up to and including the position. Once the Writer calls\n        /// this it cannot go back and change the content.\n        /// </summary>\n        /// <param name=\"position\">Absolute position in the stream</param>\n        internal void UpdateReaderLength(long position)\n        {\n            ArgumentOutOfRangeException.ThrowIfLessThan(position, ReadLength);\n#if DEBUG\n            Debug.Assert(!WriteComplete,\"UpdateReaderLength called after close\");\n#endif","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlStream.cs#L144-L180","documentation":"After WriterSeek computes the new write position, it validates that the position lies within the valid window (<= WriteLength and >= ReadLength). If not, it throws ArgumentOutOfRangeException named 'offset', meaning the requested seek offset would move the write position outside the readable/writable span of the internal buffer.","triggerScenarios":"Calling Seek(offset, SeekOrigin.Begin) with a negative offset or one beyond WriteLength, or Seek(offset, SeekOrigin.Current) where the result lands before ReadLength or past WriteLength.","commonSituations":"Bookkeeping errors in stream positions when interleaving reads and writes on the internal XAML parser stream; assumptions about stream length that don't hold for this pipe-like stream.","solutions":["Clamp or compute the offset so the resulting position stays within [ReadLength, WriteLength].","Seek from SeekOrigin.Begin with a known-valid absolute position instead of relative math.","Inspect WriteLength/ReadLength before seeking to confirm the target position is in range."],"exampleFix":"// before\nstream.Seek(-100, SeekOrigin.Current); // lands before ReadLength\n// after\nlong target = Math.Max(stream.Length, 0);\nstream.Seek(target, SeekOrigin.Begin);","handlingStrategy":"validation","validationCode":"long target = origin == SeekOrigin.Begin ? offset : currentPos + offset;\nif (target < 0 || target > maxValidPosition) throw new ArgumentOutOfRangeException(nameof(offset));","typeGuard":null,"tryCatchPattern":"try { stream.Seek(offset, origin); }\ncatch (ArgumentOutOfRangeException) { /* clamp offset and retry */ }","preventionTips":["Compute and clamp seek targets against stream bounds before seeking.","Prefer absolute Begin-relative seeks with validated positions.","Remember XamlStream positions are constrained to the buffered read/write window, not arbitrary."],"tags":["wpf","xaml","io","stream","argument-out-of-range"],"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"}