diff options
-rw-r--r-- | src/libutil/regexp.c | 6 | ||||
-rw-r--r-- | src/lua/lua_regexp.c | 4 | ||||
-rw-r--r-- | test/lua/unit/regxep.lua | 17 |
3 files changed, 16 insertions, 11 deletions
diff --git a/src/libutil/regexp.c b/src/libutil/regexp.c index 529895b7b..da9884ec1 100644 --- a/src/libutil/regexp.c +++ b/src/libutil/regexp.c @@ -335,7 +335,7 @@ fin: rspamd_regexp_generate_id (pattern, flags, res->id); /* Check number of captures */ - if (pcre_fullinfo (res->re, res->extra, PCRE_INFO_CAPTURECOUNT, + if (pcre_fullinfo (res->raw_re, res->extra, PCRE_INFO_CAPTURECOUNT, &ncaptures) == 0) { res->ncaptures = ncaptures; } @@ -445,9 +445,9 @@ rspamd_regexp_search (rspamd_regexp_t *re, const gchar *text, gsize len, g_assert (g_array_get_element_size (captures) == sizeof (struct rspamd_re_capture)); - g_array_set_size (captures, rc - 1); + g_array_set_size (captures, rc); - for (i = 0; i < rc - 1; i ++) { + for (i = 0; i < rc; i ++) { elt = &g_array_index (captures, struct rspamd_re_capture, i); elt->p = mt + ovec[i * 2]; elt->len = (mt + ovec[i * 2 + 1]) - elt->p; diff --git a/src/lua/lua_regexp.c b/src/lua/lua_regexp.c index 60dbc5edc..2f7ea3ea1 100644 --- a/src/lua/lua_regexp.c +++ b/src/lua/lua_regexp.c @@ -348,14 +348,12 @@ lua_regexp_search (lua_State *L) if (capture) { lua_newtable (L); - lua_pushlstring (L, start, end - start); - lua_rawseti (L, -2, 1); for (capn = 0; capn < captures->len; capn ++) { cap = &g_array_index (captures, struct rspamd_re_capture, capn); lua_pushlstring (L, cap->p, cap->len); - lua_rawseti (L, -2, capn + 2); + lua_rawseti (L, -2, capn + 1); } lua_rawseti (L, -2, ++i); diff --git a/test/lua/unit/regxep.lua b/test/lua/unit/regxep.lua index 8ee26be07..d0b1c422f 100644 --- a/test/lua/unit/regxep.lua +++ b/test/lua/unit/regxep.lua @@ -35,8 +35,13 @@ context("Regexp unit tests", function() test("Regexp capture", function() local cases = { {'Body=(\\S+)(?: Fuz1=(\\S+))?(?: Fuz2=(\\S+))?', - 'mc-filter4 1120; Body=1 Fuz1=1 Fuz2=1', - {'Body=1 Fuz1=1 Fuz2=1', '1', '1', '1'}} + 'mc-filter4 1120; Body=1 Fuz1=2 Fuz2=3', + {'Body=1 Fuz1=2 Fuz2=3', '1', '2', '3'}}, + {'Body=(\\S+)(?: Fuz1=(\\S+))?(?: Fuz2=(\\S+))?', + 'mc-filter4 1120; Body=1 Fuz1=2', {'Body=1 Fuz1=2', '1', '2'}}, + {'Body=(\\S+)(?: Fuz1=(\\S+))?(?: Fuz2=(\\S+))?', + 'mc-filter4 1120; Body=1 Fuz1=2 mc-filter4 1120; Body=1 Fuz1=2 Fuz2=3', + {'Body=1 Fuz1=2', '1', '2'}, {'Body=1 Fuz1=2 Fuz2=3', '1', '2', '3'}}, } for _,c in ipairs(cases) do local r = re.create_cached(c[1]) @@ -45,9 +50,11 @@ context("Regexp unit tests", function() assert_not_nil(res, "cannot find pattern") - for n,m in ipairs(res[1]) do - assert_equal(m, c[3][n], string.format("'%s' doesn't match with '%s'", - c[3][n], c[1])) + for k = 3, table.maxn(c) do + for n,m in ipairs(c[k]) do + assert_equal(res[k - 2][n], c[k][n], string.format("'%s' doesn't match with '%s'", + c[k][n], res[k - 2][n])) + end end end end) |