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
* 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