[tor-commits] [tor/master] Refactor dirserv_read_measured_bandwidths to have a single exit point

nickm at torproject.org nickm at torproject.org
Wed Jul 11 00:20:10 UTC 2018


commit 6574d4bd27471c30da934c4d8b82ecbe2b3eab7e
Author: Nick Mathewson <nickm at torproject.org>
Date:   Tue Jul 10 10:26:22 2018 -0400

    Refactor dirserv_read_measured_bandwidths to have a single exit point
---
 src/feature/dircache/dirserv.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/src/feature/dircache/dirserv.c b/src/feature/dircache/dirserv.c
index c5286b0cb..50869c2b9 100644
--- a/src/feature/dircache/dirserv.c
+++ b/src/feature/dircache/dirserv.c
@@ -2616,6 +2616,7 @@ dirserv_read_measured_bandwidths(const char *from_file,
    * if there are additional header lines, as introduced in Bandwidth List spec
    * version 1.1.0 */
   int line_is_after_headers = 0;
+  int rv = -1;
 
   /* Initialise line, so that we can't possibly run off the end. */
   memset(line, 0, sizeof(line));
@@ -2623,21 +2624,19 @@ dirserv_read_measured_bandwidths(const char *from_file,
   if (fp == NULL) {
     log_warn(LD_CONFIG, "Can't open bandwidth file at configured location: %s",
              from_file);
-    return -1;
+    goto err;
   }
 
   /* If fgets fails, line is either unmodified, or indeterminate. */
   if (!fgets(line, sizeof(line), fp)) {
     log_warn(LD_DIRSERV, "Empty bandwidth file");
-    fclose(fp);
-    return -1;
+    goto err;
   }
 
   if (!strlen(line) || line[strlen(line)-1] != '\n') {
     log_warn(LD_DIRSERV, "Long or truncated time in bandwidth file: %s",
              escaped(line));
-    fclose(fp);
-    return -1;
+    goto err;
   }
 
   line[strlen(line)-1] = '\0';
@@ -2645,16 +2644,14 @@ dirserv_read_measured_bandwidths(const char *from_file,
   if (!ok) {
     log_warn(LD_DIRSERV, "Non-integer time in bandwidth file: %s",
              escaped(line));
-    fclose(fp);
-    return -1;
+    goto err;
   }
 
   now = time(NULL);
   if ((now - file_time) > MAX_MEASUREMENT_AGE) {
     log_warn(LD_DIRSERV, "Bandwidth measurement file stale. Age: %u",
              (unsigned)(time(NULL) - file_time));
-    fclose(fp);
-    return -1;
+    goto err;
   }
 
   if (routerstatuses)
@@ -2679,11 +2676,15 @@ dirserv_read_measured_bandwidths(const char *from_file,
   /* Now would be a nice time to clean the cache, too */
   dirserv_expire_measured_bw_cache(now);
 
-  fclose(fp);
   log_info(LD_DIRSERV,
            "Bandwidth measurement file successfully read. "
            "Applied %d measurements.", applied_lines);
-  return 0;
+  rv = 0;
+
+ err:
+  if (fp)
+    fclose(fp);
+  return rv;
 }
 
 /** As dirserv_get_routerdescs(), but instead of getting signed_descriptor_t





More information about the tor-commits mailing list