o Code simplifications and refactoring
- Use the smartlist_add_asprintf alias more consistently
throughout the codebase.
+ - Convert more instances of tor_snprintf+tor_strdup into
+ tor_asprintf.
\ No newline at end of file
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));
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
tor_free(torrc_fname);
tor_free(torrc_defaults_fname);
- tor_free(_version);
+ tor_free(the_tor_version);
tor_free(global_dirfrontpagecontents);
}
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) {
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 */
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));
if (*eq == '=') {
char *val=NULL;
size_t val_len=0;
- size_t ent_len;
if (*body != '\"') {
char *val_start = body;
while (!TOR_ISSPACE(*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 {
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")) {
!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));
} 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")) {
}
} 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);
}
char *url;
char request[8192];
const char *httpcommand = NULL;
- size_t len;
tor_assert(conn);
tor_assert(conn->_base.type == CONN_TYPE_DIR);
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 */
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);
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);
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);
directory_dump_request_log(void)
{
smartlist_t *lines;
- char tmp[256];
char *result;
strmap_iter_t *iter;
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);
#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") &&
size_t len;
struct mallinfo mi;
smartlist_t *lines;
- char tmp[256];
memset(&mi, 0, sizeof(mi));
mi = mallinfo();
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 {
{
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);
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: "
*
* 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 "
(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
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: "
*
* 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 "
(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,
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));
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);
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 */
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);
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);
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"
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);
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("");