From 024364b922b59fc43308829c8ad3d854cc6778bb Mon Sep 17 00:00:00 2001 From: Artem Chernyshev Date: Sat, 16 Nov 2019 17:37:58 +0300 Subject: [PATCH] Fixed segfault when component creation(mount) throws an error --- src/imvue.cpp | 17 +++++++++++------ src/imvue_element.cpp | 2 +- tests/resources/bad.imv | 18 ++++++++++++++++++ tests/resources/components.xml | 5 ++++- tests/unit/state.cpp | 23 +++++++++++++++++++++++ 5 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 tests/resources/bad.imv diff --git a/src/imvue.cpp b/src/imvue.cpp index ed7a780..ad0f038 100644 --- a/src/imvue.cpp +++ b/src/imvue.cpp @@ -371,13 +371,18 @@ namespace ImVue { void Component::configure(rapidxml::xml_node<>* node, Context* ctx, ScriptState::Context* sctx, Element* parent) { Context* child = newChildContext(ctx); - child->root = this; - if(child->script) { - child->script->initialize(mData); - child->script->lifecycleCallback(ScriptState::BEFORE_CREATE); - } + try { + child->root = this; + if(child->script) { + child->script->initialize(mData); + child->script->lifecycleCallback(ScriptState::BEFORE_CREATE); + } - mScriptState = ctx->script; + mScriptState = ctx->script; + } catch(...) { + delete child; + throw; + } Element::configure(node, child, sctx, parent); if(mConfigured) { fireCallback(ScriptState::CREATED); diff --git a/src/imvue_element.cpp b/src/imvue_element.cpp index ab919f8..8575717 100644 --- a/src/imvue_element.cpp +++ b/src/imvue_element.cpp @@ -516,7 +516,7 @@ namespace ImVue { void Element::fireCallback(ScriptState::LifecycleCallbackType cb, bool schedule) { - if(!mCtx->script) { + if(!mCtx || !mCtx->script) { return; } diff --git a/tests/resources/bad.imv b/tests/resources/bad.imv new file mode 100644 index 0000000..192c6a7 --- /dev/null +++ b/tests/resources/bad.imv @@ -0,0 +1,18 @@ + + + diff --git a/tests/resources/components.xml b/tests/resources/components.xml index a9ead3d..53dedb0 100644 --- a/tests/resources/components.xml +++ b/tests/resources/components.xml @@ -3,7 +3,10 @@ current count {{ self.count }} -