Skip to content

Commit

Permalink
#5347: Fix a crash in scene::getNodeIndices when a primitive without …
Browse files Browse the repository at this point in the history
…a parent is encountered.

Protect the calling EntityInspector code against out_of_range exceptions that might be thrown by getNodeIndices.
  • Loading branch information
codereader committed Oct 1, 2020
1 parent 80d60d4 commit d162f4a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
22 changes: 14 additions & 8 deletions libs/scene/SelectionIndex.cpp
Expand Up @@ -115,18 +115,24 @@ std::pair<std::size_t, std::size_t> getNodeIndices(const scene::INodePtr& node)
}
else if (Node_isPrimitive(node))
{
scene::INodePtr parent = node->getParent();
auto parent = node->getParent();

// Node is a primitive, find parent entity and child index
EntityFindIndexWalker walker(parent);
GlobalSceneGraph().root()->traverse(walker);
// In rare cases, such as when a drag-selection brush is deleted
// but still selected, we might reach this point with a primitive
// node without a parent. At least we shouldn't crash.
if (parent)
{
// Node is a primitive, find parent entity and child index
EntityFindIndexWalker walker(parent);
GlobalSceneGraph().root()->traverse(walker);

result.first = walker.getIndex(); // might throw
result.first = walker.getIndex(); // might throw

PrimitiveFindIndexWalker brushWalker(node);
parent->traverse(brushWalker);
PrimitiveFindIndexWalker brushWalker(node);
parent->traverse(brushWalker);

result.second = walker.getIndex(); // might throw
result.second = walker.getIndex(); // might throw
}
}
else
{
Expand Down
12 changes: 10 additions & 2 deletions radiant/ui/einspector/EntityInspector.cpp
Expand Up @@ -1197,9 +1197,17 @@ void EntityInspector::getEntityFromSelectionSystem()
scene::INodePtr selectedNodeParent = selectedNode->getParent();
changeSelectedEntity(selectedNodeParent);

auto indices = scene::getNodeIndices(selectedNode);
try
{
auto indices = scene::getNodeIndices(selectedNode);

_primitiveNumLabel->SetLabelText(fmt::format(_("Entity {0}, Primitive {1}"), indices.first, indices.second));
_primitiveNumLabel->SetLabelText(fmt::format(_("Entity {0}, Primitive {1}"), indices.first, indices.second));
}
catch (const std::out_of_range& ex)
{
rWarning() << ex.what() << std::endl;
_primitiveNumLabel->SetLabelText("-");
}
}
}

Expand Down

0 comments on commit d162f4a

Please sign in to comment.