Skip to content

Commit

Permalink
Avoid crashes and better warn when getting PROTO
Browse files Browse the repository at this point in the history
Avoid crashes and better warn when getting the PROTO in
`WbSupervisorUtilities::getProtoParameterNodeInstance()`.

Signed-off-by: Gaël Écorchard <gael@km-robotics.cz>
  • Loading branch information
Gaël Écorchard committed Jan 11, 2024
1 parent 26e08bb commit f5d25cb
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/webots/nodes/utils/WbSupervisorUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,13 @@ WbNode *WbSupervisorUtilities::getProtoParameterNodeInstance(int nodeId, const Q
mRobot->warn(tr("%1: node not found.").arg(functionName));
return NULL;
}
return static_cast<WbBaseNode *>(node)->getFirstFinalizedProtoInstance();
WbBaseNode *proto = static_cast<WbBaseNode *>(node)->getFirstFinalizedProtoInstance();
if (!proto)
{
mRobot->warn(tr("Cannot get the PROTO instance for node `%1` (type %2).").arg(node->modelName(), node->nodeModelName()));
return NULL;
}
return proto;
}

void WbSupervisorUtilities::changeSimulationMode(int newMode) {
Expand Down Expand Up @@ -594,15 +600,21 @@ void WbSupervisorUtilities::handleMessage(QDataStream &stream) {
stream >> nodeId;
const QString &stateName = readString(stream);
WbNode *const node = getProtoParameterNodeInstance(nodeId, "wb_supervisor_node_load_state()");
node->reset(stateName);
if (node)
{
node->reset(stateName);
}
return;
}
case C_SUPERVISOR_NODE_SAVE_STATE: {
unsigned int nodeId;
stream >> nodeId;
const QString &stateName = readString(stream);
WbNode *const node = getProtoParameterNodeInstance(nodeId, "wb_supervisor_node_save_state()");
node->save(stateName);
if (node)
{
node->save(stateName);
}
return;
}
case C_SUPERVISOR_NODE_SET_JOINT_POSITION: {
Expand Down Expand Up @@ -1058,6 +1070,11 @@ void WbSupervisorUtilities::handleMessage(QDataStream &stream) {
stream >> relative;

WbNode *const node = getProtoParameterNodeInstance(id, "wb_supervisor_node_add_force()");
if (!node)
{
mRobot->warn(QString{"Cannot get node with id "} + QString::number(id));
return;
}
WbSolid *const solid = dynamic_cast<WbSolid *>(node);
if (solid) {
WbVector3 force(fx, fy, fz);
Expand Down Expand Up @@ -1090,6 +1107,11 @@ void WbSupervisorUtilities::handleMessage(QDataStream &stream) {
stream >> relative;

WbNode *const node = getProtoParameterNodeInstance(id, "wb_supervisor_node_add_force_with_offset()");
if (!node)
{
mRobot->warn(tr("Cannot get node with id ") + QString::number(id));
return;
}
WbSolid *const solid = dynamic_cast<WbSolid *>(node);
if (solid) {
const dBodyID body = solid->bodyMerger();
Expand Down Expand Up @@ -1119,6 +1141,11 @@ void WbSupervisorUtilities::handleMessage(QDataStream &stream) {
stream >> relative;

WbNode *const node = getProtoParameterNodeInstance(id, "wb_supervisor_node_add_torque()");
if (!node)
{
mRobot->warn(tr("Cannot get node with id ") + QString::number(id));
return;
}
WbSolid *const solid = dynamic_cast<WbSolid *>(node);
if (solid) {
WbVector3 torque(tx, ty, tz);
Expand Down

0 comments on commit f5d25cb

Please sign in to comment.