Repository: microsoft/terminal
File: src/host/readDataCooked.cpp (C++)

Code before:
    if (_echoInput)
    {
        // Figure out where real string ends (at carriage return or end of buffer).
        PWCHAR StringPtr = _backupLimit;
        size_t StringLength = _bytesRead;
        bool FoundCR = false;
        for (size_t i = 0; i < (_bytesRead / sizeof(WCHAR)); i++)
        {
            if (*StringPtr++ == UNICODE_CARRIAGERETURN)
            {
                StringLength = i * sizeof(WCHAR);
                FoundCR = true;
                break;
            }
        }

        if (FoundCR)
        {
            if (_commandHistory)
            {
                // add to command line recall list if we have a history list.
                CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
                LOG_IF_FAILED(_commandHistory->Add({ _backupLimit, StringLength / sizeof(wchar_t) },
                                                   WI_IsFlagSet(gci.Flags, CONSOLE_HISTORY_NODUP)));

                Tracing::s_TraceCookedRead(_backupLimit);
            }

            // check for alias
            ProcessAliases(LineCount);
        }
    }

    bool fAddDbcsLead = false;
    size_t NumBytes = 0;
    // at this point, a->NumBytes contains the number of bytes in
    // the UNICODE string read.  UserBufferSize contains the converted
    // size of the app's buffer.
    if (_bytesRead > _userBufferSize || LineCount > 1)
    {
        if (LineCount > 1)
        {
            PWSTR Tmp;
            if (!isUnicode)
            {
                if (_pInputBuffer->IsReadPartialByteSequenceAvailable())
                {
                    fAddDbcsLead = true;
                    std::unique_ptr<IInputEvent> event = GetInputBuffer()->FetchReadPartialByteSequence(false);
                    const KeyEvent* const pKeyEvent = static_cast<const KeyEvent* const>(event.get());
                    *_userBuffer = static_cast<char>(pKeyEvent->GetCharData());

Review comment (suggestion):
If there are too many client processes attached, some of them will not have a history list. We may want to place this outside the `if (_commandHistory)` block.

Code after:
    if (_echoInput)
    {
        // Figure out where real string ends (at carriage return or end of buffer).
        PWCHAR StringPtr = _backupLimit;
        size_t StringLength = _bytesRead;
        bool FoundCR = false;
        for (size_t i = 0; i < (_bytesRead / sizeof(WCHAR)); i++)
        {
            if (*StringPtr++ == UNICODE_CARRIAGERETURN)
            {
                StringLength = i * sizeof(WCHAR);
                FoundCR = true;
                break;
            }
        }

        if (FoundCR)
        {
            if (_commandHistory)
            {
                // add to command line recall list if we have a history list.
                CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
                LOG_IF_FAILED(_commandHistory->Add({ _backupLimit, StringLength / sizeof(wchar_t) },
                                                   WI_IsFlagSet(gci.Flags, CONSOLE_HISTORY_NODUP)));
            }

            Tracing::s_TraceCookedRead(_backupLimit);

            // check for alias
            ProcessAliases(LineCount);
        }
    }

    bool fAddDbcsLead = false;
    size_t NumBytes = 0;
    // at this point, a->NumBytes contains the number of bytes in
    // the UNICODE string read.  UserBufferSize contains the converted
    // size of the app's buffer.
    if (_bytesRead > _userBufferSize || LineCount > 1)
    {
        if (LineCount > 1)
        {
            PWSTR Tmp;
            if (!isUnicode)
            {
                if (_pInputBuffer->IsReadPartialByteSequenceAvailable())
                {
                    fAddDbcsLead = true;
                    std::unique_ptr<IInputEvent> event = GetInputBuffer()->FetchReadPartialByteSequence(false);
                    const KeyEvent* const pKeyEvent = static_cast<const KeyEvent* const>(event.get());
                    *_userBuffer = static_cast<char>(pKeyEvent->GetCharData());