From 29a5425adb186ac73ec2b7b436cb9e87be762adb Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 17 Mar 2015 23:29:27 +0000 Subject: [PATCH] Fix more issues in expressions parser. --- src/libutil/expression.c | 6 +++++- src/lua/lua_expression.c | 2 ++ test/lua/unit/expressions.lua | 16 +++++++++++----- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/libutil/expression.c b/src/libutil/expression.c index 2cd1fe53f..3ae7ed469 100644 --- a/src/libutil/expression.c +++ b/src/libutil/expression.c @@ -184,6 +184,9 @@ rspamd_expr_str_to_op (const gchar *a, const gchar *end, const gchar **next) *next = a + 1; } } + else { + *next = end; + } /* XXX: not especially effective */ switch (*a) { case '!': @@ -336,7 +339,7 @@ rspamd_parse_expression (const gchar *line, gsize len, state = SKIP_SPACES; } else if (rspamd_expr_is_operation_symbol (*p)) { - state = PARSE_ATOM; + state = PARSE_OP; } else { /* @@ -480,6 +483,7 @@ rspamd_parse_expression (const gchar *line, gsize len, rspamd_expr_stack_push (e, GINT_TO_POINTER (op)); } + state = SKIP_SPACES; break; case SKIP_SPACES: if (g_ascii_isspace (*p)) { diff --git a/src/lua/lua_expression.c b/src/lua/lua_expression.c index 55b0c1556..284fccb65 100644 --- a/src/lua/lua_expression.c +++ b/src/lua/lua_expression.c @@ -64,6 +64,7 @@ LUA_FUNCTION_DEF (expr, to_string); static const struct luaL_reg exprlib_m[] = { LUA_INTERFACE_DEF (expr, to_string), + {"__tostring", lua_expr_to_string}, {NULL, NULL} }; @@ -231,6 +232,7 @@ lua_expr_create (lua_State *L) } pe = lua_newuserdata (L, sizeof (struct lua_expression *)); + rspamd_lua_setclass (L, "rspamd{expr}", -1); *pe = e; lua_pushnil (L); } diff --git a/test/lua/unit/expressions.lua b/test/lua/unit/expressions.lua index 4c6c55f7d..f191019e6 100644 --- a/test/lua/unit/expressions.lua +++ b/test/lua/unit/expressions.lua @@ -4,11 +4,15 @@ context("Rspamd expressions", function() local rspamd_expression = require "rspamd_expression" local rspamd_mempool = require "rspamd_mempool" local rspamd_regexp = require "rspamd_regexp" - local split_re = rspamd_regexp.create('/\\s+/') + local split_re = rspamd_regexp.create('/\\s+|\\)|\\(/') local function parse_func(str) -- extract token till the first space character - local token = split_re:split(str)[1] + local token = str + local t = split_re:split(str) + if t then + token = t[1] + end -- Return token name return token end @@ -21,15 +25,17 @@ context("Rspamd expressions", function() local pool = rspamd_mempool.create() local cases = { - {'A & B | !C', 'A B & C ! |'} + {'A & B | !C', 'A B & C ! |'}, + {'A & (B | !C)', 'A B C ! | &'} } for _,c in ipairs(cases) do local expr,err = rspamd_expression.create(c[1], {parse_func, process_func}, pool) - if c[2] then - assert_not_nil(expr, "Cannot parse " .. c[1] ": " .. err) + if not c[2] then + assert_nil(expr, "Should not be able to parse " .. c[1]) else + assert_not_nil(expr, "Cannot parse " .. c[1]) assert_equal(expr:to_string(), c[2], string.format("Evaluated expr to '%s', expected: '%s'", expr:to_string(), c[2])) end -- 2.39.5