{"record":{"id":"da759384a57084a8","repo":"dotnet/wpf","slug":"invalidenumargumentexception-invalid-seekorigin-value-nameof","errorCode":null,"errorMessage":"InvalidEnumArgumentException: invalid SeekOrigin value (nameof(origin))","messagePattern":"InvalidEnumArgumentException: invalid SeekOrigin value \\(nameof\\(origin\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/SharedStream.cs","lineNumber":234,"sourceCode":"        public override long Seek(long offset, SeekOrigin origin)\n        {\n            long newPosition;\n            switch (origin)\n            {\n                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        }","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/SharedStream.cs#L216-L252","documentation":"SharedStream.Seek validates the SeekOrigin parameter and throws InvalidEnumArgumentException when origin is not one of Begin, Current, or End — i.e. an int was cast to SeekOrigin outside its defined range.","triggerScenarios":"Calling sharedStream.Seek(offset, (SeekOrigin)someInt) where someInt is not 0, 1, or 2 — typically an uninitialized variable, bad interop value, or numeric constant mismatch.","commonSituations":"Interop/P-Invoke code passing raw longs into SeekOrigin casts; deserialization restoring a saved stream position with a corrupt origin field; off-by-one constants (3+).","solutions":["Pass only SeekOrigin.Begin, SeekOrigin.Current, or SeekOrigin.End — never a raw cast int.","Validate any externally supplied origin value before casting: if (!Enum.IsDefined(typeof(SeekOrigin), v)) ...","Fix the source of the bad value (saved state, interop marshaling) rather than the call site.","Default to SeekOrigin.Begin when the origin is unknown."],"exampleFix":"// before\nstream.Seek(offset, (SeekOrigin)savedOrigin);\n// after\nif (!Enum.IsDefined(typeof(SeekOrigin), savedOrigin)) throw new InvalidDataException($\"bad origin {savedOrigin}\");\nstream.Seek(offset, (SeekOrigin)savedOrigin);","handlingStrategy":"type-guard","validationCode":"// Validate SeekOrigin before calling Seek\nbool IsValidSeekOrigin(SeekOrigin origin) => origin is SeekOrigin.Begin or SeekOrigin.Current or SeekOrigin.End;\n// for raw ints: Enum.IsDefined(typeof(SeekOrigin), value)","typeGuard":"static bool IsDefinedSeekOrigin(int v) => v is >= 0 and <= 2; // Begin, Current, End","tryCatchPattern":"try\n{\n    stream.Seek(offset, origin);\n}\ncatch (InvalidEnumArgumentException ex)\n{\n    throw new InvalidDataException($\"Origin must be Begin/Current/End, got {origin}\", ex);\n}","preventionTips":["Never cast raw ints to SeekOrigin without Enum.IsDefined","Initialize SeekOrigin variables explicitly (default to SeekOrigin.Begin)","Check interop marshaling of origin values","Use the enum type in your own APIs to get compile-time safety"],"tags":["io","stream","enum","argument"],"backgroundTag":"invalid-enum-argument","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"}