Repository: microsoft/terminal
File: src/cascadia/TerminalConnection/ConptyConnection.cpp (C++)

Code before:
        }
    }

    static wil::unique_hfile duplicateHandle(const HANDLE in)
    {
        wil::unique_hfile h;
        THROW_IF_WIN32_BOOL_FALSE(DuplicateHandle(GetCurrentProcess(), in, GetCurrentProcess(), h.addressof(), 0, FALSE, DUPLICATE_SAME_ACCESS));
        return h;
    }

    // Misdiagnosis: out is being tested right in the first line.
#pragma warning(suppress : 26430) // Symbol 'out' is not tested for nullness on all paths (f.23).
    void ConptyConnection::InitializeFromHandoff(HANDLE* in, HANDLE* out, HANDLE signal, HANDLE reference, HANDLE server, HANDLE client, const TERMINAL_STARTUP_INFO* startupInfo)
    {
        THROW_HR_IF(E_UNEXPECTED, !in || !out || !startupInfo);

        _sessionId = Utils::CreateGuid();

        auto pipe = Utils::CreateOverlappedPipe(PIPE_ACCESS_DUPLEX, 128 * 1024);
        auto pipeClientClone = duplicateHandle(pipe.client.get());

        auto ownedSignal = duplicateHandle(signal);
        auto ownedReference = duplicateHandle(reference);
        auto ownedServer = duplicateHandle(server);
        wil::unique_hfile ownedClient;
        LOG_IF_WIN32_BOOL_FALSE(DuplicateHandle(GetCurrentProcess(), client, GetCurrentProcess(), ownedClient.addressof(), PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_SET_INFORMATION | SYNCHRONIZE, FALSE, 0));
        if (!ownedClient)
        {
            // If we couldn't reopen the handle with SET_INFORMATION, which may be required to do things like QoS management, fall back.
            THROW_IF_WIN32_BOOL_FALSE(DuplicateHandle(GetCurrentProcess(), client, GetCurrentProcess(), ownedClient.addressof(), 0, FALSE, DUPLICATE_SAME_ACCESS));
        }

        THROW_IF_FAILED(ConptyPackPseudoConsole(ownedServer.get(), ownedReference.get(), ownedSignal.get(), &_hPC));
        ownedServer.release();
        ownedReference.release();
        ownedSignal.release();

        _piClient.hProcess = ownedClient.release();

        _startupInfo.title = winrt::hstring{ startupInfo->pszTitle, SysStringLen(startupInfo->pszTitle) };
        _startupInfo.iconPath = winrt::hstring{ startupInfo->pszIconPath, SysStringLen(startupInfo->pszIconPath) };
        _startupInfo.iconIndex = startupInfo->iconIndex;
        _startupInfo.showWindow = startupInfo->wShowWindow;

        try
        {
            _commandline = _commandlineFromProcess(_piClient.hProcess);
        }
        CATCH_LOG()

        try

Review comment (bug):
YMMV but I think it may not be worth logging this.

Also, if this failed, should `RootProcessHandle()` even return something?

Code after:
        }
    }

    static wil::unique_hfile duplicateHandle(const HANDLE in)
    {
        wil::unique_hfile h;
        THROW_IF_WIN32_BOOL_FALSE(DuplicateHandle(GetCurrentProcess(), in, GetCurrentProcess(), h.addressof(), 0, FALSE, DUPLICATE_SAME_ACCESS));
        return h;
    }

    // Misdiagnosis: out is being tested right in the first line.
#pragma warning(suppress : 26430) // Symbol 'out' is not tested for nullness on all paths (f.23).
    void ConptyConnection::InitializeFromHandoff(HANDLE* in, HANDLE* out, HANDLE signal, HANDLE reference, HANDLE server, HANDLE client, const TERMINAL_STARTUP_INFO* startupInfo)
    {
        THROW_HR_IF(E_UNEXPECTED, !in || !out || !startupInfo);

        _sessionId = Utils::CreateGuid();

        auto pipe = Utils::CreateOverlappedPipe(PIPE_ACCESS_DUPLEX, 128 * 1024);
        auto pipeClientClone = duplicateHandle(pipe.client.get());

        auto ownedSignal = duplicateHandle(signal);
        auto ownedReference = duplicateHandle(reference);
        auto ownedServer = duplicateHandle(server);
        wil::unique_hfile ownedClient;
        LOG_IF_WIN32_BOOL_FALSE(DuplicateHandle(GetCurrentProcess(), client, GetCurrentProcess(), ownedClient.addressof(), PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_SET_INFORMATION | SYNCHRONIZE, FALSE, 0));
        if (!ownedClient)
        {
            // If we couldn't reopen the handle with SET_INFORMATION, which may be required to do things like QoS management, fall back.
            ownedClient = duplicateHandle(client);
        }

        THROW_IF_FAILED(ConptyPackPseudoConsole(ownedServer.get(), ownedReference.get(), ownedSignal.get(), &_hPC));
        ownedServer.release();
        ownedReference.release();
        ownedSignal.release();

        _piClient.hProcess = ownedClient.release();

        _startupInfo.title = winrt::hstring{ startupInfo->pszTitle, SysStringLen(startupInfo->pszTitle) };
        _startupInfo.iconPath = winrt::hstring{ startupInfo->pszIconPath, SysStringLen(startupInfo->pszIconPath) };
        _startupInfo.iconIndex = startupInfo->iconIndex;
        _startupInfo.showWindow = startupInfo->wShowWindow;

        try
        {
            _commandline = _commandlineFromProcess(_piClient.hProcess);
        }
        CATCH_LOG()

        try