Repository: microsoft/terminal
File: src/cascadia/TerminalControl/TermControlAutomationPeer.cpp (C++)

Code before:
            return;
        }
        dispatcher.RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [weakThis{ get_weak() }]() {
            if (auto strongThis{ weakThis.get() })
            {
                if (auto control{ strongThis->_termControl.get() })
                {
                    // The event that is raised when the text was changed in an edit control.
                    // Do NOT fire a TextEditTextChanged. Generally, an app on the other side
                    //    will expect more information. Though you can dispatch that event
                    //    on its own, it may result in a nullptr exception on the other side
                    //    because no additional information was provided. Crashing the screen
                    //    reader.
                    strongThis->RaiseAutomationEvent(AutomationEvents::TextPatternOnTextSelectionChanged);
                }
            }
        });
    }

    void TermControlAutomationPeer::NotifyNewOutput(std::wstring_view newOutput)
    {
        // Try to suppress any events (or event data)
        // that is just the keypress the user made
        auto sanitized{ Sanitize(newOutput) };
        while (!_keyEvents.empty() && IsReadable(sanitized))
        {
            if (til::toupper_ascii(sanitized.front()) == _keyEvents.front())
            {
                // the key event's character (i.e. the "A" key) matches
                // the output character (i.e. "a" or "A" text).
                // We can assume that the output character resulted from
                // the pressed key, so we can ignore it.
                sanitized = sanitized.substr(1);
                _keyEvents.pop_front();
            }
            else
            {
                // The output doesn't match,
                // so clear the input stack and
                // move on to fire the event.
                _keyEvents.clear();
                break;
            }
        }

        // Suppress event if the remaining text is not readable
        if (!IsReadable(sanitized))
        {
            return;
        }


Review comment (security):
you might need to make this `auto&` or even (C++14) `decltype(auto)`. I _think_ that `auto keyEvents` will result in a _copy_ of `keyEvents` (thanks to how auto works).

<img width="207" alt="image" src="https://user-images.githubusercontent.com/189190/213035815-2435ffac-8862-48fb-90d6-d8e3a4156ac3.png">

<img width="206" alt="image" src="https://user-images.githubusercontent.com/189190/213035825-0830ad79-ec92-42df-a67b-67f2b64027f3.png">

See how the first one compiles down to `escape(9)` even though we just set the value to `7` through `auto m`!

Code after:
            return;
        }
        dispatcher.RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [weakThis{ get_weak() }]() {
            if (auto strongThis{ weakThis.get() })
            {
                if (auto control{ strongThis->_termControl.get() })
                {
                    // The event that is raised when the text was changed in an edit control.
                    // Do NOT fire a TextEditTextChanged. Generally, an app on the other side
                    //    will expect more information. Though you can dispatch that event
                    //    on its own, it may result in a nullptr exception on the other side
                    //    because no additional information was provided. Crashing the screen
                    //    reader.
                    strongThis->RaiseAutomationEvent(AutomationEvents::TextPatternOnTextSelectionChanged);
                }
            }
        });
    }

    void TermControlAutomationPeer::NotifyNewOutput(std::wstring_view newOutput)
    {
        auto sanitized{ Sanitize(newOutput) };
        // Try to suppress any events (or event data)
        // that are just the keypresses the user made
        {
            auto keyEvents = _keyEvents.lock();
            while (!keyEvents->empty() && IsReadable(sanitized))
            {
                if (til::toupper_ascii(sanitized.front()) == keyEvents->front())
                {
                    // the key event's character (i.e. the "A" key) matches
                    // the output character (i.e. "a" or "A" text).
                    // We can assume that the output character resulted from
                    // the pressed key, so we can ignore it.
                    sanitized = sanitized.substr(1);
                    keyEvents->pop_front();
                }
                else
                {
                    // The output doesn't match,
                    // so clear the input stack and
                    // move on to fire the event.
                    keyEvents->clear();
                    break;
                }
            }
        }

        // Suppress event if the remaining text is not readable
        if (!IsReadable(sanitized))
        {