diff options
author | Nick Mathewson <nickm@torproject.org> | 2009-12-17 18:29:37 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2009-12-17 18:29:37 -0500 |
commit | 235f1e1a967cb070c7246617461f58f0413394b3 (patch) | |
tree | 63c260d4d5c4a6f54fa620836ed291a94f6d332e /src/or/geoip.c | |
parent | 498c293afe3570210e964456e6cf5a8f315dbfc6 (diff) | |
download | tor-235f1e1a967cb070c7246617461f58f0413394b3.tar.gz tor-235f1e1a967cb070c7246617461f58f0413394b3.zip |
Refactor out the 'find string at start of any line' logic.
We do this in too many places throughout the code; it's time to start
clamping down.
Also, refactor Karsten's patch to use strchr-then-strndup, rather than
malloc-then-strlcpy-then-strchr-then-clear.
Diffstat (limited to 'src/or/geoip.c')
-rw-r--r-- | src/or/geoip.c | 50 |
1 files changed, 21 insertions, 29 deletions
diff --git a/src/or/geoip.c b/src/or/geoip.c index 535e9affff..d99e472118 100644 --- a/src/or/geoip.c +++ b/src/or/geoip.c @@ -1099,11 +1099,10 @@ parse_bridge_stats_controller(const char *stats_str, time_t now) { char stats_end_str[ISO_TIME_LEN+1], stats_start_str[ISO_TIME_LEN+1], *controller_str, *eos, *eol, *summary; - const char *stats_end_first = "bridge-stats-end ", - *stats_end_middle = "\nbridge-stats-end ", - *ips_first = "bridge-ips", - *ips_middle = "\nbridge-ips", - *tmp; + + const char *BRIDGE_STATS_END = "bridge-stats-end "; + const char *BRIDGE_IPS = "bridge-ips "; + const char *tmp; time_t stats_end_time; size_t controller_len; int seconds; @@ -1111,14 +1110,11 @@ parse_bridge_stats_controller(const char *stats_str, time_t now) /* Parse timestamp and number of seconds from "bridge-stats-end YYYY-MM-DD HH:MM:SS (N s)" */ - if (!strncmp(stats_str, stats_end_first, strlen(stats_end_first))) { - tmp = stats_str + strlen(stats_end_first); - } else { - tmp = strstr(stats_str, stats_end_middle); - if (!tmp) - return NULL; - tmp += strlen(stats_end_middle); - } + tmp = find_str_at_start_of_line(stats_str, BRIDGE_STATS_END); + if (!tmp) + return NULL; + tmp += strlen(BRIDGE_STATS_END); + if (strlen(tmp) < ISO_TIME_LEN + 6) return NULL; strlcpy(stats_end_str, tmp, sizeof(stats_end_str)); @@ -1133,23 +1129,19 @@ parse_bridge_stats_controller(const char *stats_str, time_t now) format_iso_time(stats_start_str, stats_end_time - seconds); /* Parse: "bridge-ips CC=N,CC=N,..." */ - if (!strncmp(stats_str, ips_first, strlen(ips_first))) { - tmp = stats_str + strlen(ips_first); - } else { - tmp = strstr(stats_str, ips_middle); - if (!tmp) - return NULL; - tmp += strlen(ips_middle); - } - if (strlen(tmp) > 2 && !strncmp(tmp, " ", 1)) - tmp += 1; - else - tmp = ""; - summary = tor_malloc(strlen(tmp) + 1); - strlcpy(summary, tmp, strlen(tmp) + 1); - eol = strstr(summary, "\n"); + tmp = find_str_at_start_of_line(stats_str, BRIDGE_IPS); + if (!tmp) + return NULL; + tmp += strlen(BRIDGE_IPS); + + tmp = eat_whitespace_no_nl(tmp); + + eol = strchr(tmp, '\n'); if (eol) - eol[0] = '\0'; + summary = tor_strndup(tmp, eol-tmp); + else + summary = tor_strdup(tmp); + controller_len = strlen("TimeStarted=\"\" CountrySummary=") + strlen(summary) + 42; controller_str = tor_malloc(controller_len); |