summaryrefslogtreecommitdiff
path: root/src/feature/dircache/dirserv.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-07-10 10:26:22 -0400
committerNick Mathewson <nickm@torproject.org>2018-07-10 10:36:49 -0400
commit6574d4bd27471c30da934c4d8b82ecbe2b3eab7e (patch)
treec2a425e69aeb2978a375bf2d2cc16477ee643613 /src/feature/dircache/dirserv.c
parentb04d719c1067dd1cf9b48295f1d0e7ed5adb7255 (diff)
downloadtor-6574d4bd27471c30da934c4d8b82ecbe2b3eab7e.tar.gz
tor-6574d4bd27471c30da934c4d8b82ecbe2b3eab7e.zip
Refactor dirserv_read_measured_bandwidths to have a single exit point
Diffstat (limited to 'src/feature/dircache/dirserv.c')
-rw-r--r--src/feature/dircache/dirserv.c23
1 files changed, 12 insertions, 11 deletions
diff --git a/src/feature/dircache/dirserv.c b/src/feature/dircache/dirserv.c
index c5286b0cbf..50869c2b96 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