diff options
author | Nick Mathewson <nickm@torproject.org> | 2012-01-11 14:02:59 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2012-01-16 15:03:13 -0500 |
commit | cc02823d7f6acbc3fa8ea87e5646921100796f10 (patch) | |
tree | 45d4ba4a4cc9ef73c076f08934f68d9690b9044c /src/or | |
parent | edcc9981d8b8894d2ef4e0d617a20d7d99547817 (diff) | |
download | tor-cc02823d7f6acbc3fa8ea87e5646921100796f10.tar tor-cc02823d7f6acbc3fa8ea87e5646921100796f10.tar.gz |
Convert instances of tor_snprintf+strdup into tor_asprintf
These were found by looking for tor_snprintf() instances that were
followed closely by tor_strdup(), though I probably converted some
other snprintfs as well.
Diffstat (limited to 'src/or')
-rw-r--r-- | src/or/circuitbuild.c | 4 | ||||
-rw-r--r-- | src/or/config.c | 24 | ||||
-rw-r--r-- | src/or/connection_edge.c | 5 | ||||
-rw-r--r-- | src/or/control.c | 31 | ||||
-rw-r--r-- | src/or/directory.c | 33 | ||||
-rw-r--r-- | src/or/dirserv.c | 4 | ||||
-rw-r--r-- | src/or/dirvote.c | 38 | ||||
-rw-r--r-- | src/or/rephist.c | 6 | ||||
-rw-r--r-- | src/or/router.c | 7 |
9 files changed, 41 insertions, 111 deletions
diff --git a/src/or/circuitbuild.c b/src/or/circuitbuild.c index 216c92658..88916d659 100644 --- a/src/or/circuitbuild.c +++ b/src/or/circuitbuild.c @@ -4441,10 +4441,8 @@ entry_guards_update_state(or_state_t *state) continue; /* don't write this one to disk */ *next = line = tor_malloc_zero(sizeof(config_line_t)); line->key = tor_strdup("EntryGuard"); - line->value = tor_malloc(HEX_DIGEST_LEN+MAX_NICKNAME_LEN+2); base16_encode(dbuf, sizeof(dbuf), e->identity, DIGEST_LEN); - tor_snprintf(line->value,HEX_DIGEST_LEN+MAX_NICKNAME_LEN+2, - "%s %s", e->nickname, dbuf); + tor_asprintf(&line->value, "%s %s", e->nickname, dbuf); next = &(line->next); if (e->unreachable_since) { *next = line = tor_malloc_zero(sizeof(config_line_t)); diff --git a/src/or/config.c b/src/or/config.c index d4a4e0349..a477a096b 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -781,22 +781,20 @@ set_options(or_options_t *new_val, char **msg) extern const char tor_git_revision[]; /* from tor_main.c */ /** The version of this Tor process, as parsed. */ -static char *_version = NULL; +static char *the_tor_version = NULL; /** Return the current Tor version. */ const char * get_version(void) { - if (_version == NULL) { + if (the_tor_version == NULL) { if (strlen(tor_git_revision)) { - size_t len = strlen(VERSION)+strlen(tor_git_revision)+16; - _version = tor_malloc(len); - tor_snprintf(_version, len, "%s (git-%s)", VERSION, tor_git_revision); + tor_asprintf(&the_tor_version, "%s (git-%s)", VERSION, tor_git_revision); } else { - _version = tor_strdup(VERSION); + the_tor_version = tor_strdup(VERSION); } } - return _version; + return the_tor_version; } /** Release additional memory allocated in options @@ -841,7 +839,7 @@ config_free_all(void) tor_free(torrc_fname); tor_free(torrc_defaults_fname); - tor_free(_version); + tor_free(the_tor_version); tor_free(global_dirfrontpagecontents); } @@ -1174,9 +1172,8 @@ options_act_reversible(const or_options_t *old_options, char **msg) control_ports_write_to_file(); if (directory_caches_v2_dir_info(options)) { - size_t len = strlen(options->DataDirectory)+32; - char *fn = tor_malloc(len); - tor_snprintf(fn, len, "%s"PATH_SEPARATOR"cached-status", + char *fn = NULL; + tor_asprintf(&fn, "%s"PATH_SEPARATOR"cached-status", options->DataDirectory); if (check_private_dir(fn, running_tor ? CPD_CREATE : CPD_CHECK, options->User) < 0) { @@ -3506,11 +3503,8 @@ options_validate(or_options_t *old_options, or_options_t *options, SMARTLIST_FOREACH(options->FirewallPorts, const char *, portno, { int p = atoi(portno); - char *s; if (p<0) continue; - s = tor_malloc(16); - tor_snprintf(s, 16, "*:%d", p); - smartlist_add(instead, s); + smartlist_add_asprintf(instead, "*:%d", p); }); new_line->value = smartlist_join_strings(instead,",",0,NULL); /* These have been deprecated since 0.1.1.5-alpha-cvs */ diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c index aba9ba272..7de627d72 100644 --- a/src/or/connection_edge.c +++ b/src/or/connection_edge.c @@ -1165,11 +1165,10 @@ addressmap_rewrite(char *address, size_t maxlen, time_t *expires_out) static int addressmap_rewrite_reverse(char *address, size_t maxlen, time_t *expires_out) { - size_t len = maxlen + 16; - char *s = tor_malloc(len), *cp; + char *s, *cp; addressmap_entry_t *ent; int r = 0; - tor_snprintf(s, len, "REVERSE[%s]", address); + tor_asprintf(&s, "REVERSE[%s]", address); ent = strmap_get(addressmap, s); if (ent) { cp = tor_strdup(escaped_safe_str_client(ent->new_address)); diff --git a/src/or/control.c b/src/or/control.c index 7bb11cd8c..70bdadbb3 100644 --- a/src/or/control.c +++ b/src/or/control.c @@ -697,7 +697,6 @@ control_setconf_helper(control_connection_t *conn, uint32_t len, char *body, if (*eq == '=') { char *val=NULL; size_t val_len=0; - size_t ent_len; if (*body != '\"') { char *val_start = body; while (!TOR_ISSPACE(*body)) @@ -715,9 +714,7 @@ control_setconf_helper(control_connection_t *conn, uint32_t len, char *body, return 0; } } - ent_len = strlen(key)+val_len+3; - entry = tor_malloc(ent_len+1); - tor_snprintf(entry, ent_len, "%s %s", key, val); + tor_asprintf(&entry, "%s %s", key, val); tor_free(key); tor_free(val); } else { @@ -1304,12 +1301,9 @@ handle_control_mapaddress(control_connection_t *conn, uint32_t len, if (smartlist_len(elts) == 2) { const char *from = smartlist_get(elts,0); const char *to = smartlist_get(elts,1); - size_t anslen = strlen(line)+512; - char *ans = tor_malloc(anslen); if (address_is_invalid_destination(to, 1)) { - tor_snprintf(ans, anslen, + smartlist_add_asprintf(reply, "512-syntax error: invalid address '%s'", to); - smartlist_add(reply, ans); log_warn(LD_CONTROL, "Skipping invalid argument '%s' in MapAddress msg", to); } else if (!strcmp(from, ".") || !strcmp(from, "0.0.0.0")) { @@ -1317,28 +1311,22 @@ handle_control_mapaddress(control_connection_t *conn, uint32_t len, !strcmp(from,".") ? RESOLVED_TYPE_HOSTNAME : RESOLVED_TYPE_IPV4, tor_strdup(to)); if (!address) { - tor_snprintf(ans, anslen, + smartlist_add_asprintf(reply, "451-resource exhausted: skipping '%s'", line); - smartlist_add(reply, ans); log_warn(LD_CONTROL, "Unable to allocate address for '%s' in MapAddress msg", safe_str_client(line)); } else { - tor_snprintf(ans, anslen, "250-%s=%s", address, to); - smartlist_add(reply, ans); + smartlist_add_asprintf(reply, "250-%s=%s", address, to); } } else { addressmap_register(from, tor_strdup(to), 1, ADDRMAPSRC_CONTROLLER, 0, 0); - tor_snprintf(ans, anslen, "250-%s", line); - smartlist_add(reply, ans); + smartlist_add_asprintf(reply, "250-%s", line); } } else { - size_t anslen = strlen(line)+256; - char *ans = tor_malloc(anslen); - tor_snprintf(ans, anslen, "512-syntax error: mapping '%s' is " + smartlist_add_asprintf(reply, "512-syntax error: mapping '%s' is " "not of expected form 'foo=bar'.", line); - smartlist_add(reply, ans); log_info(LD_CONTROL, "Skipping MapAddress '%s': wrong " "number of items.", safe_str_client(line)); @@ -2012,8 +2000,7 @@ getinfo_helper_events(control_connection_t *control_conn, } else if (!strcmp(question, "status/reachability-succeeded/dir")) { *answer = tor_strdup(check_whether_dirport_reachable() ? "1" : "0"); } else if (!strcmp(question, "status/reachability-succeeded")) { - *answer = tor_malloc(16); - tor_snprintf(*answer, 16, "OR=%d DIR=%d", + tor_asprintf(answer, "OR=%d DIR=%d", check_whether_orport_reachable() ? 1 : 0, check_whether_dirport_reachable() ? 1 : 0); } else if (!strcmp(question, "status/bootstrap-phase")) { @@ -2049,9 +2036,7 @@ getinfo_helper_events(control_connection_t *control_conn, } } else if (!strcmp(question, "status/version/num-versioning") || !strcmp(question, "status/version/num-concurring")) { - char s[33]; - tor_snprintf(s, sizeof(s), "%d", get_n_authorities(V3_DIRINFO)); - *answer = tor_strdup(s); + tor_asprintf(answer, "%d", get_n_authorities(V3_DIRINFO)); log_warn(LD_GENERAL, "%s is deprecated; it no longer gives useful " "information", question); } diff --git a/src/or/directory.c b/src/or/directory.c index 8087d7603..572091a16 100644 --- a/src/or/directory.c +++ b/src/or/directory.c @@ -1125,7 +1125,6 @@ directory_send_command(dir_connection_t *conn, char *url; char request[8192]; const char *httpcommand = NULL; - size_t len; tor_assert(conn); tor_assert(conn->_base.type == CONN_TYPE_DIR); @@ -1174,9 +1173,7 @@ directory_send_command(dir_connection_t *conn, case DIR_PURPOSE_FETCH_V2_NETWORKSTATUS: tor_assert(resource); httpcommand = "GET"; - len = strlen(resource)+32; - url = tor_malloc(len); - tor_snprintf(url, len, "/tor/status/%s", resource); + tor_asprintf(&url, "/tor/status/%s", resource); break; case DIR_PURPOSE_FETCH_CONSENSUS: /* resource is optional. If present, it's a flavor name */ @@ -1191,17 +1188,13 @@ directory_send_command(dir_connection_t *conn, tor_assert(resource); tor_assert(!payload); httpcommand = "GET"; - len = strlen(resource)+32; - url = tor_malloc(len); - tor_snprintf(url, len, "/tor/keys/%s", resource); + tor_asprintf(&url, "/tor/keys/%s", resource); break; case DIR_PURPOSE_FETCH_STATUS_VOTE: tor_assert(resource); tor_assert(!payload); httpcommand = "GET"; - len = strlen(resource)+32; - url = tor_malloc(len); - tor_snprintf(url, len, "/tor/status-vote/next/%s.z", resource); + tor_asprintf(&url, "/tor/status-vote/next/%s.z", resource); break; case DIR_PURPOSE_FETCH_DETACHED_SIGNATURES: tor_assert(!resource); @@ -1212,16 +1205,12 @@ directory_send_command(dir_connection_t *conn, case DIR_PURPOSE_FETCH_SERVERDESC: tor_assert(resource); httpcommand = "GET"; - len = strlen(resource)+32; - url = tor_malloc(len); - tor_snprintf(url, len, "/tor/server/%s", resource); + tor_asprintf(&url, "/tor/server/%s", resource); break; case DIR_PURPOSE_FETCH_EXTRAINFO: tor_assert(resource); httpcommand = "GET"; - len = strlen(resource)+32; - url = tor_malloc(len); - tor_snprintf(url, len, "/tor/extra/%s", resource); + tor_asprintf(&url, "/tor/extra/%s", resource); break; case DIR_PURPOSE_FETCH_MICRODESC: tor_assert(resource); @@ -1256,9 +1245,7 @@ directory_send_command(dir_connection_t *conn, tor_assert(strlen(resource) <= REND_DESC_ID_V2_LEN_BASE32); tor_assert(!payload); httpcommand = "GET"; - len = strlen(resource) + 32; - url = tor_malloc(len); - tor_snprintf(url, len, "/tor/rendezvous2/%s", resource); + tor_asprintf(&url, "/tor/rendezvous2/%s", resource); break; case DIR_PURPOSE_UPLOAD_RENDDESC: tor_assert(!resource); @@ -2519,7 +2506,6 @@ char * directory_dump_request_log(void) { smartlist_t *lines; - char tmp[256]; char *result; strmap_iter_t *iter; @@ -2535,9 +2521,8 @@ directory_dump_request_log(void) request_t *r; strmap_iter_get(iter, &key, &val); r = val; - tor_snprintf(tmp, sizeof(tmp), "%s "U64_FORMAT" "U64_FORMAT"\n", + smartlist_add_asprintf(lines, "%s "U64_FORMAT" "U64_FORMAT"\n", key, U64_PRINTF_ARG(r->bytes), U64_PRINTF_ARG(r->count)); - smartlist_add(lines, tor_strdup(tmp)); } smartlist_sort_strings(lines); result = smartlist_join_strings(lines, "", 0, NULL); @@ -3291,8 +3276,7 @@ directory_handle_command_get(dir_connection_t *conn, const char *headers, #if defined(EXPORTMALLINFO) && defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO) #define ADD_MALLINFO_LINE(x) do { \ - tor_snprintf(tmp, sizeof(tmp), "%s %d\n", #x, mi.x); \ - smartlist_add(lines, tor_strdup(tmp)); \ + smartlist_add_asprintf(lines, "%s %d\n", #x, mi.x); \ }while(0); if (!strcmp(url,"/tor/mallinfo.txt") && @@ -3301,7 +3285,6 @@ directory_handle_command_get(dir_connection_t *conn, const char *headers, size_t len; struct mallinfo mi; smartlist_t *lines; - char tmp[256]; memset(&mi, 0, sizeof(mi)); mi = mallinfo(); diff --git a/src/or/dirserv.c b/src/or/dirserv.c index e5995697d..cc8dffb7f 100644 --- a/src/or/dirserv.c +++ b/src/or/dirserv.c @@ -2906,9 +2906,7 @@ generate_v2_networkstatus_opinion(void) contact = "(none)"; if (versioning) { - size_t v_len = 64+strlen(client_versions)+strlen(server_versions); - version_lines = tor_malloc(v_len); - tor_snprintf(version_lines, v_len, + tor_asprintf(&version_lines, "client-versions %s\nserver-versions %s\n", client_versions, server_versions); } else { diff --git a/src/or/dirvote.c b/src/or/dirvote.c index 72f49c7f4..f81ffa00b 100644 --- a/src/or/dirvote.c +++ b/src/or/dirvote.c @@ -581,15 +581,13 @@ make_consensus_method_list(int low, int high, const char *separator) { char *list; - char b[32]; int i; smartlist_t *lst; lst = smartlist_create(); for (i = low; i <= high; ++i) { if (!consensus_method_is_supported(i)) continue; - tor_snprintf(b, sizeof(b), "%d", i); - smartlist_add(lst, tor_strdup(b)); + smartlist_add_asprintf(lst, "%d", i); } list = smartlist_join_strings(lst, separator, 0, NULL); tor_assert(list); @@ -810,8 +808,6 @@ networkstatus_compute_bw_weights_v10(smartlist_t *chunks, int64_t G, int64_t Wmg = -1, Wme = -1, Wmd = -1; int64_t Wed = -1, Wee = -1; const char *casename; - char buf[512]; - int r; if (G <= 0 || M <= 0 || E <= 0 || D <= 0) { log_warn(LD_DIR, "Consensus with empty bandwidth: " @@ -1019,7 +1015,7 @@ networkstatus_compute_bw_weights_v10(smartlist_t *chunks, int64_t G, * * NOTE: This list is sorted. */ - r = tor_snprintf(buf, sizeof(buf), + smartlist_add_asprintf(chunks, "bandwidth-weights Wbd=%d Wbe=%d Wbg=%d Wbm=%d " "Wdb=%d " "Web=%d Wed=%d Wee=%d Weg=%d Wem=%d " @@ -1030,13 +1026,6 @@ networkstatus_compute_bw_weights_v10(smartlist_t *chunks, int64_t G, (int)weight_scale, (int)Wed, (int)Wee, (int)Wed, (int)Wee, (int)weight_scale, (int)Wgd, (int)Wgg, (int)Wgg, (int)weight_scale, (int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale); - if (r<0) { - log_warn(LD_BUG, - "Not enough space in buffer for bandwidth-weights line."); - *buf = '\0'; - return 0; - } - smartlist_add(chunks, tor_strdup(buf)); log_notice(LD_CIRC, "Computed bandwidth weights for %s with v10: " "G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT @@ -1060,8 +1049,6 @@ networkstatus_compute_bw_weights_v9(smartlist_t *chunks, int64_t G, int64_t M, int64_t Wmg = -1, Wme = -1, Wmd = -1; int64_t Wed = -1, Wee = -1; const char *casename; - char buf[512]; - int r; if (G <= 0 || M <= 0 || E <= 0 || D <= 0) { log_warn(LD_DIR, "Consensus with empty bandwidth: " @@ -1323,7 +1310,7 @@ networkstatus_compute_bw_weights_v9(smartlist_t *chunks, int64_t G, int64_t M, * * NOTE: This list is sorted. */ - r = tor_snprintf(buf, sizeof(buf), + smartlist_add_asprintf(chunks, "Wbd=%d Wbe=%d Wbg=%d Wbm=%d " "Wdb=%d " "Web=%d Wed=%d Wee=%d Weg=%d Wem=%d " @@ -1334,12 +1321,7 @@ networkstatus_compute_bw_weights_v9(smartlist_t *chunks, int64_t G, int64_t M, (int)weight_scale, (int)Wed, (int)Wee, (int)Wed, (int)Wee, (int)weight_scale, (int)Wgd, (int)Wgg, (int)Wgg, (int)weight_scale, (int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale); - if (r<0) { - log_warn(LD_BUG, - "Not enough space in buffer for bandwidth-weights line."); - *buf = '\0'; - } - smartlist_add(chunks, tor_strdup(buf)); + log_notice(LD_CIRC, "Computed bandwidth weights for %s with v9: " "G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT " T="I64_FORMAT, @@ -2318,20 +2300,19 @@ networkstatus_format_signatures(networkstatus_t *consensus, base16_encode(sk, sizeof(sk), sig->signing_key_digest, DIGEST_LEN); base16_encode(id, sizeof(id), sig->identity_digest, DIGEST_LEN); if (flavor == FLAV_NS) { - tor_snprintf(buf, sizeof(buf), + smartlist_add_asprintf(elements, "%s %s %s\n-----BEGIN SIGNATURE-----\n", keyword, id, sk); } else { const char *digest_name = crypto_digest_algorithm_get_name(sig->alg); - tor_snprintf(buf, sizeof(buf), + smartlist_add_asprintf(elements, "%s%s%s %s %s %s\n-----BEGIN SIGNATURE-----\n", keyword, for_detached_signatures ? " " : "", for_detached_signatures ? flavor_name : "", digest_name, id, sk); } - smartlist_add(elements, tor_strdup(buf)); base64_encode(buf, sizeof(buf), sig->signature, sig->signature_len); strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf)); smartlist_add(elements, tor_strdup(buf)); @@ -2354,7 +2335,6 @@ char * networkstatus_get_detached_signatures(smartlist_t *consensuses) { smartlist_t *elements; - char buf[4096]; char *result = NULL, *sigs = NULL; networkstatus_t *consensus_ns = NULL; tor_assert(consensuses); @@ -2383,12 +2363,11 @@ networkstatus_get_detached_signatures(smartlist_t *consensuses) format_iso_time(fu_buf, consensus_ns->fresh_until); format_iso_time(vu_buf, consensus_ns->valid_until); - tor_snprintf(buf, sizeof(buf), + smartlist_add_asprintf(elements, "consensus-digest %s\n" "valid-after %s\n" "fresh-until %s\n" "valid-until %s\n", d, va_buf, fu_buf, vu_buf); - smartlist_add(elements, tor_strdup(buf)); } /* Get all the digests for the non-FLAV_NS consensuses */ @@ -2407,9 +2386,8 @@ networkstatus_get_detached_signatures(smartlist_t *consensuses) if (tor_mem_is_zero(ns->digests.d[alg], DIGEST256_LEN)) continue; base16_encode(d, sizeof(d), ns->digests.d[alg], DIGEST256_LEN); - tor_snprintf(buf, sizeof(buf), "additional-digest %s %s %s\n", + smartlist_add_asprintf(elements, "additional-digest %s %s %s\n", flavor_name, alg_name, d); - smartlist_add(elements, tor_strdup(buf)); } } SMARTLIST_FOREACH_END(ns); diff --git a/src/or/rephist.c b/src/or/rephist.c index b7bd2387f..4129fe2bb 100644 --- a/src/or/rephist.c +++ b/src/or/rephist.c @@ -941,7 +941,6 @@ rep_hist_get_router_stability_doc(time_t now) DIGESTMAP_FOREACH(history_map, id, or_history_t *, hist) { const node_t *node; char dbuf[BASE64_DIGEST_LEN+1]; - char header_buf[512]; char *info; digest_to_base64(dbuf, id); node = node_get_by_id(id); @@ -954,7 +953,7 @@ rep_hist_get_router_stability_doc(time_t now) format_iso_time(tbuf, published); else strlcpy(tbuf, "???", sizeof(tbuf)); - tor_snprintf(header_buf, sizeof(header_buf), + smartlist_add_asprintf(chunks, "router %s %s %s\n" "published %s\n" "relevant-flags %s%s%s\n" @@ -966,10 +965,9 @@ rep_hist_get_router_stability_doc(time_t now) node->ri && node->ri->is_hibernating ? "Hibernating " : "", node_get_declared_uptime(node)); } else { - tor_snprintf(header_buf, sizeof(header_buf), + smartlist_add_asprintf(chunks, "router %s {no descriptor}\n", dbuf); } - smartlist_add(chunks, tor_strdup(header_buf)); info = rep_hist_format_router_status(hist, now); if (info) smartlist_add(chunks, info); diff --git a/src/or/router.c b/src/or/router.c index 7d9d2724f..f8aa1e3b0 100644 --- a/src/or/router.c +++ b/src/or/router.c @@ -1973,11 +1973,8 @@ router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router, format_iso_time(published, router->cache_info.published_on); if (router->declared_family && smartlist_len(router->declared_family)) { - size_t n; - char *family = smartlist_join_strings(router->declared_family, " ", 0, &n); - n += strlen("family ") + 2; /* 1 for \n, 1 for \0. */ - family_line = tor_malloc(n); - tor_snprintf(family_line, n, "family %s\n", family); + char *family = smartlist_join_strings(router->declared_family, " ", 0, NULL); + tor_asprintf(&family_line, "family %s\n", family); tor_free(family); } else { family_line = tor_strdup(""); |