Skip to content

Commit 63d135f

Browse files
committed
Add null checks after dynamic_cast calls
1 parent 6b1e530 commit 63d135f

File tree

8 files changed

+32
-11
lines changed

8 files changed

+32
-11
lines changed

Sofa/Component/Collision/Detection/Algorithm/src/sofa/component/collision/detection/algorithm/IncrSAP.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ inline void IncrSAP::addCollisionModel(core::CollisionModel *cm)
200200
_nothing_added = false;
201201

202202
CubeCollisionModel * cube_model = dynamic_cast<CubeCollisionModel *>(cm->getLast()->getPrevious());
203+
if (!cube_model)
204+
return;
203205
assert(cube_model->getPrevious() == cm->getFirst());
204206

205207
const int old_size = _boxes.size();

Sofa/Component/Collision/Detection/Intersection/src/sofa/component/collision/detection/intersection/LocalMinDistance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ bool LocalMinDistance::testValidity(Point &p, const Vec3 &PQ) const
11961196
const Vec3 pt = p.p();
11971197

11981198
const sofa::simulation::Node* node = dynamic_cast<sofa::simulation::Node*>(p.getCollisionModel()->getContext());
1199-
if ( !(node->get< LineCollisionModel<sofa::defaulttype::Vec3Types> >()) )
1199+
if ( !node || !(node->get< LineCollisionModel<sofa::defaulttype::Vec3Types> >()) )
12001200
return true;
12011201

12021202
BaseMeshTopology* topology = p.getCollisionModel()->getCollisionTopology();

Sofa/Component/Constraint/Lagrangian/Model/src/sofa/component/constraint/lagrangian/model/StopperLagrangianConstraint.inl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ template<class DataTypes>
4141
void StopperLagrangianConstraint<DataTypes>::init()
4242
{
4343
this->mstate = dynamic_cast<MechanicalState*>(this->getContext()->getMechanicalState());
44-
assert(this->mstate);
44+
if (!this->mstate)
45+
{
46+
msg_error() << "StopperLagrangianConstraint requires a MechanicalState of the correct type in its context";
47+
return;
48+
}
4549

4650
helper::WriteAccessor<Data<VecCoord> > xData = *this->mstate->write(core::vec_id::write_access::position);
4751
VecCoord& x = xData.wref();

Sofa/GUI/Component/src/sofa/gui/component/performer/FixParticlePerformer.inl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,11 @@ sofa::component::statecontainer::MechanicalObject< DataTypes >* FixParticlePerfo
130130
else if (b.mstate)
131131
{
132132
collisionState = dynamic_cast<MouseContainer*>(b.mstate);
133-
fixPoint = (collisionState->read(core::vec_id::read_access::position)->getValue())[idx];
134-
points.push_back(idx);
133+
if (collisionState)
134+
{
135+
fixPoint = (collisionState->read(core::vec_id::read_access::position)->getValue())[idx];
136+
points.push_back(idx);
137+
}
135138
}
136139

137140

Sofa/GUI/Component/src/sofa/gui/component/performer/MouseInteractor.inl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ void MouseInteractor<DataTypes>::init()
3232
{
3333
BaseMouseInteractor::init();
3434
mouseInSofa = dynamic_cast< MouseContainer*>(this->getContext()->getMechanicalState());
35-
assert(mouseInSofa);
35+
if (!mouseInSofa)
36+
{
37+
msg_error() << "MouseInteractor requires a MechanicalState of the correct type in its context";
38+
return;
39+
}
3640
}
3741

3842
} // namespace sofa::gui::component::performer

Sofa/framework/Simulation/Common/src/sofa/simulation/common/xml/BaseMultiMappingElement.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,16 @@ bool BaseMultiMappingElement::initNode()
6060
for( iterState = inputStates.begin(); iterState != inputStates.end(); ++iterState)
6161
{
6262
simulation::Node* node = dynamic_cast< simulation::Node* >((*iterState)->getContext());
63-
inputNodes.push_back(node);
63+
if (node)
64+
inputNodes.push_back(node);
6465
}
6566
/* */
6667
/* get the Nodes corresponding to each output BaseState context */
6768
for( iterState = outputStates.begin(); iterState != outputStates.end(); ++iterState)
6869
{
6970
simulation::Node* node = dynamic_cast< simulation::Node* >((*iterState)->getContext());
70-
outputNodes.push_back(node);
71+
if (node)
72+
outputNodes.push_back(node);
7173
}
7274

7375
type::vector<simulation::Node*>::iterator iterNode;

applications/plugins/ArticulatedSystemPlugin/src/ArticulatedSystemPlugin/ArticulatedHierarchyContainer.inl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ void ArticulatedHierarchyContainer::init ()
242242
for (; ac != acEnd; ac++)
243243
{
244244
context = dynamic_cast<simulation::Node *>((*ac)->getContext());
245+
if (!context)
246+
continue;
245247
for (simulation::Node::ChildIterator it = context->child.begin(); it != context->child.end(); ++it)
246248
{
247249
simulation::Node* n = it->get();

applications/plugins/Sensable/OmniDriver.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,8 @@ void OmniDriver::handleEvent(core::objectmodel::Event *event)
577577
Quat dummyQuat;
578578
sofa::core::objectmodel::HapticDeviceEvent event(currentToolIndex,dummyVector,dummyQuat,buttonState);
579579
simulation::Node *groot = dynamic_cast<simulation::Node *>(getContext()->getRootContext()); // access to current node
580-
groot->propagateEvent(core::ExecParams::defaultInstance(), &event);
580+
if (groot)
581+
groot->propagateEvent(core::ExecParams::defaultInstance(), &event);
581582
sofa::helper::AdvancedTimer::stepEnd("OmniDriver::6");
582583
}
583584

@@ -639,7 +640,8 @@ void OmniDriver::handleEvent(core::objectmodel::Event *event)
639640
sofa::core::objectmodel::HapticDeviceEvent event(currentToolIndex,dummyVector,dummyQuat,
640641
sofa::core::objectmodel::HapticDeviceEvent::Button1StateMask);
641642
simulation::Node *groot = dynamic_cast<simulation::Node *>(getContext()->getRootContext()); // access to current node
642-
groot->propagateEvent(core::ExecParams::defaultInstance(), &event);
643+
if (groot)
644+
groot->propagateEvent(core::ExecParams::defaultInstance(), &event);
643645
}
644646
if (kpe->getKey()=='J' || kpe->getKey()=='j')
645647
{
@@ -649,7 +651,8 @@ void OmniDriver::handleEvent(core::objectmodel::Event *event)
649651
sofa::core::objectmodel::HapticDeviceEvent event(currentToolIndex,dummyVector,dummyQuat,
650652
sofa::core::objectmodel::HapticDeviceEvent::Button2StateMask);
651653
simulation::Node *groot = dynamic_cast<simulation::Node *>(getContext()->getRootContext()); // access to current node
652-
groot->propagateEvent(core::ExecParams::defaultInstance(), &event);
654+
if (groot)
655+
groot->propagateEvent(core::ExecParams::defaultInstance(), &event);
653656
}
654657

655658
}
@@ -666,7 +669,8 @@ void OmniDriver::handleEvent(core::objectmodel::Event *event)
666669
Quat dummyQuat;
667670
sofa::core::objectmodel::HapticDeviceEvent event(currentToolIndex,dummyVector,dummyQuat,0);
668671
simulation::Node *groot = dynamic_cast<simulation::Node *>(getContext()->getRootContext()); // access to current node
669-
groot->propagateEvent(core::ExecParams::defaultInstance(), &event);
672+
if (groot)
673+
groot->propagateEvent(core::ExecParams::defaultInstance(), &event);
670674
}
671675
}
672676
}

0 commit comments

Comments
 (0)