{"record":{"id":"163056fe49639906","repo":"dotnet/maui","slug":"bad-value-for-origin","errorCode":null,"errorMessage":"Bad value for origin","messagePattern":"Bad value for origin","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/WPF/Microsoft.Windows.Shell/Standard/StreamHelper.cs","lineNumber":702,"sourceCode":"            }\n            return cbRead;\n        }\n\n        public override long Seek(long offset, SeekOrigin origin)\n        {\n            switch (origin)\n            {\n                case SeekOrigin.Begin:\n                    Position = offset;\n                    break;\n                case SeekOrigin.Current:\n                    Position += offset;\n                    break;\n                case SeekOrigin.End:\n                    Position = Length + offset;\n                    break;\n                default:\n                    throw new FormatException(\"Bad value for origin\");\n            }\n            return Position;\n        }\n\n        public override void SetLength(long value)\n        {\n            throw new NotSupportedException();\n        }\n\n        public override void Write(byte[] buffer, int offset, int count)\n        {\n            throw new NotSupportedException();\n        }\n    }\n#endif\n}\n","sourceCodeStart":684,"sourceCodeEnd":719,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/WPF/Microsoft.Windows.Shell/Standard/StreamHelper.cs#L684-L719","documentation":"StringStream.Seek switches on the SeekOrigin enum (Begin, Current, End) and throws FormatException for any other value. Since SeekOrigin is a well-known BCL enum, the default branch is only reachable if the enum is cast from an out-of-range integer. The error indicates a corrupted or illegally-cast SeekOrigin value.","triggerScenarios":"Calling Seek on a StringStream with a SeekOrigin value produced by casting an integer that is not 0, 1, or 2 — e.g. (SeekOrigin)42. This is typically a programming bug rather than a data-driven condition.","commonSituations":"Interop code that marshals an integer origin from native code without validation; deserialization of a SeekOrigin from untrusted or corrupt data; accidental arithmetic on the enum before passing it.","solutions":["Validate that origin is one of SeekOrigin.Begin, SeekOrigin.Current, or SeekOrigin.End before calling Seek.","If the origin arrives as an int from interop, map it through a known mapping table and reject unknown values upstream.","Use Enum.IsDefined(typeof(SeekOrigin), value) to guard against out-of-range casts."],"exampleFix":"// before\nstream.Seek(offset, (SeekOrigin)originInt);\n\n// after\nif (!Enum.IsDefined(typeof(SeekOrigin), originInt))\n{\n    throw new ArgumentOutOfRangeException(nameof(originInt));\n}\nstream.Seek(offset, (SeekOrigin)originInt);","handlingStrategy":"validation","validationCode":"// Validate SeekOrigin before calling Seek\nif (!Enum.IsDefined(typeof(SeekOrigin), origin))\n{\n    throw new ArgumentOutOfRangeException(nameof(origin), \"Invalid SeekOrigin value.\");\n}\nstream.Seek(offset, origin);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never cast arbitrary integers to SeekOrigin without validation.","When receiving origin from interop or deserialization, validate with Enum.IsDefined.","Keep enum arithmetic out of stream-seek call sites."],"tags":["stream","enum","validation","interop"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}