[or-cvs] Use tor_parse_long in more places

Nick Mathewson nickm at seul.org
Tue Oct 12 19:33:06 UTC 2004


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

Modified Files:
	routerparse.c test.c 
Log Message:
Use tor_parse_long in more places

Index: routerparse.c
===================================================================
RCS file: /home/or/cvsroot/src/or/routerparse.c,v
retrieving revision 1.49
retrieving revision 1.50
diff -u -d -r1.49 -r1.50
--- routerparse.c	12 Oct 2004 19:01:53 -0000	1.49
+++ routerparse.c	12 Oct 2004 19:33:03 -0000	1.50
@@ -778,9 +778,9 @@
     router->addr = 0;
 
     if (tok->n_args >= 5) {
-      router->or_port = atoi(tok->args[2]);
-      router->socks_port = atoi(tok->args[3]);
-      router->dir_port = atoi(tok->args[4]);
+      router->or_port = (uint16_t) tor_parse_long(tok->args[2],10,0,65535,NULL,NULL);
+      router->socks_port = (uint16_t) tor_parse_long(tok->args[3],10,0,65535,NULL,NULL);
+      router->dir_port = (uint16_t) tor_parse_long(tok->args[4],10,0,65535,NULL,NULL);
       ports_set = 1;
     }
   } else {
@@ -797,9 +797,9 @@
       log_fn(LOG_WARN,"Wrong # of arguments to \"ports\"");
       goto err;
     }
-    router->or_port = atoi(tok->args[0]);
-    router->socks_port = atoi(tok->args[1]);
-    router->dir_port = atoi(tok->args[2]);
+    router->or_port = tor_parse_long(tok->args[0],10,0,65535,NULL,NULL);
+    router->socks_port = tor_parse_long(tok->args[1],10,0,65535,NULL,NULL);
+    router->dir_port = tor_parse_long(tok->args[2],10,0,65535,NULL,NULL);
     ports_set = 1;
   }
 
@@ -811,7 +811,7 @@
       log_fn(LOG_WARN,"Wrong # of arguments to \"dircacheport\"");
       goto err;
     }
-    router->dir_port = atoi(tok->args[0]);
+    router->dir_port = tor_parse_long(tok->args[0],10,1,65535,NULL,NULL);
   }
 
   tok = find_first_by_keyword(tokens, K_BANDWIDTH);
@@ -824,10 +824,10 @@
       log_fn(LOG_WARN,"Not enough arguments to \"bandwidth\"");
       goto err;
     }
-    router->bandwidthrate = atoi(tok->args[0]);
-    router->bandwidthburst = atoi(tok->args[1]);
+    router->bandwidthrate = tor_parse_long(tok->args[0],10,0,INT_MAX,NULL,NULL);
+    router->bandwidthburst = tor_parse_long(tok->args[1],10,0,INT_MAX,NULL,NULL);
     if(tok->n_args > 2)
-      router->bandwidthcapacity = atoi(tok->args[2]);
+      router->bandwidthcapacity = tor_parse_long(tok->args[2],10,0,INT_MAX,NULL,NULL);
     bw_set = 1;
   }
 
@@ -835,7 +835,7 @@
     if (tok->n_args != 1) {
       log_fn(LOG_WARN, "Unrecognized number of args on K_UPTIME; skipping.");
     } else {
-      router->uptime = atol(tok->args[0]);
+      router->uptime = tor_parse_long(tok->args[0],10,0,LONG_MAX,NULL,NULL);
     }
   }
 
@@ -1003,7 +1003,7 @@
   struct exit_policy_t*newe;
   struct in_addr in;
   char *arg, *address, *mask, *port, *endptr;
-  int bits;
+  int bits, ok;
 
   tor_assert(tok->tp == K_REJECT || tok->tp == K_ACCEPT);
 
@@ -1071,16 +1071,18 @@
     newe->prt_max = 65535;
   } else {
     endptr = NULL;
-    newe->prt_min = (uint16_t) strtol(port, &endptr, 10);
+    newe->prt_min = (uint16_t) tor_parse_long(port, 10, 1, 65535,
+                                              NULL, &endptr);
     if (*endptr == '-') {
       port = endptr+1;
       endptr = NULL;
-      newe->prt_max = (uint16_t) strtol(port, &endptr, 10);
-      if (*endptr) {
+      newe->prt_max = (uint16_t) tor_parse_long(port, 10, 1, 65535, NULL,
+                                                &endptr);
+      if (*endptr || !newe->prt_max) {
       log_fn(LOG_WARN, "Malformed port %s on exit policy; rejecting.",
              port);
       }
-    } else if (*endptr) {
+    } else if (*endptr || !newe->prt_min) {
       log_fn(LOG_WARN, "Malformed port %s on exit policy; rejecting.",
              port);
       goto policy_read_failed;

Index: test.c
===================================================================
RCS file: /home/or/cvsroot/src/or/test.c,v
retrieving revision 1.124
retrieving revision 1.125
diff -u -d -r1.124 -r1.125
--- test.c	12 Oct 2004 16:04:25 -0000	1.124
+++ test.c	12 Oct 2004 19:33:03 -0000	1.125
@@ -664,6 +664,10 @@
   test_eq(u16, 0);
   tor_free(cp);
 
+  /* Test tor_parse_long. */
+  test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
+  test_eq(0L, tor_parse_long("10",10,50,100,NULL,NULL));
+
   /* XXXX test older functions. */
   smartlist_free(sl);
 }
@@ -919,7 +923,7 @@
   ex2.policy_type = EXIT_POLICY_REJECT;
   ex2.addr = 18 << 24;
   ex2.msk = 0xFF000000u;
-  ex2.prt_min = ex1.prt_max = 24;
+  ex2.prt_min = ex2.prt_max = 24;
   ex2.next = NULL;
   r2.address = "tor.tor.tor";
   r2.addr = 0x0a030201u; /* 10.3.2.1 */



More information about the tor-commits mailing list