diff --git a/Sofa/Component/Collision/Detection/Algorithm/src/sofa/component/collision/detection/algorithm/IncrSAP.cpp b/Sofa/Component/Collision/Detection/Algorithm/src/sofa/component/collision/detection/algorithm/IncrSAP.cpp index e8e991d9a37..5d854f23a3e 100644 --- a/Sofa/Component/Collision/Detection/Algorithm/src/sofa/component/collision/detection/algorithm/IncrSAP.cpp +++ b/Sofa/Component/Collision/Detection/Algorithm/src/sofa/component/collision/detection/algorithm/IncrSAP.cpp @@ -200,6 +200,8 @@ inline void IncrSAP::addCollisionModel(core::CollisionModel *cm) _nothing_added = false; CubeCollisionModel * cube_model = dynamic_cast(cm->getLast()->getPrevious()); + if (!cube_model) + return; assert(cube_model->getPrevious() == cm->getFirst()); const int old_size = _boxes.size(); diff --git a/Sofa/Component/Collision/Detection/Intersection/src/sofa/component/collision/detection/intersection/LocalMinDistance.cpp b/Sofa/Component/Collision/Detection/Intersection/src/sofa/component/collision/detection/intersection/LocalMinDistance.cpp index 2e32744597a..156df23d1c8 100644 --- a/Sofa/Component/Collision/Detection/Intersection/src/sofa/component/collision/detection/intersection/LocalMinDistance.cpp +++ b/Sofa/Component/Collision/Detection/Intersection/src/sofa/component/collision/detection/intersection/LocalMinDistance.cpp @@ -1196,7 +1196,7 @@ bool LocalMinDistance::testValidity(Point &p, const Vec3 &PQ) const const Vec3 pt = p.p(); const sofa::simulation::Node* node = dynamic_cast(p.getCollisionModel()->getContext()); - if ( !(node->get< LineCollisionModel >()) ) + if ( !node || !(node->get< LineCollisionModel >()) ) return true; BaseMeshTopology* topology = p.getCollisionModel()->getCollisionTopology(); diff --git a/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/StopperLagrangianConstraint.inl b/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/StopperLagrangianConstraint.inl index ec48235ed5e..abc5006cd1a 100644 --- a/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/StopperLagrangianConstraint.inl +++ b/Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/StopperLagrangianConstraint.inl @@ -41,7 +41,6 @@ template void StopperLagrangianConstraint::init() { this->mstate = dynamic_cast(this->getContext()->getMechanicalState()); - assert(this->mstate); helper::WriteAccessor > xData = *this->mstate->write(core::vec_id::write_access::position); VecCoord& x = xData.wref(); diff --git a/Sofa/GUI/Component/src/sofa/gui/component/performer/FixParticlePerformer.inl b/Sofa/GUI/Component/src/sofa/gui/component/performer/FixParticlePerformer.inl index c29717ca026..698323e6a7e 100644 --- a/Sofa/GUI/Component/src/sofa/gui/component/performer/FixParticlePerformer.inl +++ b/Sofa/GUI/Component/src/sofa/gui/component/performer/FixParticlePerformer.inl @@ -130,8 +130,11 @@ sofa::component::statecontainer::MechanicalObject< DataTypes >* FixParticlePerfo else if (b.mstate) { collisionState = dynamic_cast(b.mstate); - fixPoint = (collisionState->read(core::vec_id::read_access::position)->getValue())[idx]; - points.push_back(idx); + if (collisionState) + { + fixPoint = (collisionState->read(core::vec_id::read_access::position)->getValue())[idx]; + points.push_back(idx); + } } diff --git a/Sofa/GUI/Component/src/sofa/gui/component/performer/MouseInteractor.inl b/Sofa/GUI/Component/src/sofa/gui/component/performer/MouseInteractor.inl index 8c203a0f257..73f4d456410 100644 --- a/Sofa/GUI/Component/src/sofa/gui/component/performer/MouseInteractor.inl +++ b/Sofa/GUI/Component/src/sofa/gui/component/performer/MouseInteractor.inl @@ -32,7 +32,13 @@ void MouseInteractor::init() { BaseMouseInteractor::init(); mouseInSofa = dynamic_cast< MouseContainer*>(this->getContext()->getMechanicalState()); - assert(mouseInSofa); + if (!mouseInSofa) + { + msg_error() << "MouseInteractor requires a MechanicalState of the correct type (" << DataTypes::Name() << ") in its context"; + this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid); + + return; + } } } // namespace sofa::gui::component::performer diff --git a/Sofa/framework/Core/src/sofa/core/behavior/DefaultMultiMatrixAccessor.cpp b/Sofa/framework/Core/src/sofa/core/behavior/DefaultMultiMatrixAccessor.cpp index 2857e709542..a49c379b1ab 100644 --- a/Sofa/framework/Core/src/sofa/core/behavior/DefaultMultiMatrixAccessor.cpp +++ b/Sofa/framework/Core/src/sofa/core/behavior/DefaultMultiMatrixAccessor.cpp @@ -206,7 +206,12 @@ DefaultMultiMatrixAccessor::InteractionMatrixRef DefaultMultiMatrixAccessor::get InteractionMatrixRef r2; if (mstate1 == mstate2)// case where state1 == state2, interaction matrix is on the diagonal stiffness block { - const MatrixRef r = diagonalStiffnessBloc.find(mstate1)->second; + const auto it = diagonalStiffnessBloc.find(mstate1); + if (it == diagonalStiffnessBloc.end()) + { + return r2; // Return empty/default InteractionMatrixRef + } + const MatrixRef r = it->second; r2.matrix = r.matrix; r2.offRow = r.offset; r2.offCol = r.offset; diff --git a/Sofa/framework/Helper/src/sofa/helper/Utils.cpp b/Sofa/framework/Helper/src/sofa/helper/Utils.cpp index 21ed54809ee..80087c8bac0 100644 --- a/Sofa/framework/Helper/src/sofa/helper/Utils.cpp +++ b/Sofa/framework/Helper/src/sofa/helper/Utils.cpp @@ -226,7 +226,7 @@ const std::string& Utils::getUserHomeDirectory() { #ifdef WIN32 // Windows: ${HOME} const char* homeDir = std::getenv("USERPROFILE"); - return std::string(homeDir); + return homeDir ? std::string(homeDir) : std::string(""); #elif defined(__APPLE__) // macOS : ${HOME} (usually /Users/username) glob_t globbuf; if (glob("~", GLOB_TILDE, nullptr, &globbuf) == 0) @@ -256,10 +256,12 @@ const std::string& Utils::getUserHomeDirectory() if ((homeDir = std::getenv("HOME")) == nullptr) { // else system calls are used - homeDir = getpwuid(getuid())->pw_dir; + struct passwd* pw = getpwuid(getuid()); + if (pw != nullptr) + homeDir = pw->pw_dir; } - return std::string(homeDir); + return homeDir ? std::string(homeDir) : std::string(""); #endif }; diff --git a/Sofa/framework/Simulation/Common/src/sofa/simulation/common/xml/BaseMultiMappingElement.cpp b/Sofa/framework/Simulation/Common/src/sofa/simulation/common/xml/BaseMultiMappingElement.cpp index 53ea0fac1ab..388d2d7eb4a 100644 --- a/Sofa/framework/Simulation/Common/src/sofa/simulation/common/xml/BaseMultiMappingElement.cpp +++ b/Sofa/framework/Simulation/Common/src/sofa/simulation/common/xml/BaseMultiMappingElement.cpp @@ -60,14 +60,16 @@ bool BaseMultiMappingElement::initNode() for( iterState = inputStates.begin(); iterState != inputStates.end(); ++iterState) { simulation::Node* node = dynamic_cast< simulation::Node* >((*iterState)->getContext()); - inputNodes.push_back(node); + if (node) + inputNodes.push_back(node); } /* */ /* get the Nodes corresponding to each output BaseState context */ for( iterState = outputStates.begin(); iterState != outputStates.end(); ++iterState) { simulation::Node* node = dynamic_cast< simulation::Node* >((*iterState)->getContext()); - outputNodes.push_back(node); + if (node) + outputNodes.push_back(node); } type::vector::iterator iterNode; diff --git a/applications/plugins/ArticulatedSystemPlugin/src/ArticulatedSystemPlugin/ArticulatedHierarchyContainer.inl b/applications/plugins/ArticulatedSystemPlugin/src/ArticulatedSystemPlugin/ArticulatedHierarchyContainer.inl index 52354430354..754ae522f5d 100644 --- a/applications/plugins/ArticulatedSystemPlugin/src/ArticulatedSystemPlugin/ArticulatedHierarchyContainer.inl +++ b/applications/plugins/ArticulatedSystemPlugin/src/ArticulatedSystemPlugin/ArticulatedHierarchyContainer.inl @@ -242,6 +242,8 @@ void ArticulatedHierarchyContainer::init () for (; ac != acEnd; ac++) { context = dynamic_cast((*ac)->getContext()); + if (!context) + continue; for (simulation::Node::ChildIterator it = context->child.begin(); it != context->child.end(); ++it) { simulation::Node* n = it->get(); diff --git a/applications/plugins/Sensable/OmniDriver.cpp b/applications/plugins/Sensable/OmniDriver.cpp index bc6112c2fc4..f259f472c22 100644 --- a/applications/plugins/Sensable/OmniDriver.cpp +++ b/applications/plugins/Sensable/OmniDriver.cpp @@ -577,7 +577,8 @@ void OmniDriver::handleEvent(core::objectmodel::Event *event) Quat dummyQuat; sofa::core::objectmodel::HapticDeviceEvent event(currentToolIndex,dummyVector,dummyQuat,buttonState); simulation::Node *groot = dynamic_cast(getContext()->getRootContext()); // access to current node - groot->propagateEvent(core::ExecParams::defaultInstance(), &event); + if (groot) + groot->propagateEvent(core::ExecParams::defaultInstance(), &event); sofa::helper::AdvancedTimer::stepEnd("OmniDriver::6"); } @@ -639,7 +640,8 @@ void OmniDriver::handleEvent(core::objectmodel::Event *event) sofa::core::objectmodel::HapticDeviceEvent event(currentToolIndex,dummyVector,dummyQuat, sofa::core::objectmodel::HapticDeviceEvent::Button1StateMask); simulation::Node *groot = dynamic_cast(getContext()->getRootContext()); // access to current node - groot->propagateEvent(core::ExecParams::defaultInstance(), &event); + if (groot) + groot->propagateEvent(core::ExecParams::defaultInstance(), &event); } if (kpe->getKey()=='J' || kpe->getKey()=='j') { @@ -649,7 +651,8 @@ void OmniDriver::handleEvent(core::objectmodel::Event *event) sofa::core::objectmodel::HapticDeviceEvent event(currentToolIndex,dummyVector,dummyQuat, sofa::core::objectmodel::HapticDeviceEvent::Button2StateMask); simulation::Node *groot = dynamic_cast(getContext()->getRootContext()); // access to current node - groot->propagateEvent(core::ExecParams::defaultInstance(), &event); + if (groot) + groot->propagateEvent(core::ExecParams::defaultInstance(), &event); } } @@ -666,7 +669,8 @@ void OmniDriver::handleEvent(core::objectmodel::Event *event) Quat dummyQuat; sofa::core::objectmodel::HapticDeviceEvent event(currentToolIndex,dummyVector,dummyQuat,0); simulation::Node *groot = dynamic_cast(getContext()->getRootContext()); // access to current node - groot->propagateEvent(core::ExecParams::defaultInstance(), &event); + if (groot) + groot->propagateEvent(core::ExecParams::defaultInstance(), &event); } } }