diff --git a/src/feature/dircache/dirserv.c b/src/feature/dircache/dirserv.c
index e7b5239e0b..c3ccc3c8ff 100644
--- a/src/feature/dircache/dirserv.c
+++ b/src/feature/dircache/dirserv.c
@@ -2600,13 +2600,13 @@ measured_bw_line_apply(measured_bw_line_t *parsed_line,
/**
* Read the measured bandwidth list file, apply it to the list of
- * vote_routerstatus_t and store all the headers in bwlist_headers.
+ * vote_routerstatus_t and store all the headers in bw_file_headers.
* Returns -1 on error, 0 otherwise.
*/
int
dirserv_read_measured_bandwidths(const char *from_file,
smartlist_t *routerstatuses,
- smartlist_t *bwlist_headers)
+ smartlist_t *bw_file_headers)
{
FILE *fp = tor_fopen_cloexec(from_file, "r");
int applied_lines = 0;
@@ -2656,7 +2656,11 @@ dirserv_read_measured_bandwidths(const char *from_file,
goto err;
}
- smartlist_add_asprintf(bwlist_headers, "timestamp=%ld", file_time);
+ /* If timestamp was correct and bw_file_headers is not NULL,
+ * add timestamp to bw_file_headers */
+ if (bw_file_headers)
+ smartlist_add_asprintf(bw_file_headers, "timestamp=%lu",
+ (unsigned long)file_time);
if (routerstatuses)
smartlist_sort(routerstatuses, compare_vote_routerstatus_entries);
@@ -2673,15 +2677,22 @@ dirserv_read_measured_bandwidths(const char *from_file,
dirserv_cache_measured_bw(&parsed_line, file_time);
if (measured_bw_line_apply(&parsed_line, routerstatuses) > 0)
applied_lines++;
- } else {
- /* If line does not contain the header separator and it is key_value,
- * it is probably a KeyValue header.*/
- if (strcmp(line, "====\n") != 0 &&
- string_is_key_value(LOG_DEBUG, line)) {
- line[strlen(line)-1] = '\0';
- smartlist_add_strdup(bwlist_headers, line);
- };
- }
+ /* if the terminator is found, it is the end of header lines, set the
+ * flag but do not store anything */
+ } else if (strcmp(line, BW_FILE_TERMINATOR) == 0)
+ line_is_after_headers = 1;
+ /* if the line was not a correct relay line nor the terminator and
+ * the end of the header lines has not been detected yet
+ * and it is key_value and bw_file_headers did not reach the maximum
+ * number of headers,
+ * then assume this line is a header and add it to bw_file_headers */
+ else if (bw_file_headers &&
+ (line_is_after_headers == 0) &&
+ string_is_key_value(LOG_DEBUG, line) &&
+ (smartlist_len(bw_file_headers) < MAX_BW_FILE_HEADERS_LEN)) {
+ line[strlen(line)-1] = '\0';
+ smartlist_add_strdup(bw_file_headers, line);
+ };
}
}