{"record":{"id":"ebc4d2fc523b5077","repo":"dotnet/maui","slug":"invalid-enumeration-value","errorCode":null,"errorMessage":"Invalid enumeration value","messagePattern":"Invalid enumeration value","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/WPF/Microsoft.Windows.Shell/Standard/Utilities.cs","lineNumber":325,"sourceCode":"\t\t\t\t\treturn null;\n\t\t\t\tcase SafeCopyFileOptions.Overwrite:\n\t\t\t\t\tFile.Copy(sourceFileName, destFileName, true);\n\t\t\t\t\treturn destFileName;\n\t\t\t\tcase SafeCopyFileOptions.FindBetterName:\n\t\t\t\t\tstring directoryPart = Path.GetDirectoryName(destFileName);\n\t\t\t\t\tstring fileNamePart = Path.GetFileNameWithoutExtension(destFileName);\n\t\t\t\t\tstring extensionPart = Path.GetExtension(destFileName);\n\t\t\t\t\tforeach (string path in GenerateFileNames(directoryPart, fileNamePart, extensionPart))\n\t\t\t\t\t{\n\t\t\t\t\t\tif (!File.Exists(path))\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tFile.Copy(sourceFileName, path);\n\t\t\t\t\t\t\treturn path;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\treturn null;\n\t\t\t}\n\t\t\tthrow new ArgumentException(\"Invalid enumeration value\", \"options\");\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Simple guard against the exceptions that File.Delete throws on null and empty strings.\n\t\t/// </summary>\n\t\t/// <param name=\"path\">The path to delete.  Unlike File.Delete, this can be null or empty.</param>\n\t\t/// <remarks>\n\t\t/// Note that File.Delete, and by extension SafeDeleteFile, does not throw an exception\n\t\t/// if the file does not exist.\n\t\t/// </remarks>\n\t\t[SuppressMessage(\"Microsoft.Performance\", \"CA1811:AvoidUncalledPrivateCode\")]\n\t\tpublic static void SafeDeleteFile(string path)\n\t\t{\n\t\t\tif (!string.IsNullOrEmpty(path))\n\t\t\t{\n\t\t\t\tFile.Delete(path);\n\t\t\t}\n\t\t}","sourceCodeStart":307,"sourceCodeEnd":343,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/WPF/Microsoft.Windows.Shell/Standard/Utilities.cs#L307-L343","documentation":"SafeCopyFile switches on the SafeCopyFileOptions enum (PreserveOriginal, Overwrite, FindBetterName) and throws ArgumentException with paramName \"options\" if none of the known cases match. This guards against an undefined enum value, which in C# is only possible via an illegal cast from an out-of-range integer.","triggerScenarios":"Calling Utilities.SafeCopyFile(source, dest, options) where options is an undefined SafeCopyFileOptions value — e.g. (SafeCopyFileOptions)99. Reachable only through interop, deserialization, or arithmetic on the enum.","commonSituations":"Config-driven code that maps an integer config value to the enum without validation; deserializing SafeCopyFileOptions from JSON with an unknown numeric value; future enum members added without updating the switch.","solutions":["Validate the options value with Enum.IsDefined(typeof(SafeCopyFileOptions), options) before calling SafeCopyFile.","Map integer config inputs through an explicit switch that rejects unknown values.","If a new enum member is added, update the switch in SafeCopyFile to handle it."],"exampleFix":"// before\nUtilities.SafeCopyFile(src, dest, (SafeCopyFileOptions)configValue);\n\n// after\nif (!Enum.IsDefined(typeof(SafeCopyFileOptions), configValue))\n{\n    throw new ArgumentOutOfRangeException(nameof(configValue));\n}\nUtilities.SafeCopyFile(src, dest, (SafeCopyFileOptions)configValue);","handlingStrategy":"validation","validationCode":"// Validate enum before calling SafeCopyFile\nif (!Enum.IsDefined(typeof(SafeCopyFileOptions), options))\n{\n    throw new ArgumentOutOfRangeException(nameof(options), \"Invalid SafeCopyFileOptions value.\");\n}\nUtilities.SafeCopyFile(source, dest, options);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate enum values from config or interop with Enum.IsDefined before use.","Map integer config inputs through an explicit, rejecting switch.","When adding a new enum member, update all switch statements that handle it."],"tags":["enum","validation","io","file-copy"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}