{"record":{"id":"565af0d9b2b91e60","repo":"BCUninstaller/Bulk-Crap-Uninstaller","slug":"path-is-too-short-invalid","errorCode":null,"errorMessage":"Path is too short/invalid","messagePattern":"Path is too short/invalid","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"source/KlocTools/Tools/RegistryTools.cs","lineNumber":215,"sourceCode":"                }\n\n                throw;\n            }\n        }\n\n        /// <summary>\n        ///     Open registry key using its fully qualified path.\n        ///     Root key can be named by either its long or short name. (long: \"HKEY_LOCAL_MACHINE\", short: \"HKLM\")\n        /// </summary>\n        /// <param name=\"fullPath\">Full path of the requested registry key</param>\n        /// <param name=\"writable\">If false, key is opened read-only</param>\n        public static RegistryKey OpenRegistryKey(string fullPath, bool writable)\n        {\n            if (fullPath == null)\n                throw new ArgumentNullException(nameof(fullPath));\n\n            if (fullPath.Length < 4)\n                throw new ArgumentException(\"Path is too short/invalid\");\n\n            var rootKey = GetRootHive(fullPath);\n\n            var result = rootKey.OpenSubKey(StripKeyRoot(fullPath), writable);\n            //if (result == null)\n            //    throw new ArgumentException(\"Invalid subpath\");\n            return result;\n        }\n\n        [return: NotNull]\n        private static RegistryKey GetRootHive(string fullPath)\n        {\n            RegistryKey rootKey;\n            switch (GetKeyRoot(fullPath, true))\n            {\n                case HklmShortRootName:\n                    rootKey = Registry.LocalMachine;\n                    break;","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/BCUninstaller/Bulk-Crap-Uninstaller/blob/608321de98e92297377b1eb69029af55c25504a1/source/KlocTools/Tools/RegistryTools.cs#L197-L233","documentation":"Thrown by RegistryTools.OpenRegistryKey(string fullPath, bool writable). After a null check, if fullPath.Length < 4 it throws ArgumentException(\"Path is too short/invalid\"). The magic 4 is the minimum needed to encode a root hive short name (e.g. \"HKLM\") plus at least a separator; shorter strings cannot name a hive and GetRootHive would fail.","triggerScenarios":"Passing a registry path shorter than 4 characters: \"\", \"HK\", \"X\", or a stray value read from config that is empty/almost empty.","commonSituations":"Config-supplied registry path left blank or truncated, concatenation bug producing just a hive prefix, or user input that was never validated.","solutions":["Validate the path length and root prefix before calling: require it to start with a known hive name (HKLM, HKCU, HKCR, HKU, HKCC or the long forms).","Reject empty/short paths at the config/UI layer rather than relying on the runtime throw.","Use the helper GetRootHive/StripKeyRoot flow yourself to confirm the hive resolves before opening."],"exampleFix":"// before\nvar key = RegistryTools.OpenRegistryKey(input, writable: true); // input may be \"HK\"\n\n// after\nvar hives = new[]{ \"HKLM\",\"HKCU\",\"HKCR\",\"HKU\",\"HKCC\",\"HKEY_\" };\nif (string.IsNullOrEmpty(input) || !hives.Any(h => input.StartsWith(h, StringComparison.OrdinalIgnoreCase)))\n    throw new ArgumentException(\"Invalid registry path\");\nvar key = RegistryTools.OpenRegistryKey(input, writable: true);","handlingStrategy":"validation","validationCode":"var roots = new[]{ \"HKLM\",\"HKCU\",\"HKCR\",\"HKU\",\"HKCC\",\"HKEY_LOCAL_MACHINE\",\"HKEY_CURRENT_USER\",\"HKEY_CLASSES_ROOT\",\"HKEY_USERS\",\"HKEY_CURRENT_CONFIG\" };\nif (string.IsNullOrEmpty(fullPath) || fullPath.Length < 4 || !roots.Any(r => fullPath.StartsWith(r, StringComparison.OrdinalIgnoreCase)))\n    throw new ArgumentException(\"Invalid registry path\");\nvar key = RegistryTools.OpenRegistryKey(fullPath, writable);","typeGuard":"static bool IsValidRegistryPath(string p)\n    => !string.IsNullOrEmpty(p) && p.Length >= 4\n       && (p.StartsWith(\"HK\", StringComparison.OrdinalIgnoreCase)\n           || p.StartsWith(\"HKEY_\", StringComparison.OrdinalIgnoreCase));","tryCatchPattern":"try { return RegistryTools.OpenRegistryKey(path, writable); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"too short\"))\n{ return null; }","preventionTips":["Validate registry paths against known root hive prefixes at the config/UI boundary.","Never feed empty or near-empty strings into OpenRegistryKey.","Use GetRootHive yourself to confirm a hive resolves before opening."],"tags":["registry","validation","windows","csharp","kloctools"],"backgroundTag":null,"analyzedSha":"608321de98e92297377b1eb69029af55c25504a1","analyzedAt":"2026-08-13T12:17:35.389Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}