{"record":{"id":"bdebfc270dba94c6","repo":"dotnet/wpf","slug":"cannot-set-seek-pointer-to-negative-position","errorCode":null,"errorMessage":"Cannot set seek pointer to negative position.","messagePattern":"Cannot set seek pointer to negative position\\.","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs","lineNumber":202,"sourceCode":"        public override long Seek(long offset, SeekOrigin origin)\n        {\n            CheckDisposedStatus();\n\n            if (!CanSeek)\n            {\n                throw new NotSupportedException(SR.SeekNotSupported);\n            }\n\n            long seekPos = 0;\n            int translatedSeekOrigin = 0;\n\n            switch (origin)\n            {\n                case SeekOrigin.Begin:\n                    translatedSeekOrigin = NativeMethods.STREAM_SEEK_SET;\n                    if (0 > offset)\n                    {\n                        throw new ArgumentOutOfRangeException(nameof(offset),\n                                                              SR.SeekNegative);\n                    }\n                    break;\n\n                case SeekOrigin.Current:\n                    translatedSeekOrigin = NativeMethods.STREAM_SEEK_CUR;\n                    break;\n\n                case SeekOrigin.End:\n                    translatedSeekOrigin = NativeMethods.STREAM_SEEK_END;\n                    break;\n                default:\n                    throw new System.ComponentModel.InvalidEnumArgumentException(\"origin\",\n                                                                                 (int)origin,\n                                                                                 typeof(SeekOrigin));\n            }\n\n            _securitySuppressedIStream.Seek(offset, translatedSeekOrigin, out seekPos);","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs#L184-L220","documentation":"When seeking with SeekOrigin.Begin, ByteStream rejects negative offsets with ArgumentOutOfRangeException(SR.SeekNegative). A negative absolute position is meaningless, so the call is refused before touching the underlying IStream.","triggerScenarios":"Calling Seek(offset, SeekOrigin.Begin) with a negative offset, usually from arithmetic like Seek(relativeOffset, SeekOrigin.Begin) where relativeOffset was computed from subtraction or an uninitialized variable.","commonSituations":"Parsing container/part offsets where a header value is misread (endian mismatch), or code confusing SeekOrigin.Current semantics (where negatives are legal) with SeekOrigin.Begin semantics.","solutions":["Clamp or validate the offset before seeking: if (offset < 0) offset = 0;","Use SeekOrigin.Current with a negative offset if a backward relative seek was intended.","Fix the offset arithmetic that produced the negative value (check units and header parsing)."],"exampleFix":"// before\nstream.Seek(delta, SeekOrigin.Begin); // ArgumentOutOfRangeException when delta<0\n// after\nstream.Seek(delta, delta >= 0 ? SeekOrigin.Begin : SeekOrigin.Current);","handlingStrategy":"validation","validationCode":"if (offset < 0) throw new ArgumentOutOfRangeException(nameof(offset));","typeGuard":null,"tryCatchPattern":"try { stream.Seek(offset, SeekOrigin.Begin); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"offset\") { /* fix/clamp offset */ }","preventionTips":["Clamp offsets to >= 0 before absolute seeks","Use SeekOrigin.Current for backward relative seeks","Unit-test offset arithmetic with boundary values"],"tags":["argument-out-of-range","seek","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"}