Browse Source

r11670@Kushana: nickm | 2006-12-21 12:23:55 -0500
Clean up logic in parse_port_range(); accept 0 on low end and 65536 on high end for people who are bad at math.


svn:r9169

Nick Mathewson 17 years ago
parent
commit
94d7d8d88f
1 changed files with 22 additions and 15 deletions
  1. 22 15
      src/common/util.c

+ 22 - 15
src/common/util.c

@@ -1745,41 +1745,48 @@ int
 parse_port_range(const char *port, uint16_t *port_min_out,
                  uint16_t *port_max_out)
 {
+  int port_min, port_max, ok;
   tor_assert(port_min_out);
   tor_assert(port_max_out);
 
   if (!port || *port == '\0' || strcmp(port, "*") == 0) {
-    *port_min_out = 1;
-    *port_max_out = 65535;
+    port_min = 1;
+    port_max = 65535;
   } else {
     char *endptr = NULL;
-    *port_min_out = (uint16_t) tor_parse_long(port, 10, 1, 65535,
-                                              NULL, &endptr);
-    if (*endptr == '-' && *port_min_out) {
+    port_min = tor_parse_long(port, 10, 0, 65535, &ok, &endptr);
+    if (!ok) {
+      log_warn(LD_GENERAL,
+               "Malformed port %s on address range; rejecting.",
+               escaped(port));
+      return -1;
+    } else if (endptr && *endptr == '-') {
       port = endptr+1;
       endptr = NULL;
-      *port_max_out = (uint16_t) tor_parse_long(port, 10, 1, 65535, NULL,
-                                                &endptr);
-      if (*endptr || !*port_max_out) {
+      port_max = tor_parse_long(port, 10, 1, 65536, &ok, &endptr);
+      if (!ok) {
         log_warn(LD_GENERAL,
                  "Malformed port %s on address range; rejecting.",
                  escaped(port));
         return -1;
       }
-    } else if (*endptr || !*port_min_out) {
-      log_warn(LD_GENERAL,
-               "Malformed port %s on address range; rejecting.",
-               escaped(port));
-      return -1;
     } else {
-      *port_max_out = *port_min_out;
+      port_max = port_min;
     }
-    if (*port_min_out > *port_max_out) {
+    if (port_min > port_max) {
       log_warn(LD_GENERAL, "Insane port range on address policy; rejecting.");
       return -1;
     }
   }
 
+  if (port_min < 1)
+    port_min = 1;
+  if (port_max > 65535)
+    port_max = 65535;
+
+  *port_min_out = (uint16_t) port_min;
+  *port_max_out = (uint16_t) port_max;
+
   return 0;
 }