From 92f0aeaac8736e5e44b301fa90c07a5614664be1 Mon Sep 17 00:00:00 2001 From: Caolán McNamara Date: Mon, 28 Feb 2022 09:45:55 +0000 Subject: check if cast is to the right type LIBREOFFICE-311XVJ95 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130670 Tested-by: Jenkins Reviewed-by: Michael Stahl (cherry picked from commit b44bd9ef8e2efdb66558917200e1f179b9db1c58) Change-Id: I159f516daafad3e4088677fe2c8c6f5423b3e264 (cherry picked from commit f9350f9a8404fd9eb5b6963022f0069e89ddd061) --- drawinglayer/source/tools/emfphelperdata.cxx | 31 ++++++++++++++++------------ 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/drawinglayer/source/tools/emfphelperdata.cxx b/drawinglayer/source/tools/emfphelperdata.cxx index 6e4859f0bad7..0a26aff9ef54 100644 --- a/drawinglayer/source/tools/emfphelperdata.cxx +++ b/drawinglayer/source/tools/emfphelperdata.cxx @@ -350,7 +350,7 @@ namespace emfplushelper } else // we use a pen { - const EMFPPen* pen = static_cast(maEMFPObjects[brushIndexOrColor & 0xff].get()); + const EMFPPen* pen = dynamic_cast(maEMFPObjects[brushIndexOrColor & 0xff].get()); if (pen) { color = pen->GetColor(); @@ -535,7 +535,7 @@ namespace emfplushelper } else // use Brush { - EMFPBrush* brush = static_cast( maEMFPObjects[brushIndexOrColor & 0xff].get() ); + EMFPBrush* brush = dynamic_cast( maEMFPObjects[brushIndexOrColor & 0xff].get() ); SAL_INFO("drawinglayer", "EMF+\t Fill polygon, brush slot: " << brushIndexOrColor << " (brush type: " << (brush ? brush->GetType() : -1) << ")"); // give up in case something wrong happened @@ -1033,7 +1033,11 @@ namespace emfplushelper rMS.ReadUInt32(brushIndexOrColor); SAL_INFO("drawinglayer", "EMF+ FillRegion slot: " << index); - EMFPPlusFillPolygon(static_cast(maEMFPObjects[flags & 0xff].get())->regionPolyPolygon, flags & 0x8000, brushIndexOrColor); + EMFPRegion* region = dynamic_cast(maEMFPObjects[flags & 0xff].get()); + if (region) + EMFPPlusFillPolygon(region->regionPolyPolygon, flags & 0x8000, brushIndexOrColor); + else + SAL_WARN("drawinglayer.emf", "EMF+\tEmfPlusRecordTypeFillRegion missing region"); } break; case EmfPlusRecordTypeDrawEllipse: @@ -1210,9 +1214,10 @@ namespace emfplushelper SAL_INFO("drawinglayer", "EMF+\tTODO: use image attributes"); // For DrawImage and DrawImagePoints, source unit of measurement type must be 1 pixel - if (sourceUnit == UnitTypePixel && maEMFPObjects[flags & 0xff].get()) + if (EMFPImage* image = sourceUnit == UnitTypePixel ? + dynamic_cast(maEMFPObjects[flags & 0xff].get()) : + nullptr) { - EMFPImage& image = *static_cast(maEMFPObjects[flags & 0xff].get()); float sx, sy, sw, sh; ReadRectangle(rMS, sx, sy, sw, sh); ::tools::Rectangle aSource(Point(sx, sy), Size(sw, sh)); @@ -1262,9 +1267,9 @@ namespace emfplushelper aDstPoint.getX(), aDstPoint.getY()); - if (image.type == ImageDataTypeBitmap) + if (image->type == ImageDataTypeBitmap) { - BitmapEx aBmp(image.graphic.GetBitmapEx()); + BitmapEx aBmp(image->graphic.GetBitmapEx()); aBmp.Crop(aSource); Size aSize(aBmp.GetSizePixel()); SAL_INFO("drawinglayer", "EMF+\t bitmap size: " << aSize.Width() << "x" << aSize.Height()); @@ -1278,9 +1283,9 @@ namespace emfplushelper SAL_INFO("drawinglayer", "EMF+\t warning: empty bitmap"); } } - else if (image.type == ImageDataTypeMetafile) + else if (image->type == ImageDataTypeMetafile) { - GDIMetaFile aGDI(image.graphic.GetGDIMetaFile()); + GDIMetaFile aGDI(image->graphic.GetGDIMetaFile()); aGDI.Clip(aSource); mrTargetHolders.Current().append( o3tl::make_unique(aTransformMatrix, aGDI)); @@ -1314,7 +1319,7 @@ namespace emfplushelper // get the stringFormat from the Object table ( this is OPTIONAL and may be nullptr ) const EMFPStringFormat *stringFormat = dynamic_cast(maEMFPObjects[formatId & 0xff].get()); // get the font from the flags - const EMFPFont *font = static_cast< EMFPFont* >( maEMFPObjects[flags & 0xff].get() ); + const EMFPFont *font = dynamic_cast(maEMFPObjects[flags & 0xff].get()); if (!font) { break; @@ -1680,7 +1685,7 @@ namespace emfplushelper SAL_INFO("drawinglayer", "EMF+ SetClipPath combine mode: " << combineMode); SAL_INFO("drawinglayer", "EMF+\tpath in slot: " << (flags & 0xff)); - EMFPPath *path = static_cast(maEMFPObjects[flags & 0xff].get()); + EMFPPath *path = dynamic_cast(maEMFPObjects[flags & 0xff].get()); if (!path) { break; @@ -1697,7 +1702,7 @@ namespace emfplushelper int combineMode = (flags >> 8) & 0xf; SAL_INFO("drawinglayer", "EMF+ SetClipRegion"); SAL_INFO("drawinglayer", "EMF+\tregion in slot: " << (flags & 0xff) << " combine mode: " << combineMode); - EMFPRegion *region = static_cast(maEMFPObjects[flags & 0xff].get()); + EMFPRegion *region = dynamic_cast(maEMFPObjects[flags & 0xff].get()); if (!region) { break; @@ -1765,7 +1770,7 @@ namespace emfplushelper } // get the font from the flags - EMFPFont *font = static_cast< EMFPFont* >( maEMFPObjects[flags & 0xff].get() ); + EMFPFont *font = dynamic_cast(maEMFPObjects[flags & 0xff].get()); if (!font) { break; -- cgit v1.2.3