BCUninstaller/Bulk-Crap-Uninstaller · error · ArgumentException

Path is too short/invalid

Error message

Path is too short/invalid

What it means

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.

Source

Thrown at source/KlocTools/Tools/RegistryTools.cs:215

                }

                throw;
            }
        }

        /// <summary>
        ///     Open registry key using its fully qualified path.
        ///     Root key can be named by either its long or short name. (long: "HKEY_LOCAL_MACHINE", short: "HKLM")
        /// </summary>
        /// <param name="fullPath">Full path of the requested registry key</param>
        /// <param name="writable">If false, key is opened read-only</param>
        public static RegistryKey OpenRegistryKey(string fullPath, bool writable)
        {
            if (fullPath == null)
                throw new ArgumentNullException(nameof(fullPath));

            if (fullPath.Length < 4)
                throw new ArgumentException("Path is too short/invalid");

            var rootKey = GetRootHive(fullPath);

            var result = rootKey.OpenSubKey(StripKeyRoot(fullPath), writable);
            //if (result == null)
            //    throw new ArgumentException("Invalid subpath");
            return result;
        }

        [return: NotNull]
        private static RegistryKey GetRootHive(string fullPath)
        {
            RegistryKey rootKey;
            switch (GetKeyRoot(fullPath, true))
            {
                case HklmShortRootName:
                    rootKey = Registry.LocalMachine;
                    break;

View on GitHub (pinned to 608321de98)

Solutions

  1. 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).
  2. Reject empty/short paths at the config/UI layer rather than relying on the runtime throw.
  3. Use the helper GetRootHive/StripKeyRoot flow yourself to confirm the hive resolves before opening.

Example fix

// before
var key = RegistryTools.OpenRegistryKey(input, writable: true); // input may be "HK"

// after
var hives = new[]{ "HKLM","HKCU","HKCR","HKU","HKCC","HKEY_" };
if (string.IsNullOrEmpty(input) || !hives.Any(h => input.StartsWith(h, StringComparison.OrdinalIgnoreCase)))
    throw new ArgumentException("Invalid registry path");
var key = RegistryTools.OpenRegistryKey(input, writable: true);
Defensive patterns

Strategy: validation

Validate before calling

var roots = new[]{ "HKLM","HKCU","HKCR","HKU","HKCC","HKEY_LOCAL_MACHINE","HKEY_CURRENT_USER","HKEY_CLASSES_ROOT","HKEY_USERS","HKEY_CURRENT_CONFIG" };
if (string.IsNullOrEmpty(fullPath) || fullPath.Length < 4 || !roots.Any(r => fullPath.StartsWith(r, StringComparison.OrdinalIgnoreCase)))
    throw new ArgumentException("Invalid registry path");
var key = RegistryTools.OpenRegistryKey(fullPath, writable);

Type guard

static bool IsValidRegistryPath(string p)
    => !string.IsNullOrEmpty(p) && p.Length >= 4
       && (p.StartsWith("HK", StringComparison.OrdinalIgnoreCase)
           || p.StartsWith("HKEY_", StringComparison.OrdinalIgnoreCase));

Try / catch

try { return RegistryTools.OpenRegistryKey(path, writable); }
catch (ArgumentException ex) when (ex.Message.Contains("too short"))
{ return null; }

Prevention

When it happens

Trigger: Passing a registry path shorter than 4 characters: "", "HK", "X", or a stray value read from config that is empty/almost empty.

Common situations: Config-supplied registry path left blank or truncated, concatenation bug producing just a hive prefix, or user input that was never validated.

Related errors


AI-assisted analysis of BCUninstaller/Bulk-Crap-Uninstaller@608321de98 (2026-08-13). Data as JSON: /api/errors/565af0d9b2b91e60. Report an issue: GitHub.