aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNeil <nyamatongwe@gmail.com>2021-05-17 10:47:39 +1000
committerNeil <nyamatongwe@gmail.com>2021-05-17 10:47:39 +1000
commitcbf05ab19171c3d092fa424e010fc2380ea8d45f (patch)
tree08c8fcd39a80fa48866af1697843a6e8ed87a871
parent90942f40527c251720416c6e754ad017e8a65f04 (diff)
downloadscintilla-mirror-cbf05ab19171c3d092fa424e010fc2380ea8d45f.tar.gz
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.
-rw-r--r--win32/PlatWin.cxx28
-rw-r--r--win32/ScintillaWin.cxx24
2 files changed, 31 insertions, 21 deletions
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<wchar_t, stackBufferLength> {
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<const FontGDI *>(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<size_t>(width) * height);
AlphaBlend(hdc, static_cast<int>(rc.left), static_cast<int>(rc.top),
static_cast<int>(rc.Width()), static_cast<int>(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<const FontDirectWrite *>(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<SurfaceD2D *>(&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<const FontDirectWrite *>(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<const FontDirectWrite *>(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<size_t>(hitTestMetrics.textPosition) + hitTestMetrics.length : caretMetrics.textPosition;
}
// Get the character position in original string
diff --git a/win32/ScintillaWin.cxx b/win32/ScintillaWin.cxx
index 4e6fc9b0d..810682df4 100644
--- a/win32/ScintillaWin.cxx
+++ b/win32/ScintillaWin.cxx
@@ -363,8 +363,8 @@ class ScintillaWin :
void Finalise() override;
#if defined(USE_D2D)
void EnsureRenderTarget(HDC hdc);
- void DropRenderTarget();
#endif
+ void DropRenderTarget() noexcept;
HWND MainHWND() const noexcept;
static sptr_t DirectFunction(
@@ -394,7 +394,7 @@ class ScintillaWin :
sptr_t HandleCompositionWindowed(uptr_t wParam, sptr_t lParam);
sptr_t HandleCompositionInline(uptr_t wParam, sptr_t lParam);
static bool KoreanIME() noexcept;
- void MoveImeCarets(Sci::Position offset);
+ void MoveImeCarets(Sci::Position offset) noexcept;
void DrawImeIndicator(int indicator, Sci::Position len);
void SetCandidateWindowPos();
void SelectionToHangul();
@@ -602,9 +602,7 @@ void ScintillaWin::Finalise() {
FineTickerCancel(tr);
}
SetIdle(false);
-#if defined(USE_D2D)
DropRenderTarget();
-#endif
::RevokeDragDrop(MainHWND());
if (SUCCEEDED(hrOle)) {
::OleUninitialize();
@@ -627,7 +625,7 @@ void ScintillaWin::EnsureRenderTarget(HDC hdc) {
// Create a Direct2D render target.
#if 1
- D2D1_RENDER_TARGET_PROPERTIES drtp;
+ D2D1_RENDER_TARGET_PROPERTIES drtp {};
drtp.type = D2D1_RENDER_TARGET_TYPE_DEFAULT;
drtp.pixelFormat.format = DXGI_FORMAT_UNKNOWN;
drtp.pixelFormat.alphaMode = D2D1_ALPHA_MODE_UNKNOWN;
@@ -651,7 +649,7 @@ void ScintillaWin::EnsureRenderTarget(HDC hdc) {
}
} else {
- D2D1_HWND_RENDER_TARGET_PROPERTIES dhrtp;
+ D2D1_HWND_RENDER_TARGET_PROPERTIES dhrtp {};
dhrtp.hwnd = hw;
dhrtp.pixelSize = size;
dhrtp.presentOptions = (technology == SC_TECHNOLOGY_DIRECTWRITERETAIN) ?
@@ -690,12 +688,14 @@ void ScintillaWin::EnsureRenderTarget(HDC hdc) {
}
}
}
+#endif
-void ScintillaWin::DropRenderTarget() {
- ReleaseUnknown(pRenderTarget);
-}
+void ScintillaWin::DropRenderTarget() noexcept {
+#if defined(USE_D2D)
+ ReleaseUnknown(pRenderTarget);
#endif
+}
HWND ScintillaWin::MainHWND() const noexcept {
return HwndFromWindow(wMain);
@@ -991,7 +991,7 @@ sptr_t ScintillaWin::HandleCompositionWindowed(uptr_t wParam, sptr_t lParam) {
// Set new position after converted
const Point pos = PointMainCaret();
- COMPOSITIONFORM CompForm;
+ COMPOSITIONFORM CompForm {};
CompForm.dwStyle = CFS_POINT;
CompForm.ptCurrentPos = POINTFromPoint(pos);
::ImmSetCompositionWindow(imc.hIMC, &CompForm);
@@ -1006,7 +1006,7 @@ bool ScintillaWin::KoreanIME() noexcept {
return codePage == 949 || codePage == 1361;
}
-void ScintillaWin::MoveImeCarets(Sci::Position offset) {
+void ScintillaWin::MoveImeCarets(Sci::Position offset) noexcept {
// Move carets relatively by bytes.
for (size_t r=0; r<sel.Count(); r++) {
const Sci::Position positionInsert = sel.Range(r).Start().Position();
@@ -1835,9 +1835,7 @@ sptr_t ScintillaWin::SciMessage(unsigned int iMessage, uptr_t wParam, sptr_t lPa
} else {
bidirectional = EditModel::Bidirectional::bidiDisabled;
}
-#if defined(USE_D2D)
DropRenderTarget();
-#endif
technology = technologyNew;
// Invalidate all cached information including layout.
DropGraphics();