{"record":{"id":"9bd01b4d06b444c0","repo":"dotnet/wpf","slug":"seekorigin-value-is-not-valid-9bd01b","errorCode":null,"errorMessage":"SeekOrigin value is not valid.","messagePattern":"SeekOrigin value is not valid\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/RightsManagementEncryptedStream.cs","lineNumber":142,"sourceCode":"                {\n                    case SeekOrigin.Begin:\n                        {\n                            temp = offset;\n                            break;\n                        }\n                    case SeekOrigin.Current:\n                        {\n                            temp = _streamPosition + offset;\n                            break;\n                        }\n                    case SeekOrigin.End:\n                        {\n                            temp = Length + offset;\n                            break;\n                        }\n                    default:\n                        {\n                            throw new ArgumentOutOfRangeException(nameof(origin), SR.SeekOriginInvalid);\n                        }\n                }\n            }\n            \n            if (temp < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(offset), SR.SeekNegative);\n            }\n\n            _streamPosition = temp;\n            return _streamPosition;\n        }        \n\n        /// <summary>\n        /// See .NET Framework SDK under System.IO.Stream\n        /// </summary>\n        public override void SetLength(long newLength)\n        {","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/RightsManagementEncryptedStream.cs#L124-L160","documentation":"RightsManagementEncryptedStream.Seek supports only SeekOrigin.Begin, SeekOrigin.Current, and SeekOrigin.End. A value outside the defined SeekOrigin enum hits the switch's default case and throws ArgumentOutOfRangeException with the 'SeekOrigin value is not valid' message.","triggerScenarios":"Calling Seek with an origin value not in the SeekOrigin enum, e.g. stream.Seek(offset, (SeekOrigin)3) — commonly via a cast from an int/interop value.","commonSituations":"Interop code passing native SEEK_SET/SEEK_CUR/SEEK_END values (0/1/2 semantics may differ) or integer flags cast to SeekOrigin without mapping.","solutions":["Only pass SeekOrigin.Begin, SeekOrigin.Current, or SeekOrigin.End — never raw ints cast to SeekOrigin.","Map native seek constants to the correct SeekOrigin member before calling.","Catch ArgumentOutOfRangeException to detect a bad origin value and log it."],"exampleFix":"// before\nstream.Seek(offset, (SeekOrigin)seekConst); // interop constant\n// after\nvar origin = seekConst == 0 ? SeekOrigin.Begin : seekConst == 1 ? SeekOrigin.Current : SeekOrigin.End;\nstream.Seek(offset, origin);","handlingStrategy":"type-guard","validationCode":"bool IsValidOrigin(SeekOrigin o) => o is SeekOrigin.Begin or SeekOrigin.Current or SeekOrigin.End;","typeGuard":"static bool TryGetSeekOrigin(int raw, out SeekOrigin origin) {\n    switch (raw) {\n        case 0: origin = SeekOrigin.Begin; return true;\n        case 1: origin = SeekOrigin.Current; return true;\n        case 2: origin = SeekOrigin.End; return true;\n        default: origin = default; return false;\n    }\n}","tryCatchPattern":"try { stream.Seek(offset, origin); }\ncatch (ArgumentOutOfRangeException ex) { /* ex.ParamName == \"origin\"; map native constant and retry */ }","preventionTips":["Never cast raw ints to SeekOrigin; map native seek constants explicitly","Use Enum.IsDefined(typeof(SeekOrigin), value) before casting","Centralize seek-origin translation in one helper for interop code"],"tags":["argument-out-of-range","seek","stream","rights-management"],"backgroundTag":"invalid-enum-value","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"}