gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r23290 - gnunet/src/regex


From: gnunet
Subject: [GNUnet-SVN] r23290 - gnunet/src/regex
Date: Fri, 17 Aug 2012 19:33:40 +0200

Author: szengel
Date: 2012-08-17 19:33:40 +0200 (Fri, 17 Aug 2012)
New Revision: 23290

Modified:
   gnunet/src/regex/regex.c
   gnunet/src/regex/test_regex_eval_api.c
Log:
Fixed eval test case and stack smashing protection

Modified: gnunet/src/regex/regex.c
===================================================================
--- gnunet/src/regex/regex.c    2012-08-17 16:25:36 UTC (rev 23289)
+++ gnunet/src/regex/regex.c    2012-08-17 17:33:40 UTC (rev 23290)
@@ -630,7 +630,8 @@
   if (NULL == a || 0 == a->state_count)
     return;
 
-  for (count = 0, s = a->states_head; NULL != s; s = s->next, count++)
+  for (count = 0, s = a->states_head; NULL != s && count < a->state_count;
+       s = s->next, count++)
   {
     s->traversal_id = count;
     marks[s->traversal_id] = GNUNET_NO;
@@ -750,7 +751,7 @@
                                        struct GNUNET_REGEX_Automaton *dfa,
                                        const unsigned int stride_len)
 {
-  struct GNUNET_REGEX_Strided_Context ctx = { stride_len, NULL, NULL};
+  struct GNUNET_REGEX_Strided_Context ctx = { stride_len, NULL, NULL };
   struct GNUNET_REGEX_Transition *t;
   struct GNUNET_REGEX_Transition *t_next;
 

Modified: gnunet/src/regex/test_regex_eval_api.c
===================================================================
--- gnunet/src/regex/test_regex_eval_api.c      2012-08-17 16:25:36 UTC (rev 
23289)
+++ gnunet/src/regex/test_regex_eval_api.c      2012-08-17 17:33:40 UTC (rev 
23290)
@@ -46,7 +46,8 @@
 /**
  * Random regex test. Generate a random regex as well as 'str_count' strings to
  * match it against. Will match using GNUNET_REGEX implementation and compare
- * the result to glibc regex result. 'rx_length' has to be smaller then 
'max_str_len'.
+ * the result to glibc regex result. 'rx_length' has to be smaller then
+ * 'max_str_len'.
  *
  * @param rx_length length of the regular expression.
  * @param max_str_len maximum length of the random strings.
@@ -64,34 +65,33 @@
   int eval;
   int eval_check;
   int eval_canonical;
+  int eval_canonical_check;
   struct GNUNET_REGEX_Automaton *dfa;
   regex_t rx;
   regmatch_t matchptr[1];
   char error[200];
   int result;
-  size_t str_len;
   char *canonical_regex;
 
-  // At least one string is needed for matching
+  /* At least one string is needed for matching */
   GNUNET_assert (str_count > 0);
-  // The string should be at least as long as the regex itself
+  /* The string should be at least as long as the regex itself */
   GNUNET_assert (max_str_len >= rx_length);
 
-  // Generate random regex and a string that matches the regex
+  /* Generate random regex and a string that matches the regex */
   matching_str = GNUNET_malloc (rx_length + 1);
   rand_rx = GNUNET_REGEX_generate_random_regex (rx_length, matching_str);
 
-  // Now match
+  /* Now match */
   result = 0;
   for (i = 0; i < str_count; i++)
   {
     if (0 < i)
     {
       matching_str = GNUNET_REGEX_generate_random_string (max_str_len);
-      str_len = strlen (matching_str);
     }
 
-    // Match string using DFA
+    /* Match string using DFA */
     dfa = GNUNET_REGEX_construct_dfa (rand_rx, strlen (rand_rx));
     if (NULL == dfa)
     {
@@ -100,26 +100,39 @@
     }
 
     eval = GNUNET_REGEX_eval (dfa, matching_str);
+    /* save the canonical regex for later comparison */
     canonical_regex = GNUNET_strdup (GNUNET_REGEX_get_canonical_regex (dfa));
     GNUNET_REGEX_automaton_destroy (dfa);
 
-    // Match string using glibc regex
+    /* Match string using glibc regex */
     if (0 != regcomp (&rx, rand_rx, REG_EXTENDED))
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  "Could not compile regex using regcomp\n");
+                  "Could not compile regex using regcomp: %s\n", rand_rx);
       return -1;
     }
 
     eval_check = regexec (&rx, matching_str, 1, matchptr, 0);
     regfree (&rx);
 
-    // We only want to match the whole string, because that's what our DFA 
does, too.
+    /* We only want to match the whole string, because that's what our DFA 
does,
+     * too. */
     if (eval_check == 0 &&
         (matchptr[0].rm_so != 0 || matchptr[0].rm_eo != strlen (matching_str)))
       eval_check = 1;
 
-    // Match canonical regex
+    /* Match canonical regex */
+    dfa =
+        GNUNET_REGEX_construct_dfa (canonical_regex, strlen (canonical_regex));
+    if (NULL == dfa)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constructing DFA failed\n");
+      return -1;
+    }
+
+    eval_canonical = GNUNET_REGEX_eval (dfa, matching_str);
+    GNUNET_REGEX_automaton_destroy (dfa);
+
     if (0 != regcomp (&rx, canonical_regex, REG_EXTENDED))
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -128,17 +141,24 @@
       return -1;
     }
 
-    eval_canonical = regexec (&rx, matching_str, 1, matchptr, 0);
+    eval_canonical_check = regexec (&rx, matching_str, 1, matchptr, 0);
     regfree (&rx);
-    GNUNET_free (canonical_regex);
 
-    // compare result
-    if (eval_check != eval)
+    /* We only want to match the whole string, because that's what our DFA 
does,
+     * too. */
+    if (eval_canonical_check == 0 &&
+        (matchptr[0].rm_so != 0 || matchptr[0].rm_eo != strlen (matching_str)))
+      eval_canonical_check = 1;
+
+    /* compare results */
+    if (eval_check != eval || eval_canonical != eval_canonical_check)
     {
       regerror (eval_check, &rx, error, sizeof error);
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  "Unexpected result:\nregex: %s\nstring: %s\ngnunet regex: 
%i\nglibc regex: %i\nglibc error: %s\n\n",
-                  rand_rx, matching_str, eval, eval_check, error);
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unexpected result:\nregex: 
%s\ncanonical_regex: %s\n\
+                   string: %s\ngnunet regex: %i\nglibc regex: %i\n\
+                   canonical regex: %i\ncanonical regex glibc: %i\n\
+                   glibc error: %s\n\n", rand_rx, canonical_regex, 
matching_str,
+                  eval, eval_check, eval_canonical, eval_canonical_check, 
error);
       result += 1;
     }
 
@@ -146,6 +166,7 @@
   }
 
   GNUNET_free (rand_rx);
+  GNUNET_free (canonical_regex);
 
   return result;
 }
@@ -157,7 +178,8 @@
  *
  * @param a automaton.
  * @param rx compiled glibc regex.
- * @param rxstr regular expression and strings with expected results to match 
against.
+ * @param rxstr regular expression and strings with expected results to
+ *              match against.
  *
  * @return 0 on successfull, non 0 otherwise
  */
@@ -185,7 +207,8 @@
     eval = GNUNET_REGEX_eval (a, rxstr->strings[i]);
     eval_check = regexec (rx, rxstr->strings[i], 1, matchptr, 0);
 
-    // We only want to match the whole string, because that's what our DFA 
does, too.
+    /* We only want to match the whole string, because that's what our DFA 
does,
+     * too. */
     if (eval_check == 0 &&
         (matchptr[0].rm_so != 0 ||
          matchptr[0].rm_eo != strlen (rxstr->strings[i])))
@@ -198,11 +221,13 @@
       result = 1;
       regerror (eval_check, rx, error, sizeof error);
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  "Unexpected result:\nregex: %s\ncanonical_regex: %s\nstring: 
%s\nexpected result: %i\n"
-                  "gnunet regex: %i\nglibc regex: %i\nglibc error: %s\nrm_so: 
%i\nrm_eo: %i\n\n",
-                  rxstr->regex, GNUNET_REGEX_get_canonical_regex (a),
-                  rxstr->strings[i], rxstr->expected_results[i], eval,
-                  eval_check, error, matchptr[0].rm_so, matchptr[0].rm_eo);
+                  "Unexpected result:\nregex: %s\ncanonical_regex: %s\n"
+                  "string: %s\nexpected result: %i\n"
+                  "gnunet regex: %i\nglibc regex: %i\nglibc error: %s\n"
+                  "rm_so: %i\nrm_eo: %i\n\n", rxstr->regex,
+                  GNUNET_REGEX_get_canonical_regex (a), rxstr->strings[i],
+                  rxstr->expected_results[i], eval, eval_check, error,
+                  matchptr[0].rm_so, matchptr[0].rm_eo);
     }
   }
   return result;
@@ -227,16 +252,18 @@
   int check_rand;
   char *check_proof;
 
-  struct Regex_String_Pair rxstr[16] = {
+  struct Regex_String_Pair rxstr[17] = {
     {"ab?(abcd)?", 5,
      {"ababcd", "abab", "aabcd", "a", "abb"},
      {match, nomatch, match, match, nomatch}},
     {"ab(c|d)+c*(a(b|c)d)+", 5,
-     {"abcdcdcdcdddddabd", "abcd", 
"abcddddddccccccccccccccccccccccccabdacdabd",
+     {"abcdcdcdcdddddabd", "abcd",
+      "abcddddddccccccccccccccccccccccccabdacdabd",
       "abccccca", "abcdcdcdccdabdabd"},
      {match, nomatch, match, nomatch, match}},
     {"ab+c*(a(bx|c)d)+", 5,
-     {"abcdcdcdcdddddabd", "abcd", 
"abcddddddccccccccccccccccccccccccabdacdabd",
+     {"abcdcdcdcdddddabd", "abcd",
+      "abcddddddccccccccccccccccccccccccabdacdabd",
       "abccccca", "abcdcdcdccdabdabd"},
      {nomatch, nomatch, nomatch, nomatch, nomatch}},
     {"a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*", 1,
@@ -255,7 +282,8 @@
      {"", "bla", "blabla", "bl", "la", "b", "l", "a"},
      {match, match, match, nomatch, nomatch, nomatch, nomatch, nomatch}},
     {"ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*", 8,
-     {"ab", "abcabdbla", "abdcccccccccccabcbccdblablabla", "bl", "la", "b", 
"l",
+     {"ab", "abcabdbla", "abdcccccccccccabcbccdblablabla", "bl", "la", "b",
+      "l",
       "a"},
      {nomatch, match, match, nomatch, nomatch, nomatch, nomatch, nomatch}},
     {"a|aa*a", 6,
@@ -278,6 +306,9 @@
      {nomatch, match, match}},
     {"ab(c|d)+c*(a(b|c)d)+", 1,
      {"abacd"},
+     {nomatch}},
+    {"d|5kl", 1,
+     {"d5kl"},
      {nomatch}}
   };
 
@@ -285,7 +316,7 @@
   check_dfa = 0;
   check_rand = 0;
 
-  for (i = 0; i < 16; i++)
+  for (i = 0; i < 17; i++)
   {
     if (0 != regcomp (&rx, rxstr[i].regex, REG_EXTENDED))
     {
@@ -294,12 +325,12 @@
       return 1;
     }
 
-    // NFA test
+    /* NFA test */
     a = GNUNET_REGEX_construct_nfa (rxstr[i].regex, strlen (rxstr[i].regex));
     check_nfa += test_automaton (a, &rx, &rxstr[i]);
     GNUNET_REGEX_automaton_destroy (a);
 
-    // DFA test
+    /* DFA test */
     a = GNUNET_REGEX_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex));
     check_dfa += test_automaton (a, &rx, &rxstr[i]);
     check_proof = GNUNET_strdup (GNUNET_REGEX_get_canonical_regex (a));
@@ -315,9 +346,10 @@
     regfree (&rx);
   }
 
+  /* Random tests */
   srand (time (NULL));
   for (i = 0; i < 50; i++)
-    check_rand += test_random (100, 120, 20);
+    check_rand += test_random (50, 60, 30);
 
   return check_nfa + check_dfa + check_rand;
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]