From e9bc970c288a8892bfaf3f56b0ac5f5bd6e4d079 Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 17 Jul 2020 11:01:14 +0200 Subject: [PATCH] PVS: V522 There might be dereferencing of a potential null pointer --- src/Mod/Fem/App/FemMesh.cpp | 6 +- src/Mod/Fem/App/FemMesh.h | 2 +- src/Mod/Fem/Gui/ViewProviderFemMesh.cpp | 4 +- src/Mod/TechDraw/App/DrawWeldSymbol.cpp | 2 +- src/Mod/TechDraw/App/ShapeExtractor.cpp | 5 +- src/Mod/TechDraw/Gui/CommandAnnotate.cpp | 2 +- src/Mod/TechDraw/Gui/CommandCreateDims.cpp | 2 +- src/Mod/TechDraw/Gui/DrawGuiUtil.cpp | 106 +++++++++------------ src/Mod/TechDraw/Gui/Grabber3d.cpp | 8 +- src/Mod/TechDraw/Gui/QGIViewBalloon.cpp | 10 +- src/Mod/TechDraw/Gui/QGVPage.cpp | 2 +- src/Mod/TechDraw/Gui/TaskCenterLine.cpp | 2 +- src/Mod/TechDraw/Gui/TaskLineDecor.cpp | 2 +- src/Mod/TechDraw/Gui/TaskRichAnno.cpp | 2 +- src/Mod/TechDraw/Gui/ViewProviderPage.cpp | 10 +- 15 files changed, 78 insertions(+), 87 deletions(-) diff --git a/src/Mod/Fem/App/FemMesh.cpp b/src/Mod/Fem/App/FemMesh.cpp index 1cba782789..6fbed27681 100644 --- a/src/Mod/Fem/App/FemMesh.cpp +++ b/src/Mod/Fem/App/FemMesh.cpp @@ -2087,14 +2087,16 @@ int FemMesh::addGroup(const std::string TypeString, const std::string Name, cons return aId; } -void FemMesh::addGroupElements(const int GroupId, const std::set ElementIds) +void FemMesh::addGroupElements(int GroupId, const std::set& ElementIds) { SMESH_Group* group = this->getSMesh()->GetGroup(GroupId); if (!group) { throw std::runtime_error("AddGroupElements: No group for given id."); } SMESHDS_Group* groupDS = dynamic_cast(group->GetGroupDS()); - // TODO: is this dynamic_cast OK? + if (!groupDS) { + throw std::runtime_error("addGroupElements: Failed to add group elements."); + } // Traverse the full mesh and add elements to group if id is in set 'ids' // and if group type is compatible with element diff --git a/src/Mod/Fem/App/FemMesh.h b/src/Mod/Fem/App/FemMesh.h index 63770e6b81..a46c6b7777 100644 --- a/src/Mod/Fem/App/FemMesh.h +++ b/src/Mod/Fem/App/FemMesh.h @@ -137,7 +137,7 @@ public: /// Adds group to mesh int addGroup(const std::string, const std::string, const int=-1); /// Adds elements to group (int due to int used by raw SMESH functions) - void addGroupElements(int, std::set); + void addGroupElements(int, const std::set&); /// Remove group (Name due to similarity to SMESH basis functions) bool removeGroup(int); //@} diff --git a/src/Mod/Fem/Gui/ViewProviderFemMesh.cpp b/src/Mod/Fem/Gui/ViewProviderFemMesh.cpp index e828508797..daa4a95f63 100644 --- a/src/Mod/Fem/Gui/ViewProviderFemMesh.cpp +++ b/src/Mod/Fem/Gui/ViewProviderFemMesh.cpp @@ -392,7 +392,9 @@ void ViewProviderFemMesh::onChanged(const App::Property* prop) else if (prop == &ShowInner ) { // recalc mesh with new settings ViewProviderFEMMeshBuilder builder; - builder.createMesh(&(dynamic_cast(this->pcObject)->FemMesh), pcCoords, pcFaces, pcLines, vFaceElementIdx, vNodeElementIdx, onlyEdges, ShowInner.getValue(), MaxFacesShowInner.getValue()); + builder.createMesh(&(static_cast(this->pcObject)->FemMesh), + pcCoords, pcFaces, pcLines, vFaceElementIdx, vNodeElementIdx, + onlyEdges, ShowInner.getValue(), MaxFacesShowInner.getValue()); } else if (prop == &LineWidth) { pcDrawStyle->lineWidth = LineWidth.getValue(); diff --git a/src/Mod/TechDraw/App/DrawWeldSymbol.cpp b/src/Mod/TechDraw/App/DrawWeldSymbol.cpp index 0279868ea6..2484105111 100644 --- a/src/Mod/TechDraw/App/DrawWeldSymbol.cpp +++ b/src/Mod/TechDraw/App/DrawWeldSymbol.cpp @@ -100,8 +100,8 @@ void DrawWeldSymbol::onSettingDocument() DrawTileWeld* tile2 = dynamic_cast(tile2Obj); if (tile2 != nullptr) { tile2->TileParent.setValue(this); + tile2->TileRow.setValue(-1); //other side is row -1 } - tile2->TileRow.setValue(-1); //other side is row -1 DrawView::onSettingDocument(); } diff --git a/src/Mod/TechDraw/App/ShapeExtractor.cpp b/src/Mod/TechDraw/App/ShapeExtractor.cpp index febe6fcd06..133d984a80 100644 --- a/src/Mod/TechDraw/App/ShapeExtractor.cpp +++ b/src/Mod/TechDraw/App/ShapeExtractor.cpp @@ -401,10 +401,9 @@ bool ShapeExtractor::isDraftPoint(App::DocumentObject* obj) // Base::Console().Message("SE::isDraftPoint()\n"); bool result = false; //if the docObj doesn't have a Proxy property, it definitely isn't a Draft point - App::Property* proxy = obj->getPropertyByName("Proxy"); + App::PropertyPythonObject* proxy = dynamic_cast(obj->getPropertyByName("Proxy")); if (proxy != nullptr) { - App::PropertyPythonObject* proxyPy = dynamic_cast(proxy); - std::string pp = proxyPy->toString(); + std::string pp = proxy->toString(); // Base::Console().Message("SE::isDraftPoint - pp: %s\n", pp.c_str()); if (pp.find("Point") != std::string::npos) { result = true; diff --git a/src/Mod/TechDraw/Gui/CommandAnnotate.cpp b/src/Mod/TechDraw/Gui/CommandAnnotate.cpp index d36a178fc4..6f32a3a717 100644 --- a/src/Mod/TechDraw/Gui/CommandAnnotate.cpp +++ b/src/Mod/TechDraw/Gui/CommandAnnotate.cpp @@ -1206,7 +1206,7 @@ void CmdTechDrawShowAll::activated(int iMsg) Gui::ViewProvider* vp = QGIView::getViewProvider(baseFeat); auto partVP = dynamic_cast(vp); - if ( vp != nullptr ) { + if ( partVP != nullptr ) { bool state = partVP->ShowAllEdges.getValue(); state = !state; partVP->ShowAllEdges.setValue(state); diff --git a/src/Mod/TechDraw/Gui/CommandCreateDims.cpp b/src/Mod/TechDraw/Gui/CommandCreateDims.cpp index 5d3ffef371..8caaddc7a9 100644 --- a/src/Mod/TechDraw/Gui/CommandCreateDims.cpp +++ b/src/Mod/TechDraw/Gui/CommandCreateDims.cpp @@ -1380,7 +1380,7 @@ void CmdTechDrawLandmarkDimension::activated(int iMsg) return; } - TechDraw::DrawViewPart* dvp = dynamic_cast(views.front()); + TechDraw::DrawViewPart* dvp = static_cast(views.front()); std::vector refs2d; diff --git a/src/Mod/TechDraw/Gui/DrawGuiUtil.cpp b/src/Mod/TechDraw/Gui/DrawGuiUtil.cpp index 75b76481ae..1fa0ce681e 100644 --- a/src/Mod/TechDraw/Gui/DrawGuiUtil.cpp +++ b/src/Mod/TechDraw/Gui/DrawGuiUtil.cpp @@ -149,36 +149,30 @@ TechDraw::DrawPage* DrawGuiUtil::findPage(Gui::Command* cmd) bool DrawGuiUtil::isDraftObject(App::DocumentObject* obj) { bool result = false; - App::Property* proxy = obj->getPropertyByName("Proxy"); + App::PropertyPythonObject* proxy = dynamic_cast(obj->getPropertyByName("Proxy")); if (proxy != nullptr) { //if no proxy, can not be Draft obj //if has proxy, might be Draft obj - App::PropertyPythonObject* proxyPy = dynamic_cast(proxy); std::stringstream ss; - if (proxyPy != nullptr) { - Py::Object proxyObj = proxyPy->getValue(); - std::stringstream ss; - if (proxyPy != nullptr) { - Base::PyGILStateLocker lock; - try { - if (proxyObj.hasAttr("__module__")) { - Py::String mod(proxyObj.getAttr("__module__")); - ss << (std::string)mod; - if (ss.str().find("Draft") != std::string::npos) { - result = true; - } else if (ss.str().find("draft") != std::string::npos) { - result = true; - } - } - } - catch (Py::Exception&) { - Base::PyException e; // extract the Python error text - e.ReportException(); - result = false; + Py::Object proxyObj = proxy->getValue(); + Base::PyGILStateLocker lock; + try { + if (proxyObj.hasAttr("__module__")) { + Py::String mod(proxyObj.getAttr("__module__")); + ss << (std::string)mod; + if (ss.str().find("Draft") != std::string::npos) { + result = true; + } else if (ss.str().find("draft") != std::string::npos) { + result = true; } } } + catch (Py::Exception&) { + Base::PyException e; // extract the Python error text + e.ReportException(); + result = false; + } } return result; } @@ -186,31 +180,28 @@ bool DrawGuiUtil::isDraftObject(App::DocumentObject* obj) bool DrawGuiUtil::isArchObject(App::DocumentObject* obj) { bool result = false; - App::Property* proxy = obj->getPropertyByName("Proxy"); + App::PropertyPythonObject* proxy = dynamic_cast(obj->getPropertyByName("Proxy")); if (proxy != nullptr) { //if no proxy, can not be Arch obj //if has proxy, might be Arch obj - App::PropertyPythonObject* proxyPy = dynamic_cast(proxy); - Py::Object proxyObj = proxyPy->getValue(); + Py::Object proxyObj = proxy->getValue(); std::stringstream ss; - if (proxyPy != nullptr) { - Base::PyGILStateLocker lock; - try { - if (proxyObj.hasAttr("__module__")) { - Py::String mod(proxyObj.getAttr("__module__")); - ss << (std::string)mod; - //does this have to be an ArchSection, or can it be any Arch object? - if (ss.str().find("Arch") != std::string::npos) { - result = true; - } + Base::PyGILStateLocker lock; + try { + if (proxyObj.hasAttr("__module__")) { + Py::String mod(proxyObj.getAttr("__module__")); + ss << (std::string)mod; + //does this have to be an ArchSection, or can it be any Arch object? + if (ss.str().find("Arch") != std::string::npos) { + result = true; } } - catch (Py::Exception&) { - Base::PyException e; // extract the Python error text - e.ReportException(); - result = false; - } + } + catch (Py::Exception&) { + Base::PyException e; // extract the Python error text + e.ReportException(); + result = false; } } return result; @@ -218,32 +209,29 @@ bool DrawGuiUtil::isArchObject(App::DocumentObject* obj) bool DrawGuiUtil::isArchSection(App::DocumentObject* obj) { - bool result = false; - App::Property* proxy = obj->getPropertyByName("Proxy"); + bool result = false; + App::PropertyPythonObject* proxy = dynamic_cast(obj->getPropertyByName("Proxy")); if (proxy != nullptr) { //if no proxy, can not be Arch obj //if has proxy, might be Arch obj - App::PropertyPythonObject* proxyPy = dynamic_cast(proxy); - Py::Object proxyObj = proxyPy->getValue(); + Py::Object proxyObj = proxy->getValue(); std::stringstream ss; - if (proxyPy != nullptr) { - Base::PyGILStateLocker lock; - try { - if (proxyObj.hasAttr("__module__")) { - Py::String mod(proxyObj.getAttr("__module__")); - ss << (std::string)mod; - //does this have to be an ArchSection, or can it be other Arch objects? - if (ss.str().find("ArchSectionPlane") != std::string::npos) { - result = true; - } + Base::PyGILStateLocker lock; + try { + if (proxyObj.hasAttr("__module__")) { + Py::String mod(proxyObj.getAttr("__module__")); + ss << (std::string)mod; + //does this have to be an ArchSection, or can it be other Arch objects? + if (ss.str().find("ArchSectionPlane") != std::string::npos) { + result = true; } } - catch (Py::Exception&) { - Base::PyException e; // extract the Python error text - e.ReportException(); - result = false; - } + } + catch (Py::Exception&) { + Base::PyException e; // extract the Python error text + e.ReportException(); + result = false; } } return result; diff --git a/src/Mod/TechDraw/Gui/Grabber3d.cpp b/src/Mod/TechDraw/Gui/Grabber3d.cpp index ba34298788..71f8a952e4 100644 --- a/src/Mod/TechDraw/Gui/Grabber3d.cpp +++ b/src/Mod/TechDraw/Gui/Grabber3d.cpp @@ -149,10 +149,10 @@ double Grabber3d::copyActiveViewToSvgFile(App::Document* appDoc, double sourceHeight = 0.0; double sourceAngle = 45.0; if (sourceCam->getTypeId() == SoOrthographicCamera::getClassTypeId()) { - oCam = dynamic_cast(sourceCam); + oCam = static_cast(sourceCam); sourceHeight = oCam->height.getValue(); } else if (sourceCam->getTypeId() == SoPerspectiveCamera::getClassTypeId()) { - pCam = dynamic_cast(sourceCam); + pCam = static_cast(sourceCam); sourceAngle = pCam->heightAngle.getValue(); } oCam = nullptr; @@ -197,11 +197,11 @@ double Grabber3d::copyActiveViewToSvgFile(App::Document* appDoc, svgCam->focalDistance.setValue(sourceFocal); svgCam->aspectRatio.setValue(sourceAspect); if (svgCam->getTypeId() == SoOrthographicCamera::getClassTypeId()) { - SoOrthographicCamera* oSvgCam = dynamic_cast(svgCam); + SoOrthographicCamera* oSvgCam = static_cast(svgCam); double newHeight = sourceHeight * zoomFactor; oSvgCam->height.setValue(newHeight); } else if (svgCam->getTypeId() == SoPerspectiveCamera::getClassTypeId()) { - SoPerspectiveCamera* vSvgCam = dynamic_cast(svgCam); + SoPerspectiveCamera* vSvgCam = static_cast(svgCam); vSvgCam->heightAngle.setValue(sourceAngle); } diff --git a/src/Mod/TechDraw/Gui/QGIViewBalloon.cpp b/src/Mod/TechDraw/Gui/QGIViewBalloon.cpp index 8811bf3eb7..64802b0994 100644 --- a/src/Mod/TechDraw/Gui/QGIViewBalloon.cpp +++ b/src/Mod/TechDraw/Gui/QGIViewBalloon.cpp @@ -368,7 +368,8 @@ void QGIViewBalloon::setViewPartFeature(TechDraw::DrawViewBalloon *balloon) App::DocumentObject* docObj = balloon->SourceView.getValue(); if (docObj == nullptr) { balloonParent = dynamic_cast(docObj); - scale = balloonParent->getScale(); + if (balloonParent) + scale = balloonParent->getScale(); } float x = Rez::guiX(balloon->X.getValue() * scale) ; @@ -509,12 +510,9 @@ void QGIViewBalloon::placeBalloon(QPointF pos) return; } - DrawView* balloonParent = nullptr; - App::DocumentObject* docObj = balloon->SourceView.getValue(); - if (docObj == nullptr) { + DrawView* balloonParent = dynamic_cast(balloon->SourceView.getValue()); + if (balloonParent == nullptr) { return; - } else { - balloonParent = dynamic_cast(docObj); } auto featPage = balloonParent->findParentPage(); diff --git a/src/Mod/TechDraw/Gui/QGVPage.cpp b/src/Mod/TechDraw/Gui/QGVPage.cpp index 822a81230e..3a61eb3fa9 100644 --- a/src/Mod/TechDraw/Gui/QGVPage.cpp +++ b/src/Mod/TechDraw/Gui/QGVPage.cpp @@ -858,7 +858,7 @@ void QGVPage::saveSvg(QString filename) m_vpPage->setFrameState(saveState); m_vpPage->setTemplateMarkers(saveState); setExporting(false); - if (templateVisible) { + if (templateVisible && svgTemplate) { svgTemplate->show(); } diff --git a/src/Mod/TechDraw/Gui/TaskCenterLine.cpp b/src/Mod/TechDraw/Gui/TaskCenterLine.cpp index 4146d0673f..9f97bd3ae7 100644 --- a/src/Mod/TechDraw/Gui/TaskCenterLine.cpp +++ b/src/Mod/TechDraw/Gui/TaskCenterLine.cpp @@ -426,7 +426,7 @@ double TaskCenterLine::getCenterWidth() delete lg; Gui::ViewProvider* vp = QGIView::getViewProvider(m_partFeat); auto partVP = dynamic_cast(vp); - if ( vp != nullptr ) { + if ( partVP != nullptr ) { width = partVP->IsoWidth.getValue(); } return width; diff --git a/src/Mod/TechDraw/Gui/TaskLineDecor.cpp b/src/Mod/TechDraw/Gui/TaskLineDecor.cpp index f7094c1d3a..7dc10302ba 100644 --- a/src/Mod/TechDraw/Gui/TaskLineDecor.cpp +++ b/src/Mod/TechDraw/Gui/TaskLineDecor.cpp @@ -143,7 +143,7 @@ void TaskLineDecor::getDefaults(void) } else { Gui::ViewProvider* vp = QGIView::getViewProvider(m_partFeat); auto partVP = dynamic_cast(vp); - if ( vp != nullptr ) { + if ( partVP != nullptr ) { m_weight = partVP->LineWidth.getValue(); m_style = Qt::SolidLine; // = 1 m_color = LineFormat::getDefEdgeColor(); diff --git a/src/Mod/TechDraw/Gui/TaskRichAnno.cpp b/src/Mod/TechDraw/Gui/TaskRichAnno.cpp index 3944c4d6ff..1ade732bb9 100644 --- a/src/Mod/TechDraw/Gui/TaskRichAnno.cpp +++ b/src/Mod/TechDraw/Gui/TaskRichAnno.cpp @@ -463,7 +463,7 @@ QPointF TaskRichAnno::calcTextStartPos(double scale) std::vector points; if (m_baseFeat != nullptr) { if (m_baseFeat->isDerivedFrom(TechDraw::DrawLeaderLine::getClassTypeId())) { - TechDraw::DrawLeaderLine* dll = dynamic_cast(m_baseFeat); + TechDraw::DrawLeaderLine* dll = static_cast(m_baseFeat); points = dll->WayPoints.getValues(); } else { // Base::Console().Message("TRA::calcTextPos - m_baseFeat is not Leader\n"); diff --git a/src/Mod/TechDraw/Gui/ViewProviderPage.cpp b/src/Mod/TechDraw/Gui/ViewProviderPage.cpp index 4af8ecec1e..4b9262769a 100644 --- a/src/Mod/TechDraw/Gui/ViewProviderPage.cpp +++ b/src/Mod/TechDraw/Gui/ViewProviderPage.cpp @@ -449,10 +449,12 @@ void ViewProviderPage::setTemplateMarkers(bool state) Gui::Document* guiDoc = Gui::Application::Instance->getDocument(templateFeat->getDocument()); Gui::ViewProvider* vp = guiDoc->getViewProvider(templateFeat); ViewProviderTemplate* vpt = dynamic_cast(vp); - vpt->setMarkers(state); - QGITemplate* t = vpt->getQTemplate(); - if (t != nullptr) { - t->updateView(true); + if (vpt) { + vpt->setMarkers(state); + QGITemplate* t = vpt->getQTemplate(); + if (t != nullptr) { + t->updateView(true); + } } }