Skip to content

Commit

Permalink
Terminate rep_hist_get_overload_stats_lines() with an NL.
Browse files Browse the repository at this point in the history
We use it in router.c, where chunks are joined with "", not with
NL... so leaving off the terminating NL will lead to an unparseable
extrainfo.

Found by toralf. Bug not in any released Tor.
  • Loading branch information
nmathewson committed Mar 18, 2021
1 parent 411db63 commit cd0e5a9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/feature/stats/rephist.c
Expand Up @@ -233,7 +233,7 @@ rep_hist_get_overload_stats_lines(void)
/* First encode the general overload */
if (overload_happened_recently(overload_stats.overload_general_time, 72)) {
format_iso_time(tbuf, overload_stats.overload_general_time);
smartlist_add_asprintf(chunks, "overload-general %d %s",
smartlist_add_asprintf(chunks, "overload-general %d %s\n",
OVERLOAD_STATS_VERSION, tbuf);
}

Expand All @@ -243,7 +243,7 @@ rep_hist_get_overload_stats_lines(void)
format_iso_time(tbuf, overload_stats.overload_ratelimits_time);
smartlist_add_asprintf(chunks,
"overload-ratelimits %d %s %" PRIu64 " %" PRIu64
" %" PRIu64 " %" PRIu64,
" %" PRIu64 " %" PRIu64 "\n",
OVERLOAD_STATS_VERSION, tbuf,
options->BandwidthRate, options->BandwidthBurst,
overload_stats.overload_read_count,
Expand All @@ -254,7 +254,7 @@ rep_hist_get_overload_stats_lines(void)
if (overload_happened_recently(
overload_stats.overload_fd_exhausted_time, 72)) {
format_iso_time(tbuf, overload_stats.overload_fd_exhausted_time);
smartlist_add_asprintf(chunks, "overload-fd-exhausted %d %s",
smartlist_add_asprintf(chunks, "overload-fd-exhausted %d %s\n",
OVERLOAD_STATS_VERSION, tbuf);
}

Expand All @@ -263,7 +263,7 @@ rep_hist_get_overload_stats_lines(void)
goto done;
}

result = smartlist_join_strings(chunks, "\n", 0, NULL);
result = smartlist_join_strings(chunks, "", 0, NULL);

done:
SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
Expand Down
20 changes: 10 additions & 10 deletions src/test/test_stats.c
Expand Up @@ -728,7 +728,7 @@ test_overload_stats(void *arg)

/* Now check the string */
stats_str = rep_hist_get_overload_stats_lines();
tt_str_op("overload-general 1 2002-01-03 23:00:00", OP_EQ, stats_str);
tt_str_op("overload-general 1 2002-01-03 23:00:00\n", OP_EQ, stats_str);
tor_free(stats_str);

/* Move the time forward 72 hours: see that the line has disappeared. */
Expand All @@ -744,15 +744,15 @@ test_overload_stats(void *arg)
rep_hist_note_overload(OVERLOAD_GENERAL);

stats_str = rep_hist_get_overload_stats_lines();
tt_str_op("overload-general 1 2002-01-07 00:00:00", OP_EQ, stats_str);
tt_str_op("overload-general 1 2002-01-07 00:00:00\n", OP_EQ, stats_str);
tor_free(stats_str);

/* Also note an fd exhaustion event */
rep_hist_note_overload(OVERLOAD_FD_EXHAUSTED);

stats_str = rep_hist_get_overload_stats_lines();
tt_str_op("overload-general 1 2002-01-07 00:00:00\n"
"overload-fd-exhausted 1 2002-01-07 00:00:00", OP_EQ, stats_str);
"overload-fd-exhausted 1 2002-01-07 00:00:00\n", OP_EQ, stats_str);
tor_free(stats_str);

/* Move the time forward. Register DNS overload. See that the time changed */
Expand All @@ -763,7 +763,7 @@ test_overload_stats(void *arg)

stats_str = rep_hist_get_overload_stats_lines();
tt_str_op("overload-general 1 2002-01-07 02:00:00\n"
"overload-fd-exhausted 1 2002-01-07 00:00:00", OP_EQ, stats_str);
"overload-fd-exhausted 1 2002-01-07 00:00:00\n", OP_EQ, stats_str);
tor_free(stats_str);

/* Move the time forward. Register a bandwidth ratelimit event. See that the
Expand All @@ -780,7 +780,7 @@ test_overload_stats(void *arg)
stats_str = rep_hist_get_overload_stats_lines();
tt_str_op("overload-general 1 2002-01-07 02:00:00\n"
"overload-ratelimits 1 2002-01-07 04:00:00 1000 2000 1 0\n"
"overload-fd-exhausted 1 2002-01-07 00:00:00", OP_EQ, stats_str);
"overload-fd-exhausted 1 2002-01-07 00:00:00\n", OP_EQ, stats_str);
tor_free(stats_str);

/* Move the time forward 24 hours: no rate limit line anymore. */
Expand All @@ -789,15 +789,15 @@ test_overload_stats(void *arg)

stats_str = rep_hist_get_overload_stats_lines();
tt_str_op("overload-general 1 2002-01-07 02:00:00\n"
"overload-fd-exhausted 1 2002-01-07 00:00:00", OP_EQ, stats_str);
"overload-fd-exhausted 1 2002-01-07 00:00:00\n", OP_EQ, stats_str);
tor_free(stats_str);

/* Move the time forward 44 hours: no fd exhausted line anymore. */
current_time += 3600*44;
update_approx_time(current_time);

stats_str = rep_hist_get_overload_stats_lines();
tt_str_op("overload-general 1 2002-01-07 02:00:00", OP_EQ, stats_str);
tt_str_op("overload-general 1 2002-01-07 02:00:00\n", OP_EQ, stats_str);
tor_free(stats_str);

/* Move the time forward 2 hours: there is nothing left. */
Expand All @@ -815,7 +815,7 @@ test_overload_stats(void *arg)
* registered ten more overload events, but only one should have been counted
* because of the rate limiter */
stats_str = rep_hist_get_overload_stats_lines();
tt_str_op("overload-ratelimits 1 2002-01-10 02:00:00 1000 2000 2 0",
tt_str_op("overload-ratelimits 1 2002-01-10 02:00:00 1000 2000 2 0\n",
OP_EQ, stats_str);
tor_free(stats_str);

Expand All @@ -828,7 +828,7 @@ test_overload_stats(void *arg)
rep_hist_note_overload(OVERLOAD_READ);
}
stats_str = rep_hist_get_overload_stats_lines();
tt_str_op("overload-ratelimits 1 2002-01-10 02:00:00 1000 2000 2 0",
tt_str_op("overload-ratelimits 1 2002-01-10 02:00:00 1000 2000 2 0\n",
OP_EQ, stats_str);
tor_free(stats_str);

Expand All @@ -842,7 +842,7 @@ test_overload_stats(void *arg)
rep_hist_note_overload(OVERLOAD_WRITE);
}
stats_str = rep_hist_get_overload_stats_lines();
tt_str_op("overload-ratelimits 1 2002-01-10 02:00:00 1000 2000 3 1",
tt_str_op("overload-ratelimits 1 2002-01-10 02:00:00 1000 2000 3 1\n",
OP_EQ, stats_str);
tor_free(stats_str);

Expand Down

0 comments on commit cd0e5a9

Please sign in to comment.