{"record":{"id":"d483af68a82a8941","repo":"dotnet/wpf","slug":"sr-seeknegative","errorCode":null,"errorMessage":"SR.SeekNegative","messagePattern":"SR\\.SeekNegative","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CFStream.cs","lineNumber":159,"sourceCode":"    {\n        CheckDisposedStatus();\n\n        if (!CanSeek)\n        {\n            throw new NotSupportedException(SR.SeekNotSupported);\n        }\n        \n        // \n        long seekPos = 0;\n        int translatedSeekOrigin = 0;\n\n        switch(origin)\n        {\n            case SeekOrigin.Begin:\n                translatedSeekOrigin = SafeNativeCompoundFileConstants.STREAM_SEEK_SET;\n                if( 0 > offset )\n                {\n                    throw new ArgumentOutOfRangeException(nameof(offset),\n                        SR.SeekNegative);\n                }\n                break;\n            case SeekOrigin.Current:\n                translatedSeekOrigin = SafeNativeCompoundFileConstants.STREAM_SEEK_CUR;\n                // Need to find the current seek pointer to see if we'll end\n                //  up with a negative position.\n                break;\n            case SeekOrigin.End:\n                translatedSeekOrigin = SafeNativeCompoundFileConstants.STREAM_SEEK_END;\n                // Need to find the current seek pointer to see if we'll end\n                //  up with a negative position.\n                break;\n            default:\n                throw new System.ComponentModel.InvalidEnumArgumentException(\"origin\", (int) origin, typeof(SeekOrigin));\n        }\n\n        _safeIStream.Seek( offset, translatedSeekOrigin, out seekPos );","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CFStream.cs#L141-L177","documentation":"CFStream.Seek throws ArgumentOutOfRangeException with SR.SeekNegative when seeking from SeekOrigin.Begin with a negative offset. Positions relative to the beginning of a stream cannot be negative, so the library rejects the call up front instead of passing it to the underlying compound-file IStream. It is a caller contract violation, not a stream-state problem.","triggerScenarios":"Calling Seek(offset, SeekOrigin.Begin) on a CompoundFile CFStream with offset < 0, e.g. stream.Seek(-10, SeekOrigin.Begin). Seeks from Current or End with negative offsets are allowed and validated later.","commonSituations":"Arithmetic computing a target position from Current goes negative and the code is rewritten to use Begin; absolute position variables that were never clamped to >= 0; porting code that used MemoryStream (which throws similar errors) but computing offsets differently for compound files.","solutions":["Ensure the offset passed to Seek with SeekOrigin.Begin is >= 0 before calling","If seeking backwards relative to the current position, use SeekOrigin.Current with a negative offset instead of SeekOrigin.Begin","Clamp computed absolute positions: long pos = Math.Max(0, computedPos);","Catch ArgumentOutOfRangeException at the call site to detect bad position arithmetic"],"exampleFix":"// before\nstream.Seek(-headerSize, SeekOrigin.Begin);\n// after\nlong target = stream.Length - headerSize;\nstream.Seek(Math.Max(0, target), SeekOrigin.Begin);","handlingStrategy":"validation","validationCode":"if (offset < 0 && origin == SeekOrigin.Begin) throw new ArgumentException(\"Begin-seek offset must be >= 0\", nameof(offset));","typeGuard":"static bool IsValidBeginSeek(long offset) => offset >= 0;","tryCatchPattern":"try { stream.Seek(offset, SeekOrigin.Begin); } catch (ArgumentOutOfRangeException ex) { /* fix offset math */ }","preventionTips":["Compute absolute targets from stream.Length and clamp to >= 0","Prefer SeekOrigin.Current for relative backward moves","Unit-test seek arithmetic with edge offsets"],"tags":["argument-out-of-range","io","stream","wpf"],"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"}