From 9b1c09818ceb27eade66aa2a00689f7908caf5f0 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 17 Oct 2021 21:49:33 +0200 Subject: [PATCH] patterns: Fixed accessing global scope items through the parent keyword --- .../include/hex/pattern_language/ast_node.hpp | 18 +++++++++++++++--- .../include/hex/pattern_language/evaluator.hpp | 7 ++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/plugins/libimhex/include/hex/pattern_language/ast_node.hpp b/plugins/libimhex/include/hex/pattern_language/ast_node.hpp index 7bb8741f1..d372031f9 100644 --- a/plugins/libimhex/include/hex/pattern_language/ast_node.hpp +++ b/plugins/libimhex/include/hex/pattern_language/ast_node.hpp @@ -1526,13 +1526,19 @@ namespace hex::pl { if (name == "parent") { scopeIndex--; + + if (-scopeIndex >= evaluator->getScopeCount()) + LogConsole::abortEvaluation("cannot access parent of global scope", this); + searchScope = *evaluator->getScope(scopeIndex).scope; auto currParent = evaluator->getScope(scopeIndex).parent; - if (currParent == nullptr) - LogConsole::abortEvaluation("no parent available", this); + if (currParent == nullptr) { + currPattern = nullptr; + } else { + currPattern = currParent->clone(); + } - currPattern = currParent->clone(); continue; } else if (name == "this") { searchScope = *evaluator->getScope(scopeIndex).scope; @@ -1592,6 +1598,9 @@ namespace hex::pl { }, index->getValue()); } + if (currPattern == nullptr) + break; + if (auto pointerPattern = dynamic_cast(currPattern)) { auto newPattern = pointerPattern->getPointedAtPattern()->clone(); delete currPattern; @@ -1611,6 +1620,9 @@ namespace hex::pl { } + if (currPattern == nullptr) + LogConsole::abortEvaluation("cannot reference global scope", this); + return { currPattern }; } diff --git a/plugins/libimhex/include/hex/pattern_language/evaluator.hpp b/plugins/libimhex/include/hex/pattern_language/evaluator.hpp index 828035272..8d5e2c8d8 100644 --- a/plugins/libimhex/include/hex/pattern_language/evaluator.hpp +++ b/plugins/libimhex/include/hex/pattern_language/evaluator.hpp @@ -45,9 +45,6 @@ namespace hex::pl { } const Scope& getScope(s32 index) { - static Scope empty; - - if (index > 0 || -index >= this->m_scopes.size()) return empty; return this->m_scopes[this->m_scopes.size() - 1 + index]; } @@ -55,6 +52,10 @@ namespace hex::pl { return this->m_scopes.front(); } + size_t getScopeCount() { + return this->m_scopes.size(); + } + bool isGlobalScope() { return this->m_scopes.size() == 1; }