From eee94f53a40f08171664e09428981ebc7e3442d9 Mon Sep 17 00:00:00 2001 From: Andrej Date: Wed, 11 Jun 2025 17:42:30 +0500 Subject: [PATCH] get_inverse, get_inverse_indices, get_total_inverses - prevent possible crashes When passing entity without an id (e.g. IfcBaseType) --- src/ifcwrap/IfcParseWrapper.i | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/ifcwrap/IfcParseWrapper.i b/src/ifcwrap/IfcParseWrapper.i index 25a40d3ba6..6b738b2b7a 100644 --- a/src/ifcwrap/IfcParseWrapper.i +++ b/src/ifcwrap/IfcParseWrapper.i @@ -128,17 +128,26 @@ static IfcUtil::ArgumentType helper_fn_attribute_type(const IfcUtil::IfcBaseClas IfcUtil::IfcBaseClass* by_guid(const std::string& guid) { return $self->instance_by_guid(guid); } - + aggregate_of_instance::ptr get_inverse(IfcUtil::IfcBaseClass* e) { - return $self->getInverse(e->as()->id(), 0, -1); + if (auto e_ = e->as()) { + return $self->getInverse(e_->id(), 0, -1); + } + throw IfcParse::IfcException("Only entities with ids are supported for get_inverse. Provided entity: '" + e->declaration().name() + "'."); } std::vector get_inverse_indices(IfcUtil::IfcBaseClass* e) { - return $self->get_inverse_indices(e->as()->id()); + if (auto e_ = e->as()) { + return $self->get_inverse_indices(e_->id()); + } + throw IfcParse::IfcException("Only entities with ids are supported for get_inverse_indices. Provided entity: '" + e->declaration().name() + "'."); } int get_total_inverses(IfcUtil::IfcBaseClass* e) { - return $self->getTotalInverses(e->as()->id()); + if (auto e_ = e->as()) { + return $self->getTotalInverses(e_->id()); + } + throw IfcParse::IfcException("Only entities with ids are supported for get_total_inverses. Provided entity: '" + e->declaration().name() + "'."); } void write(const std::string& fn) {