From cbf05ab19171c3d092fa424e010fc2380ea8d45f Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 17 May 2021 10:47:39 +1000 Subject: Fix issues reported by Coverity and Visual C++ Analysis. Throw when (impossible) dynamic_cast failures occur as it isn't reasonable to recover. That removes 'noexcept' from some methods. Cast to avoid 'sub-expression overflow' warnings. Add default initializations and noexcept where safe. Move DropRenderTarget out of #if to avoid some preprocessor use. --- win32/PlatWin.cxx | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'win32/PlatWin.cxx') diff --git a/win32/PlatWin.cxx b/win32/PlatWin.cxx index 21a5180ac..8fb415675 100644 --- a/win32/PlatWin.cxx +++ b/win32/PlatWin.cxx @@ -408,7 +408,7 @@ public: } }; -constexpr int stackBufferLength = 1000; +constexpr int stackBufferLength = 400; class TextWide : public VarBuffer { public: int tlen; // Using int instead of size_t as most Win32 APIs take int. @@ -471,7 +471,7 @@ class SurfaceGDI : public Surface { void PenColour(ColourAlpha fore, XYPOSITION widthStroke) noexcept; void BrushColour(ColourAlpha back) noexcept; - void SetFont(const Font *font_) noexcept; + void SetFont(const Font *font_); void Clear() noexcept; public: @@ -663,9 +663,12 @@ void SurfaceGDI::BrushColour(ColourAlpha back) noexcept { brushOld = SelectBrush(hdc, brush); } -void SurfaceGDI::SetFont(const Font *font_) noexcept { +void SurfaceGDI::SetFont(const Font *font_) { const FontGDI *pfm = dynamic_cast(font_); PLATFORM_ASSERT(pfm); + if (!pfm) { + throw std::runtime_error("SurfaceGDI::SetFont: wrong Font type."); + } if (fontOld) { SelectFont(hdc, pfm->hfont); } else { @@ -990,7 +993,7 @@ void SurfaceGDI::DrawRGBAImage(PRectangle rc, int width, int height, const unsig const SIZE size { width, height }; DIBSection section(hdc, size); if (section) { - RGBAImage::BGRAFromRGBA(section.Bytes(), pixelsImage, width * height); + RGBAImage::BGRAFromRGBA(section.Bytes(), pixelsImage, static_cast(width) * height); AlphaBlend(hdc, static_cast(rc.left), static_cast(rc.top), static_cast(rc.Width()), static_cast(rc.Height()), section.DC(), 0, 0, width, height, mergeAlpha); @@ -1314,7 +1317,7 @@ class SurfaceD2D : public Surface { int logPixelsY = USER_DEFAULT_SCREEN_DPI; void Clear() noexcept; - void SetFont(const Font *font_) noexcept; + void SetFont(const Font *font_); HRESULT GetBitmap(ID2D1Bitmap **ppBitmap); public: @@ -1489,9 +1492,12 @@ void SurfaceD2D::D2DPenColourAlpha(ColourAlpha fore) noexcept { } } -void SurfaceD2D::SetFont(const Font *font_) noexcept { +void SurfaceD2D::SetFont(const Font *font_) { const FontDirectWrite *pfm = dynamic_cast(font_); PLATFORM_ASSERT(pfm); + if (!pfm) { + throw std::runtime_error("SurfaceD2D::SetFont: wrong Font type."); + } pTextFormat = pfm->pTextFormat; yAscent = pfm->yAscent; yDescent = pfm->yDescent; @@ -1657,6 +1663,9 @@ void SurfaceD2D::FillRectangleAligned(PRectangle rc, Fill fill) { void SurfaceD2D::FillRectangle(PRectangle rc, Surface &surfacePattern) { SurfaceD2D *psurfOther = dynamic_cast(&surfacePattern); PLATFORM_ASSERT(psurfOther); + if (!psurfOther) { + throw std::runtime_error("SurfaceD2D::FillRectangle: wrong Surface type."); + } ID2D1Bitmap *pBitmap = nullptr; HRESULT hr = psurfOther->GetBitmap(&pBitmap); if (SUCCEEDED(hr) && pBitmap) { @@ -2083,6 +2092,9 @@ void ScreenLineLayout::FillTextLayoutFormats(const IScreenLine *screenLine, IDWr const FontDirectWrite *pfm = dynamic_cast(screenLine->FontOfPosition(bytePosition)); + if (!pfm) { + throw std::runtime_error("FillTextLayoutFormats: wrong Font type."); + } const unsigned int fontFamilyNameSize = pfm->pTextFormat->GetFontFamilyNameLength(); std::wstring fontFamilyName(fontFamilyNameSize, 0); @@ -2141,7 +2153,7 @@ ScreenLineLayout::ScreenLineLayout(const IScreenLine *screenLine) { // Get textFormat const FontDirectWrite *pfm = dynamic_cast(screenLine->FontOfPosition(0)); - if (!pIDWriteFactory || !pfm->pTextFormat) { + if (!pIDWriteFactory || !pfm || !pfm->pTextFormat) { return; } @@ -2214,7 +2226,7 @@ size_t ScreenLineLayout::PositionFromX(XYPOSITION xDistance, bool charPosition) if (charPosition) { pos = isTrailingHit ? hitTestMetrics.textPosition : caretMetrics.textPosition; } else { - pos = isTrailingHit ? hitTestMetrics.textPosition + hitTestMetrics.length : caretMetrics.textPosition; + pos = isTrailingHit ? static_cast(hitTestMetrics.textPosition) + hitTestMetrics.length : caretMetrics.textPosition; } // Get the character position in original string -- cgit v1.2.3