From f2a055c85ab6c968b372efe1cbd17f92dec5067d Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 27 Aug 2015 14:47:31 +0200 Subject: [PATCH] Use an AST node for the 'library' keyword fixes #10017 --- lib/config/config_parser.yy | 11 ++--------- lib/config/configcompiler.cpp | 10 ---------- lib/config/configcompiler.hpp | 1 - lib/config/expression.cpp | 14 ++++++++++++++ lib/config/expression.hpp | 12 +++++++++++- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/config/config_parser.yy b/lib/config/config_parser.yy index f23b377be..532a408a1 100644 --- a/lib/config/config_parser.yy +++ b/lib/config/config_parser.yy @@ -324,13 +324,6 @@ lterm_items_inner: lterm %dprec 2 } ; -library: T_LIBRARY T_STRING - { - context->HandleLibrary($2); - free($2); - } - ; - identifier: T_IDENTIFIER | T_STRING ; @@ -436,9 +429,9 @@ combined_set_op: T_SET | T_SET_BINARY_OR ; -lterm: library +lterm: T_LIBRARY rterm { - $$ = MakeLiteral(); // ASTify this + $$ = new LibraryExpression($2, @$); } | rterm combined_set_op rterm { diff --git a/lib/config/configcompiler.cpp b/lib/config/configcompiler.cpp index 05c495091..3deb93f50 100644 --- a/lib/config/configcompiler.cpp +++ b/lib/config/configcompiler.cpp @@ -217,16 +217,6 @@ Expression *ConfigCompiler::HandleIncludeZones(const String& tag, const String& return new DictExpression(expressions); } -/** - * Handles the library directive. - * - * @param library The name of the library. - */ -void ConfigCompiler::HandleLibrary(const String& library) -{ - Loader::LoadExtensionLibrary(library); -} - /** * Compiles a stream. * diff --git a/lib/config/configcompiler.hpp b/lib/config/configcompiler.hpp index 7df8e3951..63f6e0ed8 100644 --- a/lib/config/configcompiler.hpp +++ b/lib/config/configcompiler.hpp @@ -109,7 +109,6 @@ public: Expression *HandleInclude(const String& include, bool search, const DebugInfo& debuginfo = DebugInfo()); Expression *HandleIncludeRecursive(const String& path, const String& pattern, const DebugInfo& debuginfo = DebugInfo()); Expression *HandleIncludeZones(const String& tag, const String& path, const String& pattern, const DebugInfo& debuginfo = DebugInfo()); - void HandleLibrary(const String& library); size_t ReadInput(char *buffer, size_t max_bytes); void *GetScanner(void) const; diff --git a/lib/config/expression.cpp b/lib/config/expression.cpp index 58a65de77..59db84496 100644 --- a/lib/config/expression.cpp +++ b/lib/config/expression.cpp @@ -26,6 +26,7 @@ #include "base/logger.hpp" #include "base/exception.hpp" #include "base/scriptglobal.hpp" +#include "base/loader.hpp" #include #include #include @@ -780,3 +781,16 @@ ExpressionResult ForExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) return VMOps::For(frame, m_FKVar, m_FVVar, valueres.GetValue(), m_Expression, m_DebugInfo); } +ExpressionResult LibraryExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const +{ + if (frame.Sandboxed) + BOOST_THROW_EXCEPTION(ScriptError("Loading libraries is not allowed in sandbox mode.", m_DebugInfo)); + + ExpressionResult libres = m_Operand->Evaluate(frame, dhint); + CHECK_RESULT(libres); + + Loader::LoadExtensionLibrary(libres.GetValue()); + + return Empty; +} + diff --git a/lib/config/expression.hpp b/lib/config/expression.hpp index 6c75e3cfc..202c4bf65 100644 --- a/lib/config/expression.hpp +++ b/lib/config/expression.hpp @@ -288,7 +288,6 @@ protected: Expression *m_Operand2; }; - class I2_CONFIG_API VariableExpression : public DebuggableExpression { public: @@ -885,6 +884,17 @@ private: Expression *m_Expression; }; +class I2_CONFIG_API LibraryExpression : public UnaryExpression +{ +public: + LibraryExpression(Expression *expression, const DebugInfo& debugInfo = DebugInfo()) + : UnaryExpression(expression, debugInfo) + { } + +protected: + virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; +}; + } #endif /* EXPRESSION_H */