Handle anychar (.) correctly with bounded repeaat regex expression.
authorPekka Riikonen <priikone@silcnet.org>
Sat, 5 Jan 2008 19:57:25 +0000 (19:57 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Sat, 5 Jan 2008 19:57:25 +0000 (19:57 +0000)
CHANGES.RUNTIME
lib/silcutil/silcregex.c
lib/silcutil/silctypes.h
lib/silcutil/tests/test_silcregex.c

index 0372c00a0d85e60a0037f195ebeca05e1b6b9c8f..b7b5e2f4796cd51689407d7112f8bcafe95bae5e 100644 (file)
@@ -4,6 +4,9 @@ Sat Jan  5 20:19:28 EET 2008  Pekka Riikonen <priikone@silcnet.org>
          deleting data after regex match with SILC_STR_REGEX.  Affected
          files are lib/silcutil/silcbuffmt.[ch].
 
+       * Handle regex anychar (.) with bounded repeat correctly.
+         Affected file is lib/silcutil/silcregex.c.
+
 Sat Jan  5 18:00:06 EET 2008  Pekka Riikonen <priikone@silcnet.org>
 
        * Added silc_buffer_equal to lib/silcutil/silcbuffer.h.
index f13e8483a99042d84f4f9e95f01d89efc415461e..f08c1b61d4247df100ff2688a2e0eb320899fa01 100644 (file)
@@ -1520,7 +1520,7 @@ SilcResult silc_re_compile_pattern(unsigned char *regex, int size,
               Rnormals and one Rplus.  The third is compiled as n-1 Rnormals
               and m-n Rnormals with Roptionals.  0 values have special
               compilation. */
-           int min, max, i;
+           int min, max, i, alen = 2;
 
            if (pos >= size)
              goto normal_char;         /* Consider literal */
@@ -1530,6 +1530,16 @@ SilcResult silc_re_compile_pattern(unsigned char *regex, int size,
              goto normal_char;         /* Consider literal */
            pos -= 2;
            NEXTCHAR(a);
+           if (translate)
+             a = translate[(unsigned char)a];
+           op = silc_regexp_plain_ops[(unsigned char)a];
+
+           if (op == Ranychar) {
+             opcode = Canychar;
+             a = 0;
+             alen = 1;
+           }
+
            NEXTCHAR(ch);
 
            /* Get min value */
@@ -1559,9 +1569,10 @@ SilcResult silc_re_compile_pattern(unsigned char *regex, int size,
              /* Store min - 1 many Cexacts. */
              for (i = 0; i < min - 1; i++) {
                SET_LEVEL_START;
-               ALLOC(2);
-               STORE(Cexact);
-               STORE((unsigned char)a);
+               ALLOC(alen);
+               STORE(opcode);
+               if (a)
+                 STORE((unsigned char)a);
              }
              break;
            }
@@ -1581,9 +1592,10 @@ SilcResult silc_re_compile_pattern(unsigned char *regex, int size,
                /* Store min - 1 many Cexacts. */
                for (i = 0; i < min - 1; i++) {
                  SET_LEVEL_START;
-                 ALLOC(2);
-                 STORE(Cexact);
-                 STORE((unsigned char)a);
+                 ALLOC(alen);
+                 STORE(opcode);
+                 if (a)
+                   STORE((unsigned char)a);
                }
 
                /* Store Rplus */
@@ -1625,17 +1637,19 @@ SilcResult silc_re_compile_pattern(unsigned char *regex, int size,
              /* Store min - 1 many Cexacts. */
              for (i = 0; min && i < min - 1; i++) {
                SET_LEVEL_START;
-               ALLOC(2);
-               STORE(Cexact);
-               STORE((unsigned char)a);
+               ALLOC(alen);
+               STORE(opcode);
+               if (a)
+                 STORE((unsigned char)a);
              }
 
              /* Store max - min Cexacts and Roptionals. */
              for (i = 0; i < max - min; i++) {
                SET_LEVEL_START;
-               ALLOC(2);
-               STORE(Cexact);
-               STORE((unsigned char)a);
+               ALLOC(alen);
+               STORE(opcode);
+               if (a)
+                 STORE((unsigned char)a);
                ALLOC(3);
                INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump,
                            pattern_offset + 3);
index eabb83f032537e6f6906463bcf8d336a55045da8..6b4ca86153e3158345871cfdd7a5279356620c7a 100644 (file)
@@ -313,9 +313,9 @@ typedef SilcUInt32 SilcParam;
 #define SILC_PARAM_REGEX         109
 #define SILC_PARAM_OFFSET_START  110
 #define SILC_PARAM_OFFSET_END    111
-#define SILC_PARAM_APPEND        112
-#define SILC_PARAM_DELETE        113
+#define SILC_PARAM_DELETE        112
 #define SILC_PARAM_ALLOC         0x00010000     /* Allocate, bitmask */
+#define SILC_PARAM_APPEND        0x00020000    /* Append, bitmask */
 
 /* Macros */
 
index b650f6e6e8bd706418dd6cc0aaa8f60550566ffe..41c51ad3fc969a19d036f053b76b48b9946071a3 100644 (file)
@@ -18,6 +18,22 @@ int main(int argc, char **argv)
     silc_log_set_debug_string("*regex*,*errno*");
   }
 
+  regex = ".{5}";
+  SILC_LOG_DEBUG(("Regex %s", regex));
+  string = "abcdefghijklmn";
+  SILC_LOG_DEBUG(("Match %s", string));
+  if (!silc_regex(string, regex, &bmatch, NULL))
+    goto err;
+  silc_buffer_printf(&bmatch, TRUE);
+
+  regex = ".....";
+  SILC_LOG_DEBUG(("Regex %s", regex));
+  string = "abcdefghijklmn";
+  SILC_LOG_DEBUG(("Match %s", string));
+  if (!silc_regex(string, regex, &bmatch, NULL))
+    goto err;
+  silc_buffer_printf(&bmatch, TRUE);
+
   regex = "^a{0}$";
   SILC_LOG_DEBUG(("Regex %s", regex));
   string = "";