issue #0003070: VRML 2 crashes with corner cases

This commit is contained in:
wmayer
2017-06-11 16:41:52 +02:00
parent ab1b344410
commit a405b4dae0
3 changed files with 163 additions and 92 deletions

View File

@@ -433,6 +433,68 @@ PyObject* ViewProvider::getPyObject()
return pyViewObject;
}
#include <boost/graph/topological_sort.hpp>
namespace Gui {
typedef boost::adjacency_list <
boost::vecS, // class OutEdgeListS : a Sequence or an AssociativeContainer
boost::vecS, // class VertexListS : a Sequence or a RandomAccessContainer
boost::directedS, // class DirectedS : This is a directed graph
boost::no_property, // class VertexProperty:
boost::no_property, // class EdgeProperty:
boost::no_property, // class GraphProperty:
boost::listS // class EdgeListS:
> Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
typedef boost::graph_traits<Graph>::edge_descriptor Edge;
void addNodes(Graph& graph, std::map<SoNode*, Vertex>& vertexNodeMap, SoNode* node)
{
if (node->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) {
SoGroup* group = static_cast<SoGroup*>(node);
Vertex groupV = vertexNodeMap[group];
for (int i=0; i<group->getNumChildren(); i++) {
SoNode* child = group->getChild(i);
auto it = vertexNodeMap.find(child);
// the child node is not yet added to the map
if (it == vertexNodeMap.end()) {
Vertex childV = add_vertex(graph);
vertexNodeMap[child] = childV;
add_edge(groupV, childV, graph);
addNodes(graph, vertexNodeMap, child);
}
// the child is already there, only add the edge then
else {
add_edge(groupV, it->second, graph);
}
}
}
}
}
bool ViewProvider::checkRecursion(SoNode* node)
{
if (node->getTypeId().isDerivedFrom(SoGroup::getClassTypeId())) {
std::list<Vertex> make_order;
Graph graph;
std::map<SoNode*, Vertex> vertexNodeMap;
Vertex groupV = add_vertex(graph);
vertexNodeMap[node] = groupV;
addNodes(graph, vertexNodeMap, node);
try {
boost::topological_sort(graph, std::front_inserter(make_order));
}
catch (const std::exception&) {
return false;
}
}
return true;
}
SoPickedPoint* ViewProvider::getPointOnRay(const SbVec2s& pos, const View3DInventorViewer* viewer) const
{
//first get the path to this node and calculate the current transformation

View File

@@ -333,6 +333,10 @@ public:
//@}
protected:
/** Helper method to check that the node is valid, i.e. it must not cause
* and infinite recursion.
*/
bool checkRecursion(SoNode*);
/** Helper method to get picked entities while editing.
* It's in the responsibility of the caller to delete the returned instance.
*/

View File

@@ -48,6 +48,7 @@
#include "SoFCSelection.h"
#include <App/VRMLObject.h>
#include <App/Document.h>
#include <Base/Console.h>
#include <Base/FileInfo.h>
#include <Base/Stream.h>
#include <sstream>
@@ -96,21 +97,21 @@ std::vector<std::string> ViewProviderVRMLObject::getDisplayModes(void) const
template<typename T>
void ViewProviderVRMLObject::getResourceFile(SoNode* node, std::list<std::string>& resources)
{
SoSearchAction sa;
sa.setType(T::getClassTypeId());
sa.setInterest(SoSearchAction::ALL);
sa.setSearchingAll(true);
sa.apply(node);
const SoPathList & pathlist = sa.getPaths();
for (int i = 0; i < pathlist.getLength(); i++ ) {
SoFullPath * path = static_cast<SoFullPath *>(pathlist[i]);
if (path->getTail()->isOfType(T::getClassTypeId())) {
T * tex = static_cast<T*>(path->getTail());
for (int j = 0; j < tex->url.getNum(); j++) {
this->addResource(tex->url[j], resources);
}
}
}
SoSearchAction sa;
sa.setType(T::getClassTypeId());
sa.setInterest(SoSearchAction::ALL);
sa.setSearchingAll(true);
sa.apply(node);
const SoPathList & pathlist = sa.getPaths();
for (int i = 0; i < pathlist.getLength(); i++ ) {
SoFullPath * path = static_cast<SoFullPath *>(pathlist[i]);
if (path->getTail()->isOfType(T::getClassTypeId())) {
T * tex = static_cast<T*>(path->getTail());
for (int j = 0; j < tex->url.getNum(); j++) {
this->addResource(tex->url[j], resources);
}
}
}
}
namespace Gui {
@@ -118,92 +119,92 @@ namespace Gui {
template<>
void ViewProviderVRMLObject::getResourceFile<SoVRMLBackground>(SoNode* node, std::list<std::string>& resources)
{
SoSearchAction sa;
sa.setType(SoVRMLBackground::getClassTypeId());
sa.setInterest(SoSearchAction::ALL);
sa.setSearchingAll(true);
sa.apply(node);
const SoPathList & pathlist = sa.getPaths();
for (int i = 0; i < pathlist.getLength(); i++ ) {
SoFullPath * path = static_cast<SoFullPath *>(pathlist[i]);
if (path->getTail()->isOfType(SoVRMLBackground::getClassTypeId())) {
SoVRMLBackground * vrml = static_cast<SoVRMLBackground*>(path->getTail());
// backUrl
for (int j = 0; j < vrml->backUrl.getNum(); j++) {
addResource(vrml->backUrl[j], resources);
}
// bottomUrl
for (int j = 0; j < vrml->bottomUrl.getNum(); j++) {
addResource(vrml->bottomUrl[j], resources);
}
// frontUrl
for (int j = 0; j < vrml->frontUrl.getNum(); j++) {
addResource(vrml->frontUrl[j], resources);
}
// leftUrl
for (int j = 0; j < vrml->leftUrl.getNum(); j++) {
addResource(vrml->leftUrl[j], resources);
}
// rightUrl
for (int j = 0; j < vrml->rightUrl.getNum(); j++) {
addResource(vrml->rightUrl[j], resources);
}
// topUrl
for (int j = 0; j < vrml->topUrl.getNum(); j++) {
addResource(vrml->topUrl[j], resources);
}
}
}
SoSearchAction sa;
sa.setType(SoVRMLBackground::getClassTypeId());
sa.setInterest(SoSearchAction::ALL);
sa.setSearchingAll(true);
sa.apply(node);
const SoPathList & pathlist = sa.getPaths();
for (int i = 0; i < pathlist.getLength(); i++ ) {
SoFullPath * path = static_cast<SoFullPath *>(pathlist[i]);
if (path->getTail()->isOfType(SoVRMLBackground::getClassTypeId())) {
SoVRMLBackground * vrml = static_cast<SoVRMLBackground*>(path->getTail());
// backUrl
for (int j = 0; j < vrml->backUrl.getNum(); j++) {
addResource(vrml->backUrl[j], resources);
}
// bottomUrl
for (int j = 0; j < vrml->bottomUrl.getNum(); j++) {
addResource(vrml->bottomUrl[j], resources);
}
// frontUrl
for (int j = 0; j < vrml->frontUrl.getNum(); j++) {
addResource(vrml->frontUrl[j], resources);
}
// leftUrl
for (int j = 0; j < vrml->leftUrl.getNum(); j++) {
addResource(vrml->leftUrl[j], resources);
}
// rightUrl
for (int j = 0; j < vrml->rightUrl.getNum(); j++) {
addResource(vrml->rightUrl[j], resources);
}
// topUrl
for (int j = 0; j < vrml->topUrl.getNum(); j++) {
addResource(vrml->topUrl[j], resources);
}
}
}
}
}
void ViewProviderVRMLObject::addResource(const SbString& url, std::list<std::string>& resources)
{
SbString found = SoInput::searchForFile(url, SoInput::getDirectories(), SbStringList());
Base::FileInfo fi(found.getString());
if (fi.exists()) {
// add the resource file if not yet listed
if (std::find(resources.begin(), resources.end(), found.getString()) == resources.end()) {
resources.push_back(found.getString());
}
}
SbString found = SoInput::searchForFile(url, SoInput::getDirectories(), SbStringList());
Base::FileInfo fi(found.getString());
if (fi.exists()) {
// add the resource file if not yet listed
if (std::find(resources.begin(), resources.end(), found.getString()) == resources.end()) {
resources.push_back(found.getString());
}
}
}
void ViewProviderVRMLObject::getLocalResources(SoNode* node, std::list<std::string>& resources)
{
// search for SoVRMLInline files
SoSearchAction sa;
sa.setType(SoVRMLInline::getClassTypeId());
sa.setInterest(SoSearchAction::ALL);
sa.setSearchingAll(true);
sa.apply(node);
const SoPathList & pathlist = sa.getPaths();
for (int i = 0; i < pathlist.getLength(); i++ ) {
SoPath * path = pathlist[i];
SoVRMLInline * vrml = static_cast<SoVRMLInline*>(path->getTail());
const SbString& url = vrml->getFullURLName();
if (url.getLength() > 0) {
// add the resource file if not yet listed
if (std::find(resources.begin(), resources.end(), url.getString()) == resources.end()) {
resources.push_back(url.getString());
}
// if the resource file could be loaded check if it references further resources
if (vrml->getChildData()) {
getLocalResources(vrml->getChildData(), resources);
}
}
}
// search for SoVRMLImageTexture, ... files
getResourceFile<SoVRMLImageTexture >(node, resources);
getResourceFile<SoVRMLMovieTexture >(node, resources);
getResourceFile<SoVRMLScript >(node, resources);
getResourceFile<SoVRMLBackground >(node, resources);
getResourceFile<SoVRMLAudioClip >(node, resources);
getResourceFile<SoVRMLAnchor >(node, resources);
sa.setType(SoVRMLInline::getClassTypeId());
sa.setInterest(SoSearchAction::ALL);
sa.setSearchingAll(true);
sa.apply(node);
const SoPathList & pathlist = sa.getPaths();
for (int i = 0; i < pathlist.getLength(); i++ ) {
SoPath * path = pathlist[i];
SoVRMLInline * vrml = static_cast<SoVRMLInline*>(path->getTail());
const SbString& url = vrml->getFullURLName();
if (url.getLength() > 0) {
// add the resource file if not yet listed
if (std::find(resources.begin(), resources.end(), url.getString()) == resources.end()) {
resources.push_back(url.getString());
}
// if the resource file could be loaded check if it references further resources
if (vrml->getChildData()) {
getLocalResources(vrml->getChildData(), resources);
}
}
}
// search for SoVRMLImageTexture, ... files
getResourceFile<SoVRMLImageTexture >(node, resources);
getResourceFile<SoVRMLMovieTexture >(node, resources);
getResourceFile<SoVRMLScript >(node, resources);
getResourceFile<SoVRMLBackground >(node, resources);
getResourceFile<SoVRMLAudioClip >(node, resources);
getResourceFile<SoVRMLAnchor >(node, resources);
}
void ViewProviderVRMLObject::updateData(const App::Property* prop)
@@ -231,15 +232,19 @@ void ViewProviderVRMLObject::updateData(const App::Property* prop)
SoSeparator * node = SoDB::readAll(&in);
if (node) {
if (!checkRecursion(node)) {
Base::Console().Error("The VRML file causes an infinite recursion!\n");
return;
}
pcVRML->addChild(node);
std::list<std::string> urls;
getLocalResources(node, urls);
if (!urls.empty() && ivObj->Urls.getSize() == 0) {
if (!urls.empty() && ivObj->Urls.getSize() == 0) {
std::vector<std::string> res;
res.insert(res.end(), urls.begin(), urls.end());
ivObj->Urls.setValues(res);
}
ivObj->Urls.setValues(res);
}
}
SoInput::removeDirectory(filepath.constData());
SoInput::removeDirectory(subpath.constData());