Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsModel/WslDistroGenerator.cpp (C++)

Code before:
    // Figure out how many subkeys we have, and what the longest name of these subkeys is.
    DWORD dwNumSubKeys = 0;
    DWORD maxSubKeyLen = 0;
    if (RegQueryInfoKey(wslRootKey.get(), // hKey,
                        nullptr, // lpClass,
                        nullptr, // lpcchClass,
                        nullptr, // lpReserved,
                        &dwNumSubKeys, // lpcSubKeys,
                        &maxSubKeyLen, // lpcbMaxSubKeyLen,
                        nullptr, // lpcbMaxClassLen,
                        nullptr, // lpcValues,
                        nullptr, // lpcbMaxValueNameLen,
                        nullptr, // lpcbMaxValueLen,
                        nullptr, // lpcbSecurityDescriptor,
                        nullptr // lpftLastWriteTime
                        ) != ERROR_SUCCESS)
    {
        return false;
    }

    // lpcbMaxSubKeyLen does not include trailing space
    std::unique_ptr<wchar_t[]> buffer = std::make_unique<wchar_t[]>(maxSubKeyLen + 1);

    // reserve enough space for all the GUIDs we're about to enumerate
    guidStrings.reserve(dwNumSubKeys);
    for (DWORD i = 0; i < dwNumSubKeys; i++)
    {
        auto cbName = maxSubKeyLen + 1;
        const auto result = RegEnumKeyEx(wslRootKey.get(), i, buffer.get(), &cbName, nullptr, nullptr, nullptr, nullptr);
        if (result == ERROR_SUCCESS)
        {
            guidStrings.emplace_back(buffer.get(), cbName);
        }
    }
    return true;
}

// Function Description:
// - Get the list of all the names of all the WSL distros from the registry. If
//   we fail to open any regkey for the GUID of a distro, we'll just skip it.
//   Places the names of all the distros into the "names" param.
// Arguments:
// - wslRootKey: the HKEY for the Lxss node.
// - guidStrings: A list of all the GUIDs of the installed distros
// - names: a vector that recieves all the names of the installed distros.
// Return Value:
// - false if the root key was invalid, else true.
bool _getWslNames(const wil::unique_hkey& wslRootKey,
                  std::vector<std::wstring>& guidStrings,
                  std::vector<std::wstring>& names)
{

Review comment (suggestion):
I believe you should iterate until `ERROR_NO_MORE_ITEMS` instead.

Code after:
    wchar_t buffer[39]; // a {GUID} is 38 chars long
    for (DWORD i = 0;; i++)
    {
        DWORD length = 39;
        const auto result = RegEnumKeyEx(wslRootKey.get(), i, &buffer[0], &length, nullptr, nullptr, nullptr, nullptr);
        if (result == ERROR_NO_MORE_ITEMS)
        {
            break;
        }

        if (result == ERROR_SUCCESS &&
            length == 38 &&
            buffer[0] == L'{' &&
            buffer[37] == L'}')
        {
            guidStrings.emplace_back(&buffer[0], length);
        }
    }

    return true;
}

// Function Description:
// - Get the list of all the names of all the WSL distros from the registry. If
//   we fail to open any regkey for the GUID of a distro, we'll just skip it.
//   Places the names of all the distros into the "names" param.
// Arguments:
// - wslRootKey: the HKEY for the Lxss node.
// - guidStrings: A list of all the GUIDs of the installed distros
// - names: a vector that receives all the names of the installed distros.
// Return Value:
// - false if the root key was invalid, else true.
static bool getWslNames(const wil::unique_hkey& wslRootKey,
                        const std::vector<std::wstring>& guidStrings,
                        std::vector<std::wstring>& names)
{
    if (!wslRootKey)
    {
        return false;
    }
    for (const auto& guid : guidStrings)
    {
        wil::unique_hkey distroKey{ openDistroKey(wslRootKey, guid) };
        if (!distroKey)
        {
            continue;
        }

        std::wstring buffer;
        auto result = wil::AdaptFixedSizeToAllocatedResult<std::wstring, 256>(buffer, [&](PWSTR value, size_t valueLength, size_t* valueLengthNeededWithNull) -> HRESULT {
            auto length = static_cast<DWORD>(valueLength);