{"record":{"id":"4e3324e22d540739","repo":"dotnet/wpf","slug":"argumentoutofrangeexception-offset-is-outside-the-valid","errorCode":null,"errorMessage":"ArgumentOutOfRangeException: offset is outside the valid stream range (nameof(offset))","messagePattern":"ArgumentOutOfRangeException: offset is outside the valid stream range \\(nameof\\(offset\\)\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/SharedStream.cs","lineNumber":239,"sourceCode":"                case SeekOrigin.Begin:\n                    newPosition = offset;\n                    break;\n\n                case SeekOrigin.Current:\n                    newPosition = _position + offset;\n                    break;\n\n                case SeekOrigin.End:\n                    newPosition = _length + offset;\n                    break;\n\n                default:\n                    throw new InvalidEnumArgumentException(nameof(origin), (int)origin, typeof(SeekOrigin));\n            }\n\n            if (newPosition < 0 || newPosition >= _length)\n            {\n                throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty);\n            }\n\n            CheckDisposed();\n\n            _position = newPosition;\n\n            return _position;\n        }\n\n        public override void SetLength(long value)\n        {\n            throw new NotSupportedException();\n        }\n\n        protected override void Dispose(bool disposing)\n        {\n            if (disposing)\n            {","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/SharedStream.cs#L221-L257","documentation":"SharedStream.Seek computes the new position from the given offset and origin; if the result is negative or >= the stream window's _length, the offset is outside the valid stream range and ArgumentOutOfRangeException is thrown. SharedStream only exposes its fixed window, not the whole base stream.","triggerScenarios":"Calling Seek with (offset, SeekOrigin.Begin) where offset < 0 or offset >= window length, or with SeekOrigin.End where offset pushes past 0.._length bounds (e.g. Seek(-_length-1, End), or offsets computed from the full base stream's length instead of the shared window's length).","commonSituations":"Using the underlying stream's total Length instead of SharedStream's windowed Length to compute positions; skipping a magic header with a too-large jump; reading a truncated BAML whose recorded offsets exceed the actual shared window.","solutions":["Clamp/validate offsets against the SharedStream's own Length property (the window size), not the base stream's.","Compute End-relative seeks with offsets in [-Length, 0].","Check that the resource wasn't truncated — compare expected BAML size with the window length.","Guard with: if (offset < 0 || offset >= stream.Length) skip/throw a domain error before calling Seek."],"exampleFix":"// before\nstream.Seek(baseStream.Length - footerSize, SeekOrigin.Begin); // may exceed shared window\n// after\nlong pos = stream.Length - footerSize;\nif (pos >= 0) stream.Seek(pos, SeekOrigin.Begin);","handlingStrategy":"validation","validationCode":"// Validate offset against the SharedStream window before Seek\nvoid SafeSeek(Stream s, long offset, SeekOrigin origin)\n{\n    long target = origin switch\n    {\n        SeekOrigin.Begin => offset,\n        SeekOrigin.Current => s.Position + offset,\n        SeekOrigin.End => s.Length + offset,\n        _ => long.MinValue\n    };\n    if (target < 0 || target >= s.Length) throw new IOException($\"offset {offset} outside stream window 0..{s.Length}\");\n    s.Seek(offset, origin);\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    stream.Seek(offset, SeekOrigin.Begin);\n}\ncatch (ArgumentOutOfRangeException)\n{\n    // clamp into the shared window instead of crashing\n    stream.Seek(0, SeekOrigin.Begin);\n}","preventionTips":["Use the SharedStream's own Length (window size), not the base stream's","Compute End-relative offsets as negative values in [-Length, 0]","Validate recorded offsets in headers against actual window length","Check for truncated resources before positional parsing"],"tags":["io","stream","argument-out-of-range","offset"],"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"}