Skip to content

Commit

Permalink
fix SA issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ZoeyR committed Feb 25, 2020
1 parent a6c14fc commit 70b9f05
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 46 deletions.
88 changes: 45 additions & 43 deletions src/cascadia/PublicTerminalCore/HwndTerminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ HRESULT HwndTerminal::Initialize()
_terminal->Create(COORD{ 80, 25 }, 1000, *_renderer);
_terminal->SetDefaultBackground(RGB(5, 27, 80));
_terminal->SetDefaultForeground(RGB(255, 255, 255));
_terminal->SetWriteInputCallback([=](std::wstring& input) { _WriteTextToConnection(input); });
_terminal->SetWriteInputCallback([=](std::wstring & input) noexcept { _WriteTextToConnection(input); });
localPointerToThread->EnablePainting();

return S_OK;
Expand All @@ -171,7 +171,7 @@ void HwndTerminal::RegisterScrollCallback(std::function<void(int, int, int)> cal
_terminal->SetScrollPositionChangedCallback(callback);
}

void HwndTerminal::_WriteTextToConnection(std::wstring& input) noexcept
void HwndTerminal::_WriteTextToConnection(const std::wstring& input) noexcept
{
if (!_pfnWriteCallback)
{
Expand All @@ -185,15 +185,22 @@ void HwndTerminal::_WriteTextToConnection(std::wstring& input) noexcept

callingText = static_cast<wchar_t*>(::CoTaskMemAlloc(textBytes));

if (callingText == nullptr)
try
{
_pfnWriteCallback(nullptr);
if (callingText == nullptr)
{
_pfnWriteCallback(nullptr);
}
else
{
wcscpy_s(callingText, textChars, text);

_pfnWriteCallback(callingText);
}
}
else
catch (...)
{
wcscpy_s(callingText, textChars, text);

_pfnWriteCallback(callingText);
LOG_HR(wil::ResultFromCaughtException());
}
}

Expand Down Expand Up @@ -362,22 +369,22 @@ void _stdcall TerminalUserScroll(void* terminal, int viewTop)

HRESULT HwndTerminal::_StartSelection(LPARAM lParam) noexcept
{
bool altPressed = GetKeyState(VK_MENU) < 0;
COORD cursorPosition{
GET_X_LPARAM(lParam),
GET_Y_LPARAM(lParam),
};
try
{
const bool altPressed = GetKeyState(VK_MENU) < 0;
COORD cursorPosition{
GET_X_LPARAM(lParam),
GET_Y_LPARAM(lParam),
};

const auto fontSize = this->_actualFont.GetSize();
const auto fontSize = this->_actualFont.GetSize();

RETURN_HR_IF(E_NOT_VALID_STATE, fontSize.X == 0);
RETURN_HR_IF(E_NOT_VALID_STATE, fontSize.Y == 0);
RETURN_HR_IF(E_NOT_VALID_STATE, fontSize.X == 0);
RETURN_HR_IF(E_NOT_VALID_STATE, fontSize.Y == 0);

cursorPosition.X /= fontSize.X;
cursorPosition.Y /= fontSize.Y;
cursorPosition.X /= fontSize.X;
cursorPosition.Y /= fontSize.Y;

try
{
this->_terminal->SetSelectionAnchor(cursorPosition);
this->_terminal->SetBoxSelection(altPressed);

Expand All @@ -393,21 +400,21 @@ HRESULT HwndTerminal::_StartSelection(LPARAM lParam) noexcept

HRESULT HwndTerminal::_MoveSelection(LPARAM lParam) noexcept
{
COORD cursorPosition{
GET_X_LPARAM(lParam),
GET_Y_LPARAM(lParam),
};
try
{
COORD cursorPosition{
GET_X_LPARAM(lParam),
GET_Y_LPARAM(lParam),
};

const auto fontSize = this->_actualFont.GetSize();
const auto fontSize = this->_actualFont.GetSize();

RETURN_HR_IF(E_NOT_VALID_STATE, fontSize.X == 0);
RETURN_HR_IF(E_NOT_VALID_STATE, fontSize.Y == 0);
RETURN_HR_IF(E_NOT_VALID_STATE, fontSize.X == 0);
RETURN_HR_IF(E_NOT_VALID_STATE, fontSize.Y == 0);

cursorPosition.X /= fontSize.X;
cursorPosition.Y /= fontSize.Y;
cursorPosition.X /= fontSize.X;
cursorPosition.Y /= fontSize.Y;

try
{
this->_terminal->SetEndSelectionPosition(cursorPosition);
this->_renderer->TriggerSelection();
}
Expand Down Expand Up @@ -565,7 +572,7 @@ void _stdcall TerminalSetCursorVisible(void* terminal, const bool visible)
// Arguments:
// - rows - Rows of text data to copy
// - fAlsoCopyFormatting - true if the color and formatting should also be copied, false otherwise
HRESULT HwndTerminal::_CopyTextToSystemClipboard(const TextBuffer::TextAndColor& rows, bool const fAlsoCopyFormatting) noexcept
HRESULT HwndTerminal::_CopyTextToSystemClipboard(const TextBuffer::TextAndColor& rows, bool const fAlsoCopyFormatting)
{
std::wstring finalString;

Expand All @@ -581,7 +588,7 @@ HRESULT HwndTerminal::_CopyTextToSystemClipboard(const TextBuffer::TextAndColor&
wil::unique_hglobal globalHandle(GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cbNeeded));
RETURN_LAST_ERROR_IF_NULL(globalHandle.get());

PWSTR pwszClipboard = (PWSTR)GlobalLock(globalHandle.get());
PWSTR pwszClipboard = static_cast<PWSTR>(GlobalLock(globalHandle.get()));
RETURN_LAST_ERROR_IF_NULL(pwszClipboard);

// The pattern gets a bit strange here because there's no good wil built-in for global lock of this type.
Expand All @@ -594,7 +601,7 @@ HRESULT HwndTerminal::_CopyTextToSystemClipboard(const TextBuffer::TextAndColor&
RETURN_LAST_ERROR_IF(!OpenClipboard(_hwnd.get()));

{ // Clipboard Scope
auto clipboardCloser = wil::scope_exit([]() {
auto clipboardCloser = wil::scope_exit([]() noexcept {
LOG_LAST_ERROR_IF(!CloseClipboard());
});

Expand Down Expand Up @@ -628,15 +635,15 @@ HRESULT HwndTerminal::_CopyTextToSystemClipboard(const TextBuffer::TextAndColor&
// Arguments:
// - stringToCopy - The string to copy
// - lpszFormat - the name of the format
HRESULT HwndTerminal::_CopyToSystemClipboard(std::string stringToCopy, LPCWSTR lpszFormat) noexcept
HRESULT HwndTerminal::_CopyToSystemClipboard(std::string stringToCopy, LPCWSTR lpszFormat)
{
const size_t cbData = stringToCopy.size() + 1; // +1 for '\0'
if (cbData)
{
wil::unique_hglobal globalHandleData(GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, cbData));
RETURN_LAST_ERROR_IF_NULL(globalHandleData.get());

PSTR pszClipboardHTML = (PSTR)GlobalLock(globalHandleData.get());
PSTR pszClipboardHTML = static_cast<PSTR>(GlobalLock(globalHandleData.get()));
RETURN_LAST_ERROR_IF_NULL(pszClipboardHTML);

// The pattern gets a bit strange here because there's no good wil built-in for global lock of this type.
Expand All @@ -661,25 +668,20 @@ HRESULT HwndTerminal::_CopyToSystemClipboard(std::string stringToCopy, LPCWSTR l

void HwndTerminal::_PasteTextFromClipboard() noexcept
{
HANDLE ClipboardDataHandle;

// Clear any selection or scrolling that may be active.
_terminal->ClearSelection();

// Get paste data from clipboard
if (!OpenClipboard(_hwnd.get()))
{
return;
}

ClipboardDataHandle = GetClipboardData(CF_UNICODETEXT);
HANDLE ClipboardDataHandle = GetClipboardData(CF_UNICODETEXT);
if (ClipboardDataHandle == nullptr)
{
CloseClipboard();
return;
}

PWCHAR pwstr = (PWCHAR)GlobalLock(ClipboardDataHandle);
const PWCHAR pwstr = static_cast<const PWCHAR>(GlobalLock(ClipboardDataHandle));

_StringPaste(pwstr);

Expand Down
6 changes: 3 additions & 3 deletions src/cascadia/PublicTerminalCore/HwndTerminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ struct HwndTerminal : ::Microsoft::Console::Types::IControlAccessibilityInfo
friend void _stdcall TerminalSetCursorVisible(void* terminal, const bool visible);

void _UpdateFont(int newDpi);
void _WriteTextToConnection(std::wstring& text) noexcept;
HRESULT _CopyTextToSystemClipboard(const TextBuffer::TextAndColor& rows, bool const fAlsoCopyFormatting) noexcept;
HRESULT _CopyToSystemClipboard(std::string stringToCopy, LPCWSTR lpszFormat) noexcept;
void _WriteTextToConnection(const std::wstring& text) noexcept;
HRESULT _CopyTextToSystemClipboard(const TextBuffer::TextAndColor& rows, bool const fAlsoCopyFormatting);
HRESULT _CopyToSystemClipboard(std::string stringToCopy, LPCWSTR lpszFormat);
void _PasteTextFromClipboard() noexcept;
void _StringPaste(const wchar_t* const pData) noexcept;

Expand Down

0 comments on commit 70b9f05

Please sign in to comment.