[or-cvs] r11480: Get rid of a needless malloc() when parsing address policies (in tor/trunk: . doc src/or)

nickm at seul.org nickm at seul.org
Tue Sep 18 15:38:00 UTC 2007


Author: nickm
Date: 2007-09-18 11:38:00 -0400 (Tue, 18 Sep 2007)
New Revision: 11480

Modified:
   tor/trunk/
   tor/trunk/ChangeLog
   tor/trunk/doc/TODO
   tor/trunk/src/or/routerparse.c
Log:
 r15140 at catbus:  nickm | 2007-09-18 11:34:54 -0400
 Get rid of a needless malloc() when parsing address policies.  Original patch from "Some guy on #tor", via arma.  Altered to have a sufficiently large buffer, and not use the buffer so much, and to save a strlcpy.



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r15140] on 8246c3cf-6607-4228-993b-4d95d33730f1

Modified: tor/trunk/ChangeLog
===================================================================
--- tor/trunk/ChangeLog	2007-09-18 14:19:53 UTC (rev 11479)
+++ tor/trunk/ChangeLog	2007-09-18 15:38:00 UTC (rev 11480)
@@ -53,6 +53,7 @@
     - Turn "descriptor store" into a full-fledged type.
     - Move all NT services code into a separate source file.
     - Unify all code that computes medians, percentile elements, etc.
+    - Get rid of a needless malloc when parsing address policies.
 
 
 Changes in version 0.1.2.17 - 2007-08-30

Modified: tor/trunk/doc/TODO
===================================================================
--- tor/trunk/doc/TODO	2007-09-18 14:19:53 UTC (rev 11479)
+++ tor/trunk/doc/TODO	2007-09-18 15:38:00 UTC (rev 11480)
@@ -59,7 +59,7 @@
           o Detect whether votes are really all for the same period.
         . Push/pull documents as appropriate.
           - Pull votes and signatures if we don't get them.
-        - Cache votes and signatures on disk.
+        - Cache votes and signatures on disk?
           o Code to keep consensus docs in limbo if they don't have
             have enough signatures.
         o Have clients know which authorities are v3 authorities, and what

Modified: tor/trunk/src/or/routerparse.c
===================================================================
--- tor/trunk/src/or/routerparse.c	2007-09-18 14:19:53 UTC (rev 11479)
+++ tor/trunk/src/or/routerparse.c	2007-09-18 15:38:00 UTC (rev 11480)
@@ -2232,29 +2232,26 @@
 router_parse_addr_policy_from_string(const char *s, int assume_action)
 {
   directory_token_t *tok = NULL;
-  const char *cp;
-  char *tmp = NULL;
+  const char *cp, *eos;
+  /* Longest possible policy is "accept ffff:ffff:..255/ffff:...255:0-65535".
+   * But note that there can be an arbitrary amount of space between the
+   * accept and the address:mask/port element. */
+  char line[TOR_ADDR_BUF_LEN*2 + 32];
   addr_policy_t *r;
-  size_t len, idx;
-  const char *eos;
 
-  /* *s might not end with \n, so we need to extend it with one. */
-  len = strlen(s);
-  cp = tmp = tor_malloc(len+2);
-  for (idx = 0; idx < len; ++idx) {
-    tmp[idx] = TOR_TOLOWER(s[idx]);
+  s = eat_whitespace(s);
+  if ((*s == '*' || TOR_ISDIGIT(*s)) && assume_action >= 0) {
+    if (tor_snprintf(line, sizeof(line), "%s %s",
+               assume_action == ADDR_POLICY_ACCEPT?"accept":"reject", s)<0) {
+      log_warn(LD_DIR, "Policy %s is too long.", escaped(s));
+      return NULL;
+    }
+    cp = line;
+  } else { /* assume an already well-formed address policy line */
+    cp = s;
   }
-  tmp[len]='\n';
-  tmp[len+1]='\0';
-  while (TOR_ISSPACE(*cp))
-    ++cp;
-  if ((*cp == '*' || TOR_ISDIGIT(*cp)) && assume_action >= 0) {
-    char *new_str = tor_malloc(len+10);
-    tor_snprintf(new_str, len+10, "%s %s\n",
-                 assume_action == ADDR_POLICY_ACCEPT?"accept":"reject", cp);
-    tor_free(tmp);
-    cp = tmp = new_str;
-  }
+  tor_strlower(line);
+
   eos = cp + strlen(cp);
   tok = get_next_token(&cp, eos, routerdesc_token_table);
   if (tok->tp == _ERR) {
@@ -2272,7 +2269,6 @@
  err:
   r = NULL;
  done:
-  tor_free(tmp);
   token_free(tok);
   return r;
 }



More information about the tor-commits mailing list