Repository: microsoft/terminal
File: src/cascadia/TerminalApp/TerminalPage.cpp (C++)

Code before:
                }
            }

            if (_settings.GlobalSettings().TrimPaste())
            {
                text = { Utils::TrimPaste(text) };
                if (text.empty())
                {
                    // Text is all white space, nothing to paste
                    co_return;
                }
            }

            // If the requesting terminal is in bracketed paste mode, then we don't need to warn about a multi-line paste.
            auto warnMultiLine = _settings.GlobalSettings().WarnAboutMultiLinePaste() &&
                                 !eventArgs.BracketedPasteEnabled();
            if (warnMultiLine)
            {
                const auto isNewLineLambda = [](auto c) { return c == L'\n' || c == L'\r'; };
                const auto hasNewLine = std::find_if(text.cbegin(), text.cend(), isNewLineLambda) != text.cend();
                warnMultiLine = hasNewLine;
            }

            constexpr const std::size_t minimumSizeForWarning = 1024 * 5; // 5 KiB
            const auto warnLargeText = text.size() > minimumSizeForWarning &&
                                       _settings.GlobalSettings().WarnAboutLargePaste();

            if (warnMultiLine || warnLargeText)
            {
                co_await wil::resume_foreground(Dispatcher());

                // We have to initialize the dialog here to be able to change the text of the text block within it
                FindName(L"MultiLinePasteDialog").try_as<WUX::Controls::ContentDialog>();
                ClipboardText().Text(text);

                // The vertical offset on the scrollbar does not reset automatically, so reset it manually
                ClipboardContentScrollViewer().ScrollToVerticalOffset(0);

                auto warningResult = ContentDialogResult::Primary;
                if (warnMultiLine)
                {
                    warningResult = co_await _ShowMultiLinePasteWarningDialog();
                }
                else if (warnLargeText)
                {
                    warningResult = co_await _ShowLargePasteWarningDialog();
                }

                // Clear the clipboard text so it doesn't lie around in memory
                ClipboardText().Text(L"");


Review comment (suggestion):
technically there's `wil::unique_hglobal_locked`, which takes an HGLOBAL and unlocks it later! On its own!

Code after:
                    co_return;
                }

                attempts++;
                Sleep(10 * attempts);
            }

            const auto clipboardCleanup = wil::scope_exit([]() {
                CloseClipboard();
            });

            const auto data = GetClipboardData(CF_UNICODETEXT);
            if (!data)
            {
                co_return;
            }

            const auto str = static_cast<const wchar_t*>(GlobalLock(data));
            if (!str)
            {
                co_return;
            }

            const auto dataCleanup = wil::scope_exit([&]() {
                GlobalUnlock(data);
            });

            const auto maxLength = GlobalSize(data) / sizeof(wchar_t);
            const auto length = wcsnlen(str, maxLength);
            text = winrt::hstring{ str, gsl::narrow_cast<uint32_t>(length) };
        }

        if (globalSettings.TrimPaste())
        {
            text = { Utils::TrimPaste(text) };
            if (text.empty())
            {
                // Text is all white space, nothing to paste
                co_return;
            }
        }

        // If the requesting terminal is in bracketed paste mode, then we don't need to warn about a multi-line paste.
        auto warnMultiLine = globalSettings.WarnAboutMultiLinePaste() && !eventArgs.BracketedPasteEnabled();
        if (warnMultiLine)
        {
            const auto isNewLineLambda = [](auto c) { return c == L'\n' || c == L'\r'; };
            const auto hasNewLine = std::find_if(text.cbegin(), text.cend(), isNewLineLambda) != text.cend();
            warnMultiLine = hasNewLine;
        }