[or-cvs] Use new split function and strcmpstart correctly

Nick Mathewson nickm at seul.org
Thu Sep 2 18:40:02 UTC 2004


Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/tmp/cvs-serv18548/src/or

Modified Files:
	config.c routerlist.c routerparse.c 
Log Message:
Use new split function and strcmpstart correctly

Index: config.c
===================================================================
RCS file: /home/or/cvsroot/src/or/config.c,v
retrieving revision 1.152
retrieving revision 1.153
diff -u -d -r1.152 -r1.153
--- config.c	24 Aug 2004 21:57:12 -0000	1.152
+++ config.c	2 Sep 2004 18:39:59 -0000	1.153
@@ -154,7 +154,8 @@
     case CONFIG_TYPE_CSV:
       if(*(smartlist_t**)arg == NULL)
         *(smartlist_t**)arg = smartlist_create();
-      smartlist_split_string(*(smartlist_t**)arg, c->value, ",", 1);
+      smartlist_split_string(*(smartlist_t**)arg, c->value, ",",
+                             SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
       break;
     case CONFIG_TYPE_LINELIST:
       /* Note: this reverses the order that the lines appear in.  That's
@@ -980,9 +981,7 @@
                          struct exit_policy_t **dest)
 {
   struct exit_policy_t **nextp;
-  char *e, *s;
-  int last=0;
-  char line[1024];
+  smartlist_t *entries;
 
   if (!cfg)
     return;
@@ -990,30 +989,22 @@
   while (*nextp)
     nextp = &((*nextp)->next);
 
+  entries = smartlist_create();
   for (; cfg; cfg = cfg->next) {
-    s = cfg->value;
-    for (;;) {
-      e = strchr(s,',');
-      if(!e) {
-        last = 1;
-        strncpy(line,s,1023);
-      } else {
-        memcpy(line,s, ((e-s)<1023)?(e-s):1023);
-      line[e-s] = 0;
-      }
-      line[1023]=0;
-      log_fn(LOG_DEBUG,"Adding new entry '%s'",line);
-      *nextp = router_parse_exit_policy_from_string(line);
+    smartlist_split_string(entries,cfg->value,",",SPLIT_SKIP_SPACE,0);
+    SMARTLIST_FOREACH(entries, const char *, ent, {
+      log_fn(LOG_DEBUG,"Adding new entry '%s'",ent);
+      *nextp = router_parse_exit_policy_from_string(ent);
       if(*nextp) {
         nextp = &((*nextp)->next);
       } else {
-        log_fn(LOG_WARN,"Malformed exit policy %s; skipping.", line);
+        log_fn(LOG_WARN,"Malformed exit policy %s; skipping.", ent);
       }
-      if (last)
-        break;
-      s = e+1;
-    }
+    });
+    SMARTLIST_FOREACH(entries, char *, ent, tor_free(ent));
+    smartlist_clear(entries);
   }
+  smartlist_free(entries);
 }
 
 void exit_policy_free(struct exit_policy_t *p) {

Index: routerlist.c
===================================================================
RCS file: /home/or/cvsroot/src/or/routerlist.c,v
retrieving revision 1.133
retrieving revision 1.134
diff -u -d -r1.133 -r1.134
--- routerlist.c	20 Aug 2004 21:34:36 -0000	1.133
+++ routerlist.c	2 Sep 2004 18:39:59 -0000	1.134
@@ -155,25 +155,22 @@
 void
 add_nickname_list_to_smartlist(smartlist_t *sl, const char *list, int warn_if_down)
 {
-  const char *start,*end;
-  char nick[MAX_HEX_NICKNAME_LEN+1];
   routerinfo_t *router;
+  smartlist_t *nickname_list;
 
   tor_assert(sl);
   tor_assert(list);
 
-  while(isspace((int)*list) || *list==',') list++;
+  nickname_list = smartlist_create();
 
-  start = list;
-  while(*start) {
-    end=start; while(*end && !isspace((int)*end) && *end != ',') end++;
-    if (end-start > MAX_HEX_NICKNAME_LEN) {
+  smartlist_split_string(nickname_list, list, ",",
+                         SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
+
+  SMARTLIST_FOREACH(nickname_list, const char *, nick, {
+    if (strlen(nick) > MAX_HEX_NICKNAME_LEN) {
       log_fn(LOG_WARN,"Nickname too long; skipping");
-      start = end;
       continue;
     }
-    memcpy(nick,start,end-start);
-    nick[end-start] = 0; /* null terminate it */
     router = router_get_by_nickname(nick);
     if (router) {
       if (router->is_running)
@@ -184,9 +181,9 @@
     } else
       log_fn(has_fetched_directory ? LOG_WARN : LOG_INFO,
              "Nickname list includes '%s' which isn't a known router.",nick);
-    while(isspace((int)*end) || *end==',') end++;
-    start = end;
-  }
+  });
+  SMARTLIST_FOREACH(nickname_list, char *, nick, tor_free(nick));
+  smartlist_free(nickname_list);
 }
 
 /** Add every router from our routerlist that is currently running to

Index: routerparse.c
===================================================================
RCS file: /home/or/cvsroot/src/or/routerparse.c,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -d -r1.37 -r1.38
--- routerparse.c	25 Aug 2004 17:31:47 -0000	1.37
+++ routerparse.c	2 Sep 2004 18:39:59 -0000	1.38
@@ -172,7 +172,7 @@
   const char *cp = str, *eol;
   int len = strlen(REC);
   cp = str;
-  if (strncmp(str, REC, len)==0) {
+  if (strcmpstart(str, REC)==0) {
     cp += len;
   } else {
     cp = strstr(str, "\n"REC);
@@ -196,10 +196,10 @@
 /* static */ int is_obsolete_version(const char *myversion,
                            const char *versionlist) {
   const char *vl;
-  char *version, *comma, *cp;
   tor_version_t mine, other;
-  int found_newer = 0, r;
+  int found_newer = 0, r, ret;
   static int warned_too_new=0;
+  smartlist_t *version_sl;
 
   vl = versionlist;
 
@@ -209,14 +209,11 @@
     log_fn(LOG_ERR, "I couldn't parse my own version (%s)", myversion);
     tor_assert(0);
   }
+  version_sl = smartlist_create();
+  smartlist_split_string(version_sl, versionlist, ",", SPLIT_SKIP_SPACE, 0);
 
-  for(;;) {
-    comma = strchr(vl, ',');
-    version = tor_strndup(vl, comma?(comma-vl):strlen(vl));
-    cp = version;
-    while (isspace((int)*cp))
-      ++cp;
-    if (!strncmp(cp, "Tor ", 4))
+  SMARTLIST_FOREACH(version_sl, const char *, cp, {
+    if (!strcmpstart(cp, "Tor "))
       cp += 4;
 
     if (tor_version_parse(cp, &other)) {
@@ -224,28 +221,29 @@
     } else {
       r = tor_version_compare(&mine, &other);
       if (r==0) {
-        tor_free(version);
-        return 0; /* It's a match. */
+        ret = 0;
+        goto done;
       } else if (r<0) {
         found_newer = 1;
       }
     }
-    tor_free(version);
-    if (comma)
-      vl = comma+1;
-    else
-      break;
-  }
+  });
+
   if (!found_newer) {
     if (!warned_too_new) {
       log_fn(LOG_WARN, "This version of Tor (%s) is newer than any on the recommended list (%s)",
              myversion, versionlist);
       warned_too_new=1;
     }
-    return 0;
+    ret = 0;
   } else {
-    return 1;
+    ret = 1;
   }
+
+ done:
+  SMARTLIST_FOREACH(version_sl, char *, version, tor_free(version));
+  smartlist_free(version_sl);
+  return ret;
 }
 
 /* Return 0 if myversion is supported; else log a message and return
@@ -1162,13 +1160,13 @@
     }
   }
   *s = eat_whitespace(*s);
-  if (strncmp(*s, "-----BEGIN ", 11)) {
+  if (strcmpstart(*s, "-----BEGIN ")) {
     goto done_tokenizing;
   }
   obstart = *s;
   *s += 11; /* length of "-----BEGIN ". */
   next = strchr(*s, '\n');
-  if (next-*s < 6 || strncmp(next-5, "-----\n", 6)) {
+  if (next-*s < 6 || strcmpstart(next-5, "-----\n")) {
     RET_ERR("Malformed object: bad begin line");
   }
   tok->object_type = tor_strndup(*s, next-*s-5);
@@ -1178,7 +1176,7 @@
     RET_ERR("Malformed object: missing end line");
   }
   if (!strcmp(tok->object_type, "RSA PUBLIC KEY")) {
-    if (strncmp(next, "-----END RSA PUBLIC KEY-----\n", 29))
+    if (strcmpstart(next, "-----END RSA PUBLIC KEY-----\n"))
       RET_ERR("Malformed object: mismatched end line");
     next = strchr(next,'\n')+1;
     tok->key = crypto_new_pk_env();
@@ -1194,7 +1192,7 @@
     tok->object_size = i;
     *s = next + 9; /* length of "-----END ". */
     i = strlen(tok->object_type);
-    if (strncmp(*s, tok->object_type, i) || strncmp(*s+i, "-----\n", 6)) {
+    if (strncmp(*s, tok->object_type, i) || strcmpstart(*s+i, "-----\n")) {
       RET_ERR("Malformed object: mismatched end tag");
     }
     *s += i+6;



More information about the tor-commits mailing list