[or-cvs] r17812: {tor} Extact parse-the-token-arguments to its own function, and ch (tor/trunk/src/or)

nickm at seul.org nickm at seul.org
Mon Dec 29 16:54:57 UTC 2008


Author: nickm
Date: 2008-12-29 11:54:56 -0500 (Mon, 29 Dec 2008)
New Revision: 17812

Modified:
   tor/trunk/src/or/routerparse.c
Log:
Extact parse-the-token-arguments to its own function, and change it to a single-pass algorithm.  This simplifies the parsing code and speeds it up a little.

Modified: tor/trunk/src/or/routerparse.c
===================================================================
--- tor/trunk/src/or/routerparse.c	2008-12-29 16:54:51 UTC (rev 17811)
+++ tor/trunk/src/or/routerparse.c	2008-12-29 16:54:56 UTC (rev 17812)
@@ -2919,6 +2919,37 @@
   return tok;
 }
 
+/** Helper: parse space-separated arguments from the string <b>s</b> ending at
+ * <b>eol</b>, and store them in the args field of <b>tok</b>.  Store the
+ * number of parsed elements into the n_args field of <b>tok</b>.  Allocate
+ * all storage in <b>area</b>.  Return the number of arguments parsed, or
+ * return -1 if there was an insanely high number of arguments. */
+static INLINE int
+get_token_arguments(memarea_t *area, directory_token_t *tok,
+                    const char *s, const char *eol)
+{
+/** Largest number of arguments we'll accept to any token, ever. */
+#define MAX_ARGS 512
+  char *mem = memarea_strndup(area, s, eol-s);
+  char *cp = mem;
+  int j = 0;
+  char *args[MAX_ARGS];
+  while (*cp) {
+    if (j == MAX_ARGS)
+      return -1;
+    args[j++] = cp;
+    cp = (char*)find_whitespace(cp);
+    if (!cp || !*cp)
+      break; /* End of the line. */
+    *cp++ = '\0';
+    cp = (char*)eat_whitespace(cp);
+  }
+  tok->n_args = j;
+  tok->args = memarea_memdup(area, args, j*sizeof(char*));
+  return j;
+#undef MAX_ARGS
+}
+
 /** Helper function: read the next token from *s, advance *s to the end of the
  * token, and return the parsed token.  Parse *<b>s</b> according to the list
  * of tokens in <b>table</b>.
@@ -2973,31 +3004,10 @@
         tok->n_args = 1;
       } else {
         /* This keyword takes multiple arguments. */
-        /* XXXX021 this code is still too complicated. */
-        char *mem = memarea_strndup(area, *s, eol-*s);
-        char *cp = mem;
-        int j = 0;
-        while (*cp) {
-          j++;
-          cp = (char*)find_whitespace(cp);
-          if (!cp || !*cp)
-            break;
-          cp = (char*)eat_whitespace(cp);
+        if (get_token_arguments(area, tok, *s, eol)<0) {
+          tor_snprintf(ebuf, sizeof(ebuf),"Far too many arguments to %s", kwd);
+          RET_ERR(ebuf);
         }
-        tok->n_args = j;
-        if (tok->n_args) {
-          tok->args = memarea_alloc(area, sizeof(char*)*tok->n_args);
-          cp = mem;
-          j = 0;
-          while (*cp) {
-            tok->args[j++] = cp;
-            cp = (char*)find_whitespace(cp);
-              if (!cp || !*cp)
-                break;
-              *cp++ = '\0';
-              cp = (char*)eat_whitespace(cp);
-          }
-        }
         *s = eol;
       }
       if (tok->n_args < table[i].min_args) {



More information about the tor-commits mailing list