From 585058b500dfd9f6e4b36825ec604e948b3d7bc7 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 11 Feb 2022 21:02:18 +0100 Subject: [PATCH] pattern: Fixed control flow statements not being applied correctly --- .../include/hex/pattern_language/ast_node.hpp | 11 +++++------ .../include/hex/pattern_language/pattern_language.hpp | 9 ++++++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/libimhex/include/hex/pattern_language/ast_node.hpp b/lib/libimhex/include/hex/pattern_language/ast_node.hpp index 797c2fbbc..bbf96b4a7 100644 --- a/lib/libimhex/include/hex/pattern_language/ast_node.hpp +++ b/lib/libimhex/include/hex/pattern_language/ast_node.hpp @@ -2281,16 +2281,15 @@ namespace hex::pl { FunctionResult execute(Evaluator *evaluator) const override { auto returnValue = this->getReturnValue(); + auto literal = dynamic_cast(returnValue->evaluate(evaluator)); + ON_SCOPE_EXIT { delete literal; }; + evaluator->setCurrentControlFlowStatement(this->m_type); - if (returnValue == nullptr) + if (literal == nullptr) return std::nullopt; - else { - auto literal = dynamic_cast(returnValue->evaluate(evaluator)); - ON_SCOPE_EXIT { delete literal; }; - + else return literal->getValue(); - } } private: diff --git a/lib/libimhex/include/hex/pattern_language/pattern_language.hpp b/lib/libimhex/include/hex/pattern_language/pattern_language.hpp index 0929e213f..5833da708 100644 --- a/lib/libimhex/include/hex/pattern_language/pattern_language.hpp +++ b/lib/libimhex/include/hex/pattern_language/pattern_language.hpp @@ -50,12 +50,15 @@ namespace hex::pl { [[nodiscard]] bool hasDangerousFunctionBeenCalled() const; void allowDangerousFunctions(bool allow); - [[nodiscard]] std::vector &getPatterns() { - return this->m_patterns; + [[nodiscard]] const std::vector &getPatterns() { + const static std::vector empty; + + if (isRunning()) return empty; + else return this->m_patterns; } void reset(); - bool isRunning() const { return this->m_running; } + [[nodiscard]] bool isRunning() const { return this->m_running; } private: Preprocessor *m_preprocessor;