From 4c79f713ed53a2f02a1639bc854b8291daeb1f98 Mon Sep 17 00:00:00 2001 From: Thomas Krijnen Date: Mon, 26 Jan 2015 18:11:48 +0000 Subject: [PATCH] Make sure C++ exceptions do not crash the Python interpreter --- src/ifcwrap/IfcGeomWrapper.i | 3 +++ src/ifcwrap/IfcParseWrapper.i | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/src/ifcwrap/IfcGeomWrapper.i b/src/ifcwrap/IfcGeomWrapper.i index 18da4f9bc4..64b1fb9bd1 100644 --- a/src/ifcwrap/IfcGeomWrapper.i +++ b/src/ifcwrap/IfcGeomWrapper.i @@ -282,6 +282,9 @@ kernel.setValue(IfcGeom::Kernel::GV_PRECISION, precision); IfcGeom::BRepElement* brep = kernel.create_brep_for_representation_and_product(settings, representation, product); + if (!brep) { + throw IfcParse::IfcException("Failed to process shape"); + } if (settings.use_brep_data()) { IfcGeom::SerializedElement* serialization = new IfcGeom::SerializedElement(*brep); delete brep; diff --git a/src/ifcwrap/IfcParseWrapper.i b/src/ifcwrap/IfcParseWrapper.i index 5a8184c9cb..b792d9ecdb 100644 --- a/src/ifcwrap/IfcParseWrapper.i +++ b/src/ifcwrap/IfcParseWrapper.i @@ -100,6 +100,10 @@ namespace IfcUtil { } %typemap(out) std::pair { + // The SWIG %exception directive does not take care + // of our typemap. So the argument conversion block + // is wrapped in a try-catch block manually. + try { const Argument& arg = *($1.second); const IfcUtil::ArgumentType type = $1.first; if (arg.isNull() || type == IfcUtil::Argument_DERIVED) { @@ -164,6 +168,11 @@ namespace IfcUtil { break; } } + } catch(IfcParse::IfcException& e) { + SWIG_exception(SWIG_RuntimeError, e.what()); + } catch(...) { + SWIG_exception(SWIG_RuntimeError, "An unknown error occurred"); + } } %extend IfcParse::IfcFile {