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

Code before:
                        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)
{
    if (!wslRootKey)
    {
        return false;
    }

Review comment (bug):
Since you know you want to extract GUIDs and nothing else a sanity check might be useful. If the Lxss key ever changes to something different it might be better to fail early and reliably instead of accidentally working. Additionally this allows you to simplify the buffer a bit:

```cpp
HRESULT hr;
wchar_t buffer[39]; // a {GUID} is 38 chars long

for (;;) {
	DWORD length;
	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);
	}
}
```

Code after:
        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);
            const auto status = RegQueryValueExW(distroKey.get(), RegKeyDistroName, 0, nullptr, reinterpret_cast<BYTE*>(value), &length);
            // length will receive the number of bytes - convert to a number of
            // wchar_t's. AdaptFixedSizeToAllocatedResult will resize buffer to
            // valueLengthNeededWithNull