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

Code before:
    static HRESULT _CreatePseudoConsoleAndPipes(const COORD size, const DWORD dwFlags, HANDLE* phInput, HANDLE* phOutput, HPCON* phPC) noexcept
    {
        RETURN_HR_IF(E_INVALIDARG, phPC == nullptr || phInput == nullptr || phOutput == nullptr);

        wil::unique_hfile outPipeOurSide, outPipePseudoConsoleSide;
        wil::unique_hfile inPipeOurSide, inPipePseudoConsoleSide;

        RETURN_IF_WIN32_BOOL_FALSE(CreatePipe(&inPipePseudoConsoleSide, &inPipeOurSide, nullptr, 0));
        RETURN_IF_WIN32_BOOL_FALSE(CreatePipe(&outPipeOurSide, &outPipePseudoConsoleSide, nullptr, 0));
        RETURN_IF_FAILED(ConptyCreatePseudoConsole(size, inPipePseudoConsoleSide.get(), outPipePseudoConsoleSide.get(), dwFlags, phPC));
        *phInput = inPipeOurSide.release();
        *phOutput = outPipeOurSide.release();
        return S_OK;
    }

    // Function Descrption:
    // - Promotes a starting directory provided to a WSL invocation to a commandline argument.
    //   This is necessary because WSL has some modicum of support for linux-side directories (!) which
    //   CreateProcess never will.
    static std::tuple<std::wstring, std::wstring> _tryMangleStartingDirectoryForWSL(std::wstring_view commandLine, std::wstring_view startingDirectory)
    {
        do
        {
            if (startingDirectory.size() > 0 && commandLine.size() >= 3)
            { // "wsl" is three characters; this is a safe bet. no point in doing it if there's no starting directory though!
                // Find the first space, quote or the end of the string -- we'll look for wsl before that.
                const auto terminator{ commandLine.find_first_of(LR"(" )", 1) }; // look past the first character in case it starts with "
                const auto start{ til::at(commandLine, 0) == L'"' ? 1 : 0 };
                const std::filesystem::path executablePath{ commandLine.substr(start, terminator - start) };
                const auto executableFilename{ executablePath.filename().wstring() };
                if (executableFilename == L"wsl" || executableFilename == L"wsl.exe")
                {
                    // We've got a WSL -- let's just make sure it's the right one.
                    if (executablePath.has_parent_path())
                    {
                        std::wstring systemDirectory{};
                        if (FAILED(wil::GetSystemDirectoryW(systemDirectory)))
                        {
                            break; // just bail out.
                        }
                        if (executablePath.parent_path().wstring() != systemDirectory)
                        {
                            break; // it wasn't in system32!
                        }
                    }
                    else
                    {
                        // assume that unqualified WSL is the one in system32 (minor danger)
                    }

                    const auto arguments{ terminator == std::wstring_view::npos ? std::wstring_view{} : commandLine.substr(terminator + 1) };

Review comment (suggestion):
```suggestion
            { 
                // "wsl" is three characters; this is a safe bet. no point in doing it if there's no starting directory though!
```

Code after:
#pragma warning(suppress : 26430) // This statement sufficiently checks the out parameters. Analyzer cannot find this.
    static HRESULT _CreatePseudoConsoleAndPipes(const COORD size, const DWORD dwFlags, HANDLE* phInput, HANDLE* phOutput, HPCON* phPC) noexcept
    {
        RETURN_HR_IF(E_INVALIDARG, phPC == nullptr || phInput == nullptr || phOutput == nullptr);

        wil::unique_hfile outPipeOurSide, outPipePseudoConsoleSide;
        wil::unique_hfile inPipeOurSide, inPipePseudoConsoleSide;

        RETURN_IF_WIN32_BOOL_FALSE(CreatePipe(&inPipePseudoConsoleSide, &inPipeOurSide, nullptr, 0));
        RETURN_IF_WIN32_BOOL_FALSE(CreatePipe(&outPipeOurSide, &outPipePseudoConsoleSide, nullptr, 0));
        RETURN_IF_FAILED(ConptyCreatePseudoConsole(size, inPipePseudoConsoleSide.get(), outPipePseudoConsoleSide.get(), dwFlags, phPC));
        *phInput = inPipeOurSide.release();
        *phOutput = outPipeOurSide.release();
        return S_OK;
    }

    // Function Description:
    // - Promotes a starting directory provided to a WSL invocation to a commandline argument.
    //   This is necessary because WSL has some modicum of support for linux-side directories (!) which
    //   CreateProcess never will.
    static std::tuple<std::wstring, std::wstring> _tryMangleStartingDirectoryForWSL(std::wstring_view commandLine, std::wstring_view startingDirectory)
    {
        do
        {
            if (startingDirectory.size() > 0 && commandLine.size() >= 3)
            { // "wsl" is three characters; this is a safe bet. no point in doing it if there's no starting directory though!
                // Find the first space, quote or the end of the string -- we'll look for wsl before that.
                const auto terminator{ commandLine.find_first_of(LR"(" )", 1) }; // look past the first character in case it starts with "
                const auto start{ til::at(commandLine, 0) == L'"' ? 1 : 0 };
                const std::filesystem::path executablePath{ commandLine.substr(start, terminator - start) };
                const auto executableFilename{ executablePath.filename().wstring() };
                if (executableFilename == L"wsl" || executableFilename == L"wsl.exe")
                {
                    // We've got a WSL -- let's just make sure it's the right one.
                    if (executablePath.has_parent_path())
                    {
                        std::wstring systemDirectory{};
                        if (FAILED(wil::GetSystemDirectoryW(systemDirectory)))
                        {
                            break; // just bail out.
                        }
                        if (executablePath.parent_path().wstring() != systemDirectory)
                        {
                            break; // it wasn't in system32!
                        }
                    }
                    else
                    {
                        // assume that unqualified WSL is the one in system32 (minor danger)
                    }