Uninstall counterpart to cli_ensure_windows_user_path: remove exactly the * install-dir segment install added, leaving every other segment byte-for-byte * intact. Without this, every install/uninstall cycle leaves its entry behind * and the current-user PATH grows without bound (#2117). Segment identity uses * the same case- and trailing-separator-insensitive comparison as the install * `pres
| 6914 | * not present (nothing to do), CLI_ERR on a registry failure. dry_run reports |
| 6915 | * without mutating. */ |
| 6916 | static int cli_remove_windows_user_path(const char *bin_dir, bool dry_run) { |
| 6917 | wchar_t *wide_dir = cli_windows_utf8_to_wide(bin_dir); |
| 6918 | HKEY environment = NULL; |
| 6919 | if (!wide_dir || cli_windows_open_user_path_key(&environment) != CLI_OK) { |
| 6920 | free(wide_dir); |
| 6921 | return CLI_ERR; |
| 6922 | } |
| 6923 | |
| 6924 | DWORD type = REG_EXPAND_SZ; |
| 6925 | DWORD bytes = 0; |
| 6926 | LONG queried = RegQueryValueExW(environment, L"Path", NULL, &type, NULL, &bytes); |
| 6927 | if (queried == ERROR_FILE_NOT_FOUND) { |
| 6928 | /* No user PATH value at all — nothing of ours to remove. */ |
| 6929 | RegCloseKey(environment); |
| 6930 | free(wide_dir); |
| 6931 | return CLI_TRUE; |
| 6932 | } |
| 6933 | if (queried != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) { |
| 6934 | RegCloseKey(environment); |
| 6935 | free(wide_dir); |
| 6936 | return CLI_ERR; |
| 6937 | } |
| 6938 | size_t existing_capacity = (size_t)bytes / sizeof(wchar_t) + 1U; |
| 6939 | wchar_t *existing = calloc(existing_capacity, sizeof(*existing)); |
| 6940 | if (!existing) { |
| 6941 | RegCloseKey(environment); |
| 6942 | free(wide_dir); |
| 6943 | return CLI_ERR; |
| 6944 | } |
| 6945 | DWORD read_bytes = bytes; |
| 6946 | if (RegQueryValueExW(environment, L"Path", NULL, &type, (BYTE *)existing, &read_bytes) != |
| 6947 | ERROR_SUCCESS) { |
| 6948 | RegCloseKey(environment); |
| 6949 | free(existing); |
| 6950 | free(wide_dir); |
| 6951 | return CLI_ERR; |
| 6952 | } |
| 6953 | existing[existing_capacity - 1U] = L'\0'; |
| 6954 | |
| 6955 | /* Rebuild the value from every segment that is NOT our directory. Kept |
| 6956 | * segments keep their exact original characters; a single ';' rejoins |
| 6957 | * consecutive kept segments so unrelated entries survive byte-for-byte and |
| 6958 | * no stray separator is left where our entry used to be. The result is |
| 6959 | * never longer than the input, so the input length bounds the buffer. */ |
| 6960 | size_t existing_length = wcslen(existing); |
| 6961 | wchar_t *rebuilt = calloc(existing_length + 1U, sizeof(*rebuilt)); |
| 6962 | if (!rebuilt) { |
| 6963 | RegCloseKey(environment); |
| 6964 | free(existing); |
| 6965 | free(wide_dir); |
| 6966 | return CLI_ERR; |
| 6967 | } |
| 6968 | size_t out = 0; |
| 6969 | bool removed = false; |
| 6970 | bool wrote_segment = false; |
| 6971 | const wchar_t *cursor = existing; |
| 6972 | while (*cursor) { |
| 6973 | const wchar_t *separator = wcschr(cursor, L';'); |
no test coverage detected