From 8ea7401dc97203bb3097e33a2c4dd12211aabe09 Mon Sep 17 00:00:00 2001 From: Michael Niksa Date: Thu, 29 Aug 2019 13:19:01 -0700 Subject: [PATCH] C26472, no static_cast for arithmetic conversions. narrow or narrow_cast --- src/buffer/out/TextAttribute.cpp | 6 ++-- src/buffer/out/TextAttribute.hpp | 6 ++-- src/buffer/out/cursor.cpp | 2 +- src/buffer/out/textBuffer.cpp | 6 ++-- src/renderer/dx/CustomTextLayout.cpp | 8 +++--- src/renderer/dx/DxRenderer.cpp | 6 ++-- src/types/ScreenInfoUiaProviderBase.cpp | 8 +++--- src/types/UiaTextRangeBase.cpp | 38 ++++++++++++------------- src/types/WindowUiaProviderBase.cpp | 2 +- src/types/inc/utils.hpp | 2 +- src/types/utils.cpp | 6 ++-- 11 files changed, 45 insertions(+), 45 deletions(-) diff --git a/src/buffer/out/TextAttribute.cpp b/src/buffer/out/TextAttribute.cpp index 36d28b1605..b224c6c8ad 100644 --- a/src/buffer/out/TextAttribute.cpp +++ b/src/buffer/out/TextAttribute.cpp @@ -87,10 +87,10 @@ void TextAttribute::SetBackground(const COLORREF rgbBackground) void TextAttribute::SetFromLegacy(const WORD wLegacy) noexcept { - _wAttrLegacy = static_cast(wLegacy & META_ATTRS); + _wAttrLegacy = gsl::narrow_cast(wLegacy & META_ATTRS); WI_ClearAllFlags(_wAttrLegacy, COMMON_LVB_SBCSDBCS); - const BYTE fgIndex = static_cast(wLegacy & FG_ATTRS); - const BYTE bgIndex = static_cast(wLegacy & BG_ATTRS) >> 4; + const BYTE fgIndex = gsl::narrow_cast(wLegacy & FG_ATTRS); + const BYTE bgIndex = gsl::narrow_cast(wLegacy & BG_ATTRS) >> 4; _foreground = TextColor(fgIndex); _background = TextColor(bgIndex); } diff --git a/src/buffer/out/TextAttribute.hpp b/src/buffer/out/TextAttribute.hpp index f35b306fc6..eb402c61f3 100644 --- a/src/buffer/out/TextAttribute.hpp +++ b/src/buffer/out/TextAttribute.hpp @@ -39,9 +39,9 @@ public: } constexpr TextAttribute(const WORD wLegacyAttr) noexcept : - _wAttrLegacy{ static_cast(wLegacyAttr & META_ATTRS) }, - _foreground{ static_cast(wLegacyAttr & FG_ATTRS) }, - _background{ static_cast((wLegacyAttr & BG_ATTRS) >> 4) }, + _wAttrLegacy{ gsl::narrow_cast(wLegacyAttr & META_ATTRS) }, + _foreground{ gsl::narrow_cast(wLegacyAttr & FG_ATTRS) }, + _background{ gsl::narrow_cast((wLegacyAttr & BG_ATTRS) >> 4) }, _isBold{ false } { // If we're given lead/trailing byte information with the legacy color, strip it. diff --git a/src/buffer/out/cursor.cpp b/src/buffer/out/cursor.cpp index 946d0ecfe7..97ed5f1ea8 100644 --- a/src/buffer/out/cursor.cpp +++ b/src/buffer/out/cursor.cpp @@ -342,7 +342,7 @@ const COLORREF Cursor::GetColor() const void Cursor::SetColor(const unsigned int color) { - _color = static_cast(color); + _color = gsl::narrow_cast(color); } void Cursor::SetType(const CursorType type) diff --git a/src/buffer/out/textBuffer.cpp b/src/buffer/out/textBuffer.cpp index a5f79c0b4c..c563fc1e56 100644 --- a/src/buffer/out/textBuffer.cpp +++ b/src/buffer/out/textBuffer.cpp @@ -62,7 +62,7 @@ void TextBuffer::CopyProperties(const TextBuffer& OtherBuffer) // - Total number of rows in the buffer UINT TextBuffer::TotalRowCount() const { - return static_cast(_storage.size()); + return gsl::narrow(_storage.size()); } // Routine Description: @@ -586,7 +586,7 @@ COORD TextBuffer::GetLastNonSpaceCharacter(const Microsoft::Console::Types::View const ROW* pCurrRow = &GetRowByOffset(coordEndOfText.Y); // The X position of the end of the valid text is the Right draw boundary (which is one beyond the final valid character) - coordEndOfText.X = static_cast(pCurrRow->GetCharRow().MeasureRight()) - 1; + coordEndOfText.X = gsl::narrow(pCurrRow->GetCharRow().MeasureRight()) - 1; // If the X coordinate turns out to be -1, the row was empty, we need to search backwards for the real end of text. const auto viewportTop = viewport.Top(); @@ -597,7 +597,7 @@ COORD TextBuffer::GetLastNonSpaceCharacter(const Microsoft::Console::Types::View pCurrRow = &GetRowByOffset(coordEndOfText.Y); // We need to back up to the previous row if this line is empty, AND there are more rows - coordEndOfText.X = static_cast(pCurrRow->GetCharRow().MeasureRight()) - 1; + coordEndOfText.X = gsl::narrow(pCurrRow->GetCharRow().MeasureRight()) - 1; fDoBackUp = (coordEndOfText.X < 0 && coordEndOfText.Y > viewportTop); } diff --git a/src/renderer/dx/CustomTextLayout.cpp b/src/renderer/dx/CustomTextLayout.cpp index 027450a07d..230d1ba56d 100644 --- a/src/renderer/dx/CustomTextLayout.cpp +++ b/src/renderer/dx/CustomTextLayout.cpp @@ -232,7 +232,7 @@ CustomTextLayout::CustomTextLayout(IDWriteFactory1* const factory, Run& run = _runs.at(runIndex); const UINT32 textStart = run.textStart; const UINT32 textLength = run.textLength; - UINT32 maxGlyphCount = static_cast(_glyphIndices.size() - glyphStart); + UINT32 maxGlyphCount = gsl::narrow(_glyphIndices.size() - glyphStart); UINT32 actualGlyphCount = 0; run.glyphStart = glyphStart; @@ -945,7 +945,7 @@ void CustomTextLayout::_SetCurrentRun(const UINT32 textPosition) return; } - _runIndex = static_cast( + _runIndex = gsl::narrow( std::find(_runs.begin(), _runs.end(), textPosition) - _runs.begin()); } @@ -983,7 +983,7 @@ void CustomTextLayout::_SplitCurrentRun(const UINT32 splitPosition) backHalf.textStart += splitPoint; backHalf.textLength -= splitPoint; frontHalf.textLength = splitPoint; - frontHalf.nextRunIndex = static_cast(totalRuns); - _runIndex = static_cast(totalRuns); + frontHalf.nextRunIndex = gsl::narrow(totalRuns); + _runIndex = gsl::narrow(totalRuns); } #pragma endregion diff --git a/src/renderer/dx/DxRenderer.cpp b/src/renderer/dx/DxRenderer.cpp index 49c4794662..cecdb78e79 100644 --- a/src/renderer/dx/DxRenderer.cpp +++ b/src/renderer/dx/DxRenderer.cpp @@ -375,7 +375,7 @@ void DxEngine::_ReleaseDeviceResources() noexcept _Out_ IDWriteTextLayout** ppTextLayout) noexcept { return _dwriteFactory->CreateTextLayout(string, - static_cast(stringLength), + gsl::narrow(stringLength), _dwriteTextFormat.Get(), gsl::narrow(_displaySizePixels.cx), _glyphCell.cy != 0 ? _glyphCell.cy : gsl::narrow( _displaySizePixels.cy), @@ -1219,8 +1219,8 @@ enum class CursorPaintType [[nodiscard]] Viewport DxEngine::GetViewportInCharacters(const Viewport& viewInPixels) noexcept { - const short widthInChars = static_cast(viewInPixels.Width() / _glyphCell.cx); - const short heightInChars = static_cast(viewInPixels.Height() / _glyphCell.cy); + const short widthInChars = gsl::narrow(viewInPixels.Width() / _glyphCell.cx); + const short heightInChars = gsl::narrow(viewInPixels.Height() / _glyphCell.cy); return Viewport::FromDimensions(viewInPixels.Origin(), { widthInChars, heightInChars }); } diff --git a/src/types/ScreenInfoUiaProviderBase.cpp b/src/types/ScreenInfoUiaProviderBase.cpp index 531e5fd9b2..c68dd7d175 100644 --- a/src/types/ScreenInfoUiaProviderBase.cpp +++ b/src/types/ScreenInfoUiaProviderBase.cpp @@ -379,14 +379,14 @@ IFACEMETHODIMP ScreenInfoUiaProviderBase::GetSelection(_Outptr_result_maybenull_ //apiMsg.SelectionRowCount = static_cast(ranges.size()); // make a safe array - *ppRetVal = SafeArrayCreateVector(VT_UNKNOWN, 0, static_cast(ranges.size())); + *ppRetVal = SafeArrayCreateVector(VT_UNKNOWN, 0, gsl::narrow(ranges.size())); if (*ppRetVal == nullptr) { return E_OUTOFMEMORY; } // fill the safe array - for (LONG i = 0; i < static_cast(ranges.size()); ++i) + for (LONG i = 0; i < gsl::narrow(ranges.size()); ++i) { hr = SafeArrayPutElement(*ppRetVal, &i, reinterpret_cast(ranges.at(i))); if (FAILED(hr)) @@ -428,7 +428,7 @@ IFACEMETHODIMP ScreenInfoUiaProviderBase::GetVisibleRanges(_Outptr_result_mayben // make a safe array const size_t rowCount = viewport.Height(); - *ppRetVal = SafeArrayCreateVector(VT_UNKNOWN, 0, static_cast(rowCount)); + *ppRetVal = SafeArrayCreateVector(VT_UNKNOWN, 0, gsl::narrow(rowCount)); if (*ppRetVal == nullptr) { return E_OUTOFMEMORY; @@ -473,7 +473,7 @@ IFACEMETHODIMP ScreenInfoUiaProviderBase::GetVisibleRanges(_Outptr_result_mayben return hr; } - LONG currentIndex = static_cast(i); + LONG currentIndex = gsl::narrow(i); hr = SafeArrayPutElement(*ppRetVal, ¤tIndex, reinterpret_cast(range)); if (FAILED(hr)) { diff --git a/src/types/UiaTextRangeBase.cpp b/src/types/UiaTextRangeBase.cpp index 5db8c1329f..3deb97ad49 100644 --- a/src/types/UiaTextRangeBase.cpp +++ b/src/types/UiaTextRangeBase.cpp @@ -479,13 +479,13 @@ IFACEMETHODIMP UiaTextRangeBase::GetBoundingRectangles(_Outptr_result_maybenull_ } // convert to a safearray - *ppRetVal = SafeArrayCreateVector(VT_R8, 0, static_cast(coords.size())); + *ppRetVal = SafeArrayCreateVector(VT_R8, 0, gsl::narrow(coords.size())); if (*ppRetVal == nullptr) { return E_OUTOFMEMORY; } HRESULT hr; - for (LONG i = 0; i < static_cast(coords.size()); ++i) + for (LONG i = 0; i < gsl::narrow(coords.size()); ++i) { hr = SafeArrayPutElement(*ppRetVal, &i, &coords.at(i)); if (FAILED(hr)) @@ -880,11 +880,11 @@ IFACEMETHODIMP UiaTextRangeBase::Select() COORD coordStart; COORD coordEnd; - coordStart.X = static_cast(_endpointToColumn(_pData, _start)); - coordStart.Y = static_cast(_endpointToScreenInfoRow(_pData, _start)); + coordStart.X = gsl::narrow(_endpointToColumn(_pData, _start)); + coordStart.Y = gsl::narrow(_endpointToScreenInfoRow(_pData, _start)); - coordEnd.X = static_cast(_endpointToColumn(_pData, _end)); - coordEnd.Y = static_cast(_endpointToScreenInfoRow(_pData, _end)); + coordEnd.X = gsl::narrow(_endpointToColumn(_pData, _end)); + coordEnd.Y = gsl::narrow(_endpointToScreenInfoRow(_pData, _end)); _pData->SelectNewRegion(coordStart, coordEnd); } @@ -948,15 +948,15 @@ IFACEMETHODIMP UiaTextRangeBase::ScrollIntoView(_In_ BOOL alignToTop) if (startScreenInfoRow + viewportHeight <= bottomRow) { // we can align to the top - newViewport.Top = static_cast(startScreenInfoRow); - newViewport.Bottom = static_cast(startScreenInfoRow + viewportHeight - 1); + newViewport.Top = gsl::narrow(startScreenInfoRow); + newViewport.Bottom = gsl::narrow(startScreenInfoRow + viewportHeight - 1); } else { // we can align to the top so we'll just move the viewport // to the bottom of the screen buffer - newViewport.Bottom = static_cast(bottomRow); - newViewport.Top = static_cast(bottomRow - viewportHeight + 1); + newViewport.Bottom = gsl::narrow(bottomRow); + newViewport.Top = gsl::narrow(bottomRow - viewportHeight + 1); } } else @@ -966,20 +966,20 @@ IFACEMETHODIMP UiaTextRangeBase::ScrollIntoView(_In_ BOOL alignToTop) if (endScreenInfoRow >= viewportHeight) { // we can align to bottom - newViewport.Bottom = static_cast(endScreenInfoRow); - newViewport.Top = static_cast(endScreenInfoRow - viewportHeight + 1); + newViewport.Bottom = gsl::narrow(endScreenInfoRow); + newViewport.Top = gsl::narrow(endScreenInfoRow - viewportHeight + 1); } else { // we can't align to bottom so we'll move the viewport to // the top of the screen buffer - newViewport.Top = static_cast(topRow); - newViewport.Bottom = static_cast(topRow + viewportHeight - 1); + newViewport.Top = gsl::narrow(topRow); + newViewport.Bottom = gsl::narrow(topRow + viewportHeight - 1); } } - FAIL_FAST_IF(!(newViewport.Top >= static_cast(topRow))); - FAIL_FAST_IF(!(newViewport.Bottom <= static_cast(bottomRow))); + FAIL_FAST_IF(!(newViewport.Top >= gsl::narrow(topRow))); + FAIL_FAST_IF(!(newViewport.Bottom <= gsl::narrow(bottomRow))); FAIL_FAST_IF(!(_getViewportHeight(oldViewport) == _getViewportHeight(newViewport))); try @@ -1209,7 +1209,7 @@ const bool UiaTextRangeBase::_isScreenInfoRowInViewport(const ScreenInfoRow row, { const ViewportRow viewportRow = _screenInfoRowToViewportRow(row, viewport); return viewportRow >= 0 && - viewportRow < static_cast(_getViewportHeight(viewport)); + viewportRow < gsl::narrow(_getViewportHeight(viewport)); } // Routine Description: @@ -1513,7 +1513,7 @@ std::pair UiaTextRangeBase::_moveByCharacterBackward(IUiaDat // get the right cell for the next row const ROW& row = pData->GetTextBuffer().GetRowByOffset(currentScreenInfoRow); const size_t right = row.GetCharRow().MeasureRight(); - currentColumn = static_cast((right == 0) ? 0 : right - 1); + currentColumn = gsl::narrow((right == 0) ? 0 : right - 1); } else { @@ -1768,7 +1768,7 @@ UiaTextRangeBase::_moveEndpointByUnitCharacterBackward(IUiaData* pData, // get the right cell for the next row const ROW& row = pData->GetTextBuffer().GetRowByOffset(currentScreenInfoRow); const size_t right = row.GetCharRow().MeasureRight(); - currentColumn = static_cast((right == 0) ? 0 : right - 1); + currentColumn = gsl::narrow((right == 0) ? 0 : right - 1); } else { diff --git a/src/types/WindowUiaProviderBase.cpp b/src/types/WindowUiaProviderBase.cpp index e2072046b4..7ff719081b 100644 --- a/src/types/WindowUiaProviderBase.cpp +++ b/src/types/WindowUiaProviderBase.cpp @@ -155,7 +155,7 @@ IFACEMETHODIMP WindowUiaProviderBase::get_HostRawElementProvider(_COM_Outptr_res } catch (...) { - return static_cast(UIA_E_ELEMENTNOTAVAILABLE); + return gsl::narrow_cast(UIA_E_ELEMENTNOTAVAILABLE); } } #pragma endregion diff --git a/src/types/inc/utils.hpp b/src/types/inc/utils.hpp index 8d3cd50cc8..646dfe10ed 100644 --- a/src/types/inc/utils.hpp +++ b/src/types/inc/utils.hpp @@ -46,7 +46,7 @@ namespace Microsoft::Console::Utils constexpr unsigned long EndianSwap(unsigned long value) { - return static_cast(EndianSwap(static_cast(value))); + return gsl::narrow_cast(EndianSwap(gsl::narrow_cast(value))); } constexpr GUID EndianSwap(GUID value) diff --git a/src/types/utils.cpp b/src/types/utils.cpp index adfeccf97a..710dd74df3 100644 --- a/src/types/utils.cpp +++ b/src/types/utils.cpp @@ -98,9 +98,9 @@ COLORREF Utils::ColorFromHexString(const std::string str) std::string gStr{ &str.at(3), 2 }; std::string bStr{ &str.at(5), 2 }; - const BYTE r = static_cast(std::stoul(rStr, nullptr, 16)); - const BYTE g = static_cast(std::stoul(gStr, nullptr, 16)); - const BYTE b = static_cast(std::stoul(bStr, nullptr, 16)); + const BYTE r = gsl::narrow_cast(std::stoul(rStr, nullptr, 16)); + const BYTE g = gsl::narrow_cast(std::stoul(gStr, nullptr, 16)); + const BYTE b = gsl::narrow_cast(std::stoul(bStr, nullptr, 16)); return RGB(r, g, b); }