[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] [tor/master] Fix issues found by Nick in code review.



Author: Mike Perry <mikeperry-git@xxxxxxxxxx>
Date: Sun, 9 Aug 2009 18:21:15 -0700
Subject: Fix issues found by Nick in code review.
Commit: 1060b4d82429c743b6209f7e0a5a2c6bca0a6450

---
 src/or/dirserv.c |   29 ++++++++++++++---------------
 src/or/or.h      |    8 +++-----
 2 files changed, 17 insertions(+), 20 deletions(-)

diff --git a/src/or/dirserv.c b/src/or/dirserv.c
index 1a47173..32ddcd0 100644
--- a/src/or/dirserv.c
+++ b/src/or/dirserv.c
@@ -1943,7 +1943,7 @@ routerstatus_format_entry(char *buf, size_t buf_len,
 
   if (format != NS_V2) {
     routerinfo_t* desc = router_get_by_digest(rs->identity_digest);
-    u_int32_t bw;
+    uint32_t bw;
 
     if (format != NS_CONTROL_PORT) {
       /* Blow up more or less nicely if we didn't get anything or not the
@@ -2270,7 +2270,8 @@ measured_bw_line_parse(measured_bw_line_t *out, const char *orig_line)
   }
 
   do {
-    if (strncasecmp(cp, "bw=", strlen("bw=")) == 0) {
+    if (strcmpstart(cp, "bw=") == 0) {
+      int parse_ok = 0;
       char *endptr;
       if (got_bw) {
         log_warn(LD_DIRSERV, "Double bw= in bandwidth file line: %s",
@@ -2279,17 +2280,16 @@ measured_bw_line_parse(measured_bw_line_t *out, const char *orig_line)
         return -1;
       }
       cp+=strlen("bw=");
-      errno=0;
-      out->bw = strtol(cp, &endptr, 0);
-      if (errno || endptr == cp || (*endptr && !TOR_ISSPACE(*endptr)) ||
-          out->bw < 0) {
+
+      out->bw = tor_parse_long(cp, 0, 0, LONG_MAX, &parse_ok, &endptr);
+      if (!parse_ok || (*endptr && !TOR_ISSPACE(*endptr))) {
         log_warn(LD_DIRSERV, "Invalid bandwidth in bandwidth file line: %s",
                  escaped(orig_line));
         tor_free(line);
         return -1;
       }
       got_bw=1;
-    } else if (strncasecmp(cp, "node_id=$", strlen("node_id=$")) == 0) {
+    } else if (strcmpstart(cp, "node_id=$") == 0) {
       if (got_node_id) {
         log_warn(LD_DIRSERV, "Double node_id= in bandwidth file line: %s",
                  escaped(orig_line));
@@ -2367,9 +2367,8 @@ dirserv_read_measured_bandwidths(const char *from_file,
     return -1;
   }
 
-  fgets(line, sizeof(line), fp);
-
-  if (line[strlen(line)-1] != '\n') {
+  if (!fgets(line, sizeof(line), fp)
+          || !strlen(line) || line[strlen(line)-1] != '\n') {
     log_warn(LD_DIRSERV, "Long or truncated time in bandwidth file: %s",
              escaped(line));
     fclose(fp);
@@ -2397,11 +2396,11 @@ dirserv_read_measured_bandwidths(const char *from_file,
 
   while (!feof(fp)) {
     measured_bw_line_t parsed_line;
-    fgets(line, sizeof(line), fp);
-
-    if (measured_bw_line_parse(&parsed_line, line) != -1) {
-      if (measured_bw_line_apply(&parsed_line, routerstatuses) > 0)
-        applied_lines++;
+    if (fgets(line, sizeof(line), fp) && strlen(line)) {
+      if (measured_bw_line_parse(&parsed_line, line) != -1) {
+        if (measured_bw_line_apply(&parsed_line, routerstatuses) > 0)
+          applied_lines++;
+      }
     }
   }
 
diff --git a/src/or/or.h b/src/or/or.h
index bdbd3e5..f3e639f 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -3544,12 +3544,10 @@ typedef struct measured_bw_line_t {
   long int bw;
 } measured_bw_line_t;
 
-int
-measured_bw_line_parse(measured_bw_line_t *out, const char *line);
+int measured_bw_line_parse(measured_bw_line_t *out, const char *line);
 
-int
-measured_bw_line_apply(measured_bw_line_t *parsed_line,
-                       smartlist_t *routerstatuses);
+int measured_bw_line_apply(measured_bw_line_t *parsed_line,
+                           smartlist_t *routerstatuses);
 #endif
 
 int dirserv_read_measured_bandwidths(const char *from_file,
-- 
1.5.6.5