{"record":{"id":"85bd29f793f6fa2c","repo":"dotnet/wpf","slug":"cannot-set-seek-pointer-to-a-negative-position","errorCode":null,"errorMessage":"Cannot set seek pointer to a negative position.","messagePattern":"Cannot set seek pointer to a negative position\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/NetStream.cs","lineNumber":256,"sourceCode":"                            temp = _position + offset;\n                            break;\n                        }\n\n                    case SeekOrigin.End:\n                        {\n                            temp = Length + offset;\n                            break;\n                        }\n\n                    default:\n                        {\n                            throw new ArgumentOutOfRangeException(nameof(origin), SR.SeekOriginInvalid);\n                        }\n                }\n            }\n            if (temp < 0)\n            {\n                throw new ArgumentException(SR.SeekNegative);\n            }\n\n#if DEBUG\n            if (System.IO.Packaging.PackWebRequestFactory._traceSwitch.Enabled)\n                System.Diagnostics.Trace.TraceInformation(\"NetStream.Seek() pos:{0}\", temp);\n#endif\n            _position = temp;\n            return _position;\n        }\n\n\n        /// <summary>\n        /// Logical byte position in this stream\n        /// </summary>\n        public override long Position\n        {\n            get\n            {","sourceCodeStart":238,"sourceCodeEnd":274,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/NetStream.cs#L238-L274","documentation":"After resolving the requested position against the given SeekOrigin, NetStream.Seek checks the computed target and throws ArgumentException (SR.SeekNegative) when it is below zero. This library throws it because stream positions are non-negative.","triggerScenarios":"Calling Seek with an offset that computes to a negative position, e.g. Seek(-100, SeekOrigin.Begin) or Seek(-1000, SeekOrigin.Current) when the current position is less than 1000.","commonSituations":"Rewinding farther than the amount read so far; subtracting record sizes when parsing without tracking position; copying offset arithmetic from another stream implementation with different clamping semantics.","solutions":["Clamp the target with Math.Max(0, currentPos + offset) before seeking","Track the current position (stream.Position) and only seek back as far as 0","For relative movement, guard against negative results: if (pos + offset < 0) offset = -pos;"],"exampleFix":"// before\nstream.Seek(currentOffset - recordSize, SeekOrigin.Begin);\n// after\nlong target = Math.Max(0, currentOffset - recordSize);\nstream.Seek(target, SeekOrigin.Begin);","handlingStrategy":"validation","validationCode":"long target = origin switch {\n    SeekOrigin.Begin => offset,\n    SeekOrigin.Current => stream.Position + offset,\n    SeekOrigin.End => stream.Length + offset,\n    _ => throw new ArgumentException(\"invalid origin\") };\nif (target < 0) target = 0;\nstream.Seek(target, SeekOrigin.Begin);","typeGuard":"static long ClampPosition(long p) => Math.Max(0, p);","tryCatchPattern":"try { stream.Seek(offset, origin); } catch (ArgumentException) { stream.Seek(0, SeekOrigin.Begin); }","preventionTips":["Clamp computed seek targets to >= 0","Track current position before relative rewinds","Write a SafeSeek helper and use it everywhere instead of raw Seek"],"tags":["packaging","stream","seek","negative-position"],"backgroundTag":"value-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}