[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [tor/maint-0.4.5] Fix timestamp parser in new load_stats_file.
commit 5dd6304f36a9cb9b7f778abe5b84cf6a4e9890a6
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Wed Dec 16 09:58:19 2020 +0100
Fix timestamp parser in new load_stats_file.
The previous parser only considered stats files _starting_ with the
timestamp tag, not stats files having the timestamp tag in a later
position. While this applies to all current stats files, a future
stats file might look differently. Better to fix the function now than
be surprised in another 9 years from now.
This commit also adds a test case for such future stats, and it fixes
stats file paths in newly added unit tests.
---
src/feature/relay/router.c | 4 ++--
src/test/test_stats.c | 26 +++++++++++++++++++++-----
2 files changed, 23 insertions(+), 7 deletions(-)
diff --git a/src/feature/relay/router.c b/src/feature/relay/router.c
index abe23f9aae..e018561556 100644
--- a/src/feature/relay/router.c
+++ b/src/feature/relay/router.c
@@ -3176,12 +3176,12 @@ load_stats_file(const char *filename, const char *ts_tag, time_t now,
goto end;
}
/* Do we have enough for parsing a timestamp? */
- if (strlen(contents) < strlen(ts_tag) + 1 + sizeof(timestr)) {
+ if (strlen(ts_tok) < strlen(ts_tag) + 1 + sizeof(timestr)) {
log_warn(LD_BUG, "Token %s malformed in file %s", ts_tag, filename);
goto end;
}
/* Parse timestamp in order to validate it is not too old. */
- strlcpy(timestr, contents + strlen(ts_tag) + 1, sizeof(timestr));
+ strlcpy(timestr, ts_tok + strlen(ts_tag) + 1, sizeof(timestr));
if (parse_iso_time(timestr, &written) < 0) {
log_warn(LD_BUG, "Token %s has a malformed timestamp in file %s",
ts_tag, filename);
diff --git a/src/test/test_stats.c b/src/test/test_stats.c
index 154acd4f9f..7262477d77 100644
--- a/src/test/test_stats.c
+++ b/src/test/test_stats.c
@@ -499,13 +499,12 @@ static void
test_load_stats_file(void *arg)
{
int ret;
- char *content = NULL, *read_file_content = NULL;
- const char *fname = NULL;
+ char *content = NULL, *read_file_content = NULL, *fname = NULL;
(void) arg;
/* Load conn-stats. */
- fname = get_fname("conn-stats");
+ fname = get_datadir_fname("conn-stats");
tt_assert(fname);
read_file_content = tor_strdup(
"conn-bi-direct 2020-12-13 15:48:53 (86400 s) 12,34,56,78\n"
@@ -516,7 +515,7 @@ test_load_stats_file(void *arg)
tt_str_op(read_file_content, OP_EQ, content);
/* Load hidserv-stats. */
- fname = get_fname("hidserv-stats");
+ fname = get_datadir_fname("hidserv-stats");
tt_assert(fname);
tor_free(read_file_content);
read_file_content = tor_strdup(
@@ -525,6 +524,7 @@ test_load_stats_file(void *arg)
"bin_size=1024\n"
"hidserv-dir-onions-seen 53 delta_f=8 epsilon=0.30 bin_size=8\n");
write_str_to_file(fname, read_file_content, 0);
+ tor_free(fname);
tor_free(content);
ret = load_stats_file("hidserv-stats", "hidserv-stats-end", 1607874000,
&content);
@@ -532,7 +532,7 @@ test_load_stats_file(void *arg)
tt_str_op(read_file_content, OP_EQ, content);
/* Load dirreq-stats. */
- fname = get_fname("dirreq-stats");
+ fname = get_datadir_fname("dirreq-stats");
tt_assert(fname);
tor_free(read_file_content);
read_file_content = tor_strdup(
@@ -546,13 +546,29 @@ test_load_stats_file(void *arg)
"d1=133653,d2=221050,q1=261242,d3=300622,d4=399758,md=539051,d6=721322,"
"d7=959866,q3=1103363,d8=1302035,d9=2046125,max=113404000\n");
write_str_to_file(fname, read_file_content, 0);
+ tor_free(fname);
tor_free(content);
ret = load_stats_file("dirreq-stats", "dirreq-stats-end", 1607874000,
&content);
tt_int_op(ret, OP_EQ, 1);
tt_str_op(read_file_content, OP_EQ, content);
+ /* Attempt to load future-stats file not starting with timestamp tag. */
+ fname = get_datadir_fname("future-stats");
+ tt_assert(fname);
+ tor_free(read_file_content);
+ read_file_content = tor_strdup(
+ "future-stuff-at-file-start\n"
+ "future-stats 2020-12-13 15:48:53 (86400 s)\n");
+ write_str_to_file(fname, read_file_content, 0);
+ tor_free(fname);
+ tor_free(content);
+ ret = load_stats_file("future-stats", "future-stats", 1607874000, &content);
+ tt_int_op(ret, OP_EQ, 1);
+ tt_str_op(read_file_content, OP_EQ, content);
+
done:
+ tor_free(fname);
tor_free(read_file_content);
tor_free(content);
}
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits