From b1a7978b2d29cbbe2ed9d2ebc3a3d30be090be6a Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 23 Nov 2019 14:27:28 +0000 Subject: [PATCH] [Minor] Allow rspamd_text matching in lpeg --- contrib/lua-lpeg/lptree.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/contrib/lua-lpeg/lptree.c b/contrib/lua-lpeg/lptree.c index cb8342459..ffc03f770 100644 --- a/contrib/lua-lpeg/lptree.c +++ b/contrib/lua-lpeg/lptree.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "lua.h" @@ -1155,9 +1156,25 @@ static int lp_match (lua_State *L) { #endif const char *r; size_t l; + const char *s; + Pattern *p = (getpatt(L, 1, NULL), getpattern(L, 1)); Instruction *code = (p->code != NULL) ? p->code : prepcompile(L, p, 1); - const char *s = luaL_checklstring(L, SUBJIDX, &l); + + if (lua_type (L, SUBJIDX) == LUA_TSTRING) { + s = luaL_checklstring (L, SUBJIDX, &l); + } + else if (lua_type (L, SUBJIDX) == LUA_TUSERDATA) { + struct rspamd_lua_text *t = lua_check_text (L, SUBJIDX); + if (!t) { + return luaL_error (L, "invalid argument (not a text)"); + } + s = t->start; + l = t->len; + } + else { + return luaL_error (L, "invalid argument"); + } size_t i = initposition(L, l); int ptop = lua_gettop(L), rs; lua_pushnil(L); /* initialize subscache */ -- 2.39.5