{"record":{"id":"87924cfff8b4d793","repo":"dotnet/wpf","slug":"origin-cfstream","errorCode":null,"errorMessage":"origin","messagePattern":"origin","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CFStream.cs","lineNumber":174,"sourceCode":"                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 );\n\n        return seekPos;\n    }\n\n    /// <summary>\n    /// See .NET Framework SDK under System.IO.Stream\n    /// </summary>\n    /// <param name=\"newLength\">New length</param>\n    public override void SetLength( long newLength )\n    {\n        CheckDisposedStatus();\n\n        if (!CanWrite)\n        {\n            throw new NotSupportedException(SR.SetLengthNotSupported);","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CFStream.cs#L156-L192","documentation":"CFStream.Seek throws InvalidEnumArgumentException when the origin parameter is not one of the three SeekOrigin values (Begin, Current, End). The default case of the switch rejects any out-of-range or undefined enum value before it can be forwarded to the native compound-file IStream. This protects the native call from an unmapped seek-origin constant.","triggerScenarios":"Calling Seek with an invalid cast, e.g. stream.Seek(0, (SeekOrigin)7) or an uninitialized/garbage SeekOrigin variable.","commonSituations":"Reading the origin from config/data or interop code that stores it as int; enum values from older or mismatched API versions; unvalidated deserialized enum fields.","solutions":["Only pass SeekOrigin.Begin, SeekOrigin.Current, or SeekOrigin.End","Validate the raw value before casting: if (Enum.IsDefined(typeof(SeekOrigin), value))","Catch InvalidEnumArgumentException to detect corrupted origin data"],"exampleFix":"// before\nstream.Seek(offset, (SeekOrigin)rawValue);\n// after\nif (!Enum.IsDefined(typeof(SeekOrigin), rawValue)) throw new ArgumentException(nameof(rawValue));\nstream.Seek(offset, (SeekOrigin)rawValue);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(SeekOrigin), origin)) throw new InvalidEnumArgumentException(nameof(origin), (int)origin, typeof(SeekOrigin));","typeGuard":"static bool IsDefinedSeekOrigin(SeekOrigin o) => o is SeekOrigin.Begin or SeekOrigin.Current or SeekOrigin.End;","tryCatchPattern":"try { stream.Seek(offset, origin); } catch (System.ComponentModel.InvalidEnumArgumentException ex) { /* reject bad origin */ }","preventionTips":["Never cast raw ints to SeekOrigin without Enum.IsDefined","Store seek origins as validated enum values, not ints","Parse enum values with Enum.TryParse<SeekOrigin>"],"tags":["invalid-enum","argument","stream","wpf"],"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-21T21:30:21.729Z"}