Refactor dirserv_read_measured_bandwidths to have a single exit point

This commit is contained in:
Nick Mathewson 2018-07-10 10:26:22 -04:00
parent b04d719c10
commit 6574d4bd27

View File

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