]> source.dussan.org Git - rspamd.git/commitdiff
Fix issues with capturing patterns
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 30 Jun 2015 17:26:48 +0000 (18:26 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 30 Jun 2015 17:26:48 +0000 (18:26 +0100)
src/libutil/regexp.c
src/lua/lua_regexp.c
test/lua/unit/regxep.lua

index 529895b7b94bceb28e7807e5a9e33fb1bbf84874..da9884ec11264f1b960c3690926ffa6d53f9ccd9 100644 (file)
@@ -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;
index 60dbc5edc33a48d78c89d0d14bd32e83e6484314..2f7ea3ea16b876f8b0dc252130376cd93174ca0f 100644 (file)
@@ -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);
index 8ee26be0761aa49d0af1ba900d6767f8337fe6ec..d0b1c422fa06ac8d7747e7c85c9742cda170bcbb 100644 (file)
@@ -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)