summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2012-01-16 15:07:58 -0500
committerNick Mathewson <nickm@torproject.org>2012-01-16 15:07:58 -0500
commit2bd40a8eb0be195022558addb868edf56162f9dc (patch)
tree7e234f80696dd17f212afba95b75e81a632bd03e
parent79bb44c219af8f88573fb33954e5d2e16ef8fe42 (diff)
parent5579bc0eafc7ef426922e23340a5f885f8e8d4e9 (diff)
downloadtor-2bd40a8eb0be195022558addb868edf56162f9dc.tar.gz
tor-2bd40a8eb0be195022558addb868edf56162f9dc.zip
Merge branch 'bug4889_v2'
-rw-r--r--changes/clean_asprintf5
-rw-r--r--src/common/compat.c16
-rw-r--r--src/common/util.c23
-rw-r--r--src/common/util.h4
-rw-r--r--src/or/circuitbuild.c40
-rw-r--r--src/or/config.c58
-rw-r--r--src/or/connection_edge.c26
-rw-r--r--src/or/control.c131
-rw-r--r--src/or/directory.c62
-rw-r--r--src/or/dirserv.c4
-rw-r--r--src/or/dirvote.c80
-rw-r--r--src/or/geoip.c10
-rw-r--r--src/or/hibernate.c6
-rw-r--r--src/or/networkstatus.c32
-rw-r--r--src/or/ntmain.c20
-rw-r--r--src/or/policies.c10
-rw-r--r--src/or/rephist.c71
-rw-r--r--src/or/router.c12
-rw-r--r--src/or/routerlist.c20
-rw-r--r--src/or/transports.c9
20 files changed, 201 insertions, 438 deletions
diff --git a/changes/clean_asprintf b/changes/clean_asprintf
new file mode 100644
index 0000000000..ebaabd775f
--- /dev/null
+++ b/changes/clean_asprintf
@@ -0,0 +1,5 @@
+ 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.
diff --git a/src/common/compat.c b/src/common/compat.c
index ff9d877cd6..3af43e80c9 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -1351,31 +1351,19 @@ log_credential_status(void)
return -1;
} else {
int i, retval = 0;
- char *strgid;
char *s = NULL;
smartlist_t *elts = smartlist_create();
for (i = 0; i<ngids; i++) {
- strgid = tor_malloc(11);
- if (tor_snprintf(strgid, 11, "%u", (unsigned)sup_gids[i]) < 0) {
- log_warn(LD_GENERAL, "Error printing supplementary GIDs");
- tor_free(strgid);
- retval = -1;
- goto error;
- }
- smartlist_add(elts, strgid);
+ smartlist_add_asprintf(elts, "%u", (unsigned)sup_gids[i]);
}
s = smartlist_join_strings(elts, " ", 0, NULL);
log_fn(CREDENTIAL_LOG_LEVEL, LD_GENERAL, "Supplementary groups are: %s",s);
- error:
tor_free(s);
- SMARTLIST_FOREACH(elts, char *, cp,
- {
- tor_free(cp);
- });
+ SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
smartlist_free(elts);
tor_free(sup_gids);
diff --git a/src/common/util.c b/src/common/util.c
index 22c816abcc..3d8b7dabef 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -1964,7 +1964,6 @@ int
start_writing_to_file(const char *fname, int open_flags, int mode,
open_file_t **data_out)
{
- size_t tempname_len = strlen(fname)+16;
open_file_t *new_file = tor_malloc_zero(sizeof(open_file_t));
const char *open_name;
int append = 0;
@@ -1975,7 +1974,6 @@ start_writing_to_file(const char *fname, int open_flags, int mode,
tor_assert((open_flags & (O_BINARY|O_TEXT)) != 0);
#endif
new_file->fd = -1;
- tor_assert(tempname_len > strlen(fname)); /*check for overflow*/
new_file->filename = tor_strdup(fname);
if (open_flags & O_APPEND) {
open_name = fname;
@@ -1983,11 +1981,8 @@ start_writing_to_file(const char *fname, int open_flags, int mode,
append = 1;
open_flags &= ~O_APPEND;
} else {
- open_name = new_file->tempname = tor_malloc(tempname_len);
- if (tor_snprintf(new_file->tempname, tempname_len, "%s.tmp", fname)<0) {
- log_warn(LD_GENERAL, "Failed to generate filename");
- goto err;
- }
+ tor_asprintf(&new_file->tempname, "%s.tmp", fname);
+ open_name = new_file->tempname;
/* We always replace an existing temporary file if there is one. */
open_flags |= O_CREAT|O_TRUNC;
open_flags &= ~O_EXCL;
@@ -2757,17 +2752,17 @@ tor_sscanf(const char *buf, const char *pattern, ...)
/** Append the string produced by tor_asprintf(<b>pattern</b>, <b>...</b>)
* to <b>sl</b>. */
void
-smartlist_asprintf_add(struct smartlist_t *sl, const char *pattern, ...)
+smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern, ...)
{
va_list ap;
va_start(ap, pattern);
- smartlist_vasprintf_add(sl, pattern, ap);
+ smartlist_add_vasprintf(sl, pattern, ap);
va_end(ap);
}
-/** va_list-based backend of smartlist_asprintf_add. */
+/** va_list-based backend of smartlist_add_asprintf. */
void
-smartlist_vasprintf_add(struct smartlist_t *sl, const char *pattern,
+smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern,
va_list args)
{
char *str = NULL;
@@ -2786,14 +2781,12 @@ tor_listdir(const char *dirname)
{
smartlist_t *result;
#ifdef MS_WINDOWS
- char *pattern;
+ char *pattern=NULL;
TCHAR tpattern[MAX_PATH] = {0};
char name[MAX_PATH] = {0};
HANDLE handle;
WIN32_FIND_DATA findData;
- size_t pattern_len = strlen(dirname)+16;
- pattern = tor_malloc(pattern_len);
- tor_snprintf(pattern, pattern_len, "%s\\*", dirname);
+ tor_asprintf(&pattern, "%s\\*", dirname);
#ifdef UNICODE
mbstowcs(tpattern,pattern,MAX_PATH);
#else
diff --git a/src/common/util.h b/src/common/util.h
index cbc56d0816..b15b1ea56a 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -218,9 +218,9 @@ int tor_sscanf(const char *buf, const char *pattern, ...)
#endif
;
-void smartlist_asprintf_add(struct smartlist_t *sl, const char *pattern, ...)
+void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern, ...)
CHECK_PRINTF(2, 3);
-void smartlist_vasprintf_add(struct smartlist_t *sl, const char *pattern,
+void smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern,
va_list args);
int hex_decode_digit(char c);
diff --git a/src/or/circuitbuild.c b/src/or/circuitbuild.c
index a9e5d22943..d157dbd1a2 100644
--- a/src/or/circuitbuild.c
+++ b/src/or/circuitbuild.c
@@ -675,8 +675,7 @@ circuit_build_times_update_state(circuit_build_times_t *cbt,
if (histogram[i] == 0) continue;
*next = line = tor_malloc_zero(sizeof(config_line_t));
line->key = tor_strdup("CircuitBuildTimeBin");
- line->value = tor_malloc(25);
- tor_snprintf(line->value, 25, "%d %d",
+ tor_asprintf(&line->value, "%d %d",
CBT_BIN_TO_MS(i), histogram[i]);
next = &(line->next);
}
@@ -1545,15 +1544,13 @@ circuit_list_path_impl(origin_circuit_t *circ, int verbose, int verbose_names)
if (verbose) {
const char *nickname = build_state_get_exit_nickname(circ->build_state);
- char *cp;
- tor_asprintf(&cp, "%s%s circ (length %d%s%s):",
+ smartlist_add_asprintf(elements, "%s%s circ (length %d%s%s):",
circ->build_state->is_internal ? "internal" : "exit",
circ->build_state->need_uptime ? " (high-uptime)" : "",
circ->build_state->desired_path_len,
circ->_base.state == CIRCUIT_STATE_OPEN ? "" : ", last hop ",
circ->_base.state == CIRCUIT_STATE_OPEN ? "" :
(nickname?nickname:"*unnamed*"));
- smartlist_add(elements, cp);
}
hop = circ->cpath;
@@ -1594,11 +1591,8 @@ circuit_list_path_impl(origin_circuit_t *circ, int verbose, int verbose_names)
}
tor_assert(elt);
if (verbose) {
- size_t len = strlen(elt)+2+strlen(states[hop->state])+1;
- char *v = tor_malloc(len);
tor_assert(hop->state <= 2);
- tor_snprintf(v,len,"%s(%s)",elt,states[hop->state]);
- smartlist_add(elements, v);
+ smartlist_add_asprintf(elements,"%s(%s)",elt,states[hop->state]);
tor_free(elt);
} else {
smartlist_add(elements, elt);
@@ -3599,19 +3593,17 @@ log_entry_guards(int severity)
SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e)
{
const char *msg = NULL;
- char *cp;
if (entry_is_live(e, 0, 1, 0, &msg))
- tor_asprintf(&cp, "%s [%s] (up %s)",
+ smartlist_add_asprintf(elements, "%s [%s] (up %s)",
e->nickname,
hex_str(e->identity, DIGEST_LEN),
e->made_contact ? "made-contact" : "never-contacted");
else
- tor_asprintf(&cp, "%s [%s] (%s, %s)",
+ smartlist_add_asprintf(elements, "%s [%s] (%s, %s)",
e->nickname,
hex_str(e->identity, DIGEST_LEN),
msg,
e->made_contact ? "made-contact" : "never-contacted");
- smartlist_add(elements, cp);
}
SMARTLIST_FOREACH_END(e);
@@ -3755,9 +3747,8 @@ remove_obsolete_entry_guards(time_t now)
msg = "does not seem to be from any recognized version of Tor";
version_is_bad = 1;
} else {
- size_t len = strlen(ver)+5;
- char *tor_ver = tor_malloc(len);
- tor_snprintf(tor_ver, len, "Tor %s", ver);
+ char *tor_ver = NULL;
+ tor_asprintf(&tor_ver, "Tor %s", ver);
if ((tor_version_as_new_as(tor_ver, "0.1.0.10-alpha") &&
!tor_version_as_new_as(tor_ver, "0.1.2.16-dev")) ||
(tor_version_as_new_as(tor_ver, "0.2.0.0-alpha") &&
@@ -4445,10 +4436,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));
@@ -4472,15 +4461,11 @@ entry_guards_update_state(or_state_t *state)
!strchr(e->chosen_by_version, ' ')) {
char d[HEX_DIGEST_LEN+1];
char t[ISO_TIME_LEN+1];
- size_t val_len;
*next = line = tor_malloc_zero(sizeof(config_line_t));
line->key = tor_strdup("EntryGuardAddedBy");
- val_len = (HEX_DIGEST_LEN+1+strlen(e->chosen_by_version)
- +1+ISO_TIME_LEN+1);
- line->value = tor_malloc(val_len);
base16_encode(d, sizeof(d), e->identity, DIGEST_LEN);
format_iso_time(t, e->chosen_on_date);
- tor_snprintf(line->value, val_len, "%s %s %s",
+ tor_asprintf(&line->value, "%s %s %s",
d, e->chosen_by_version, t);
next = &(line->next);
}
@@ -4512,8 +4497,6 @@ getinfo_helper_entry_guards(control_connection_t *conn,
if (!entry_guards)
entry_guards = smartlist_create();
SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e) {
- size_t len = MAX_VERBOSE_NICKNAME_LEN+ISO_TIME_LEN+32;
- char *c = tor_malloc(len);
const char *status = NULL;
time_t when = 0;
const node_t *node;
@@ -4539,11 +4522,10 @@ getinfo_helper_entry_guards(control_connection_t *conn,
if (when) {
format_iso_time(tbuf, when);
- tor_snprintf(c, len, "%s %s %s\n", nbuf, status, tbuf);
+ smartlist_add_asprintf(sl, "%s %s %s\n", nbuf, status, tbuf);
} else {
- tor_snprintf(c, len, "%s %s\n", nbuf, status);
+ smartlist_add_asprintf(sl, "%s %s\n", nbuf, status);
}
- smartlist_add(sl, c);
} SMARTLIST_FOREACH_END(e);
*answer = smartlist_join_strings(sl, "", 0, NULL);
SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
diff --git a/src/or/config.c b/src/or/config.c
index 341bc15d21..0763058d49 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) {
@@ -1650,10 +1647,8 @@ options_act(const or_options_t *old_options)
#ifdef WIN32
if (!strcmp(actual_fname, "<default>")) {
const char *conf_root = get_windows_conf_root();
- size_t len = strlen(conf_root)+16;
tor_free(actual_fname);
- actual_fname = tor_malloc(len+1);
- tor_snprintf(actual_fname, len, "%s\\geoip", conf_root);
+ tor_asprintf(&actual_fname, "%s\\geoip", conf_root);
}
#endif
geoip_load_file(actual_fname, options);
@@ -3147,11 +3142,9 @@ config_dump(const config_format_t *fmt, const void *default_options,
line = assigned = get_assigned_option(fmt, options, fmt->vars[i].name, 1);
for (; line; line = line->next) {
- char *tmp;
- tor_asprintf(&tmp, "%s%s %s\n",
+ smartlist_add_asprintf(elements, "%s%s %s\n",
comment_option ? "# " : "",
line->key, line->value);
- smartlist_add(elements, tmp);
}
config_free_lines(assigned);
}
@@ -3159,9 +3152,7 @@ config_dump(const config_format_t *fmt, const void *default_options,
if (fmt->extra) {
line = *(config_line_t**)STRUCT_VAR_P(options, fmt->extra->var_offset);
for (; line; line = line->next) {
- char *tmp;
- tor_asprintf(&tmp, "%s %s\n", line->key, line->value);
- smartlist_add(elements, tmp);
+ smartlist_add_asprintf(elements, "%s %s\n", line->key, line->value);
}
}
@@ -3510,11 +3501,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 */
@@ -6154,18 +6142,12 @@ write_configuration_file(const char *fname, const or_options_t *options)
if (rename_old) {
int i = 1;
- size_t fn_tmp_len = strlen(fname)+32;
- char *fn_tmp;
- tor_assert(fn_tmp_len > strlen(fname)); /*check for overflow*/
- fn_tmp = tor_malloc(fn_tmp_len);
+ char *fn_tmp = NULL;
while (1) {
- if (tor_snprintf(fn_tmp, fn_tmp_len, "%s.orig.%d", fname, i)<0) {
- log_warn(LD_BUG, "tor_snprintf failed inexplicably");
- tor_free(fn_tmp);
- goto err;
- }
+ tor_asprintf(&fn_tmp, "%s.orig.%d", fname, i);
if (file_status(fn_tmp) == FN_NOENT)
break;
+ tor_free(fn_tmp);
++i;
}
log_notice(LD_CONFIG, "Renaming old configuration file to \"%s\"", fn_tmp);
@@ -6612,13 +6594,13 @@ or_state_save_broken(char *fname)
{
int i;
file_status_t status;
- size_t len = strlen(fname)+16;
- char *fname2 = tor_malloc(len);
+ char *fname2 = NULL;
for (i = 0; i < 100; ++i) {
- tor_snprintf(fname2, len, "%s.%d", fname, i);
+ tor_asprintf(&fname2, "%s.%d", fname, i);
status = file_status(fname2);
if (status == FN_NOENT)
break;
+ tor_free(fname2);
}
if (i == 100) {
log_warn(LD_BUG, "Unable to parse state in \"%s\"; too many saved bad "
@@ -6994,7 +6976,6 @@ getinfo_helper_config(control_connection_t *conn,
for (i = 0; _option_vars[i].name; ++i) {
const config_var_t *var = &_option_vars[i];
const char *type;
- char *line;
switch (var->type) {
case CONFIG_TYPE_STRING: type = "String"; break;
case CONFIG_TYPE_FILENAME: type = "Filename"; break;
@@ -7018,8 +6999,7 @@ getinfo_helper_config(control_connection_t *conn,
}
if (!type)
continue;
- tor_asprintf(&line, "%s %s\n",var->name,type);
- smartlist_add(sl, line);
+ smartlist_add_asprintf(sl, "%s %s\n",var->name,type);
}
*answer = smartlist_join_strings(sl, "", 0, NULL);
SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c
index aba9ba2727..dcb9cae423 100644
--- a/src/or/connection_edge.c
+++ b/src/or/connection_edge.c
@@ -909,13 +909,10 @@ addressmap_ent_remove(const char *address, addressmap_entry_t *ent)
static void
clear_trackexithost_mappings(const char *exitname)
{
- char *suffix;
- size_t suffix_len;
+ char *suffix = NULL;
if (!addressmap || !exitname)
return;
- suffix_len = strlen(exitname) + 16;
- suffix = tor_malloc(suffix_len);
- tor_snprintf(suffix, suffix_len, ".%s.exit", exitname);
+ tor_asprintf(&suffix, ".%s.exit", exitname);
tor_strlower(suffix);
STRMAP_FOREACH_MODIFY(addressmap, address, addressmap_entry_t *, ent) {
@@ -1165,11 +1162,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));
@@ -1403,9 +1399,8 @@ client_dns_set_reverse_addressmap(const char *address, const char *v,
const char *exitname,
int ttl)
{
- size_t len = strlen(address) + 16;
- char *s = tor_malloc(len);
- tor_snprintf(s, len, "REVERSE[%s]", address);
+ char *s = NULL;
+ tor_asprintf(&s, "REVERSE[%s]", address);
client_dns_set_addressmap_impl(s, v, exitname, ttl);
tor_free(s);
}
@@ -1689,21 +1684,18 @@ addressmap_get_mappings(smartlist_t *sl, time_t min_expires,
addressmap_ent_remove(key, val);
continue;
} else if (val->new_address) {
- size_t len = strlen(key)+strlen(val->new_address)+ISO_TIME_LEN+5;
- char *line = tor_malloc(len);
if (want_expiry) {
if (val->expires < 3 || val->expires == TIME_MAX)
- tor_snprintf(line, len, "%s %s NEVER", key, val->new_address);
+ smartlist_add_asprintf(sl, "%s %s NEVER", key, val->new_address);
else {
char time[ISO_TIME_LEN+1];
format_iso_time(time, val->expires);
- tor_snprintf(line, len, "%s %s \"%s\"", key, val->new_address,
+ smartlist_add_asprintf(sl, "%s %s \"%s\"", key, val->new_address,
time);
}
} else {
- tor_snprintf(line, len, "%s %s", key, val->new_address);
+ smartlist_add_asprintf(sl, "%s %s", key, val->new_address);
}
- smartlist_add(sl, line);
}
}
iter = strmap_iter_next(addressmap,iter);
diff --git a/src/or/control.c b/src/or/control.c
index 60c62b15a5..1d2d8dd392 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -525,18 +525,15 @@ control_ports_write_to_file(void)
lines = smartlist_create();
SMARTLIST_FOREACH_BEGIN(get_connection_array(), const connection_t *, conn) {
- char *port_str = NULL;
if (conn->type != CONN_TYPE_CONTROL_LISTENER || conn->marked_for_close)
continue;
#ifdef AF_UNIX
if (conn->socket_family == AF_UNIX) {
- tor_asprintf(&port_str, "UNIX_PORT=%s\n", conn->address);
- smartlist_add(lines, port_str);
+ smartlist_add_asprintf(lines, "UNIX_PORT=%s\n", conn->address);
continue;
}
#endif
- tor_asprintf(&port_str, "PORT=%s:%d\n", conn->address, conn->port);
- smartlist_add(lines, port_str);
+ smartlist_add_asprintf(lines, "PORT=%s:%d\n", conn->address, conn->port);
} SMARTLIST_FOREACH_END(conn);
joined = smartlist_join_strings(lines, "", 0, NULL);
@@ -700,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))
@@ -718,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 {
@@ -820,19 +814,13 @@ handle_control_getconf(control_connection_t *conn, uint32_t body_len,
config_line_t *answer = option_get_assignment(options,q);
if (!answer) {
const char *name = option_get_canonical_name(q);
- size_t alen = strlen(name)+8;
- char *astr = tor_malloc(alen);
- tor_snprintf(astr, alen, "250-%s\r\n", name);
- smartlist_add(answers, astr);
+ smartlist_add_asprintf(answers, "250-%s\r\n", name);
}
while (answer) {
config_line_t *next;
- size_t alen = strlen(answer->key)+strlen(answer->value)+8;
- char *astr = tor_malloc(alen);
- tor_snprintf(astr, alen, "250-%s=%s\r\n",
+ smartlist_add_asprintf(answers, "250-%s=%s\r\n",
answer->key, answer->value);
- smartlist_add(answers, astr);
next = answer->next;
tor_free(answer->key);
@@ -1307,12 +1295,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")) {
@@ -1320,28 +1305,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));
@@ -1558,7 +1537,6 @@ getinfo_helper_listeners(control_connection_t *control_conn,
res = smartlist_create();
SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
- char *addr;
struct sockaddr_storage ss;
socklen_t ss_len = sizeof(ss);
@@ -1566,14 +1544,13 @@ getinfo_helper_listeners(control_connection_t *control_conn,
continue;
if (getsockname(conn->s, (struct sockaddr *)&ss, &ss_len) < 0) {
- tor_asprintf(&addr, "%s:%d", conn->address, (int)conn->port);
+ smartlist_add_asprintf(res, "%s:%d", conn->address, (int)conn->port);
} else {
char *tmp = tor_sockaddr_to_str((struct sockaddr *)&ss);
- addr = esc_for_log(tmp);
+ smartlist_add(res, esc_for_log(tmp));
tor_free(tmp);
}
- if (addr)
- smartlist_add(res, addr);
+
} SMARTLIST_FOREACH_END(conn);
*answer = smartlist_join_strings(res, " ", 0, NULL);
@@ -1675,13 +1652,13 @@ getinfo_helper_dir(control_connection_t *control_conn,
ri->cache_info.annotations_len);
}
} else if (!strcmpstart(question, "dir/server/")) {
- size_t answer_len = 0, url_len = strlen(question)+2;
- char *url = tor_malloc(url_len);
+ size_t answer_len = 0;
+ char *url = NULL;
smartlist_t *descs = smartlist_create();
const char *msg;
int res;
char *cp;
- tor_snprintf(url, url_len, "/tor/%s", question+4);
+ tor_asprintf(&url, "/tor/%s", question+4);
res = dirserv_get_routerdescs(descs, url, &msg);
if (res) {
log_warn(LD_CONTROL, "getinfo '%s': %s", question, msg);
@@ -1798,7 +1775,6 @@ circuit_describe_status_for_controller(origin_circuit_t *circ)
}
{
- char *buildflags = NULL;
cpath_build_state_t *build_state = circ->build_state;
smartlist_t *flaglist = smartlist_create();
char *flaglist_joined;
@@ -1816,8 +1792,7 @@ circuit_describe_status_for_controller(origin_circuit_t *circ)
if (smartlist_len(flaglist)) {
flaglist_joined = smartlist_join_strings(flaglist, ",", 0, NULL);
- tor_asprintf(&buildflags, "BUILD_FLAGS=%s", flaglist_joined);
- smartlist_add(descparts, buildflags);
+ smartlist_add_asprintf(descparts, "BUILD_FLAGS=%s", flaglist_joined);
tor_free(flaglist_joined);
}
@@ -1825,43 +1800,28 @@ circuit_describe_status_for_controller(origin_circuit_t *circ)
smartlist_free(flaglist);
}
- {
- char *purpose = NULL;
- tor_asprintf(&purpose, "PURPOSE=%s",
- circuit_purpose_to_controller_string(circ->_base.purpose));
- smartlist_add(descparts, purpose);
- }
+ smartlist_add_asprintf(descparts, "PURPOSE=%s",
+ circuit_purpose_to_controller_string(circ->_base.purpose));
{
- char *hs_state_arg = NULL;
const char *hs_state =
circuit_purpose_to_controller_hs_state_string(circ->_base.purpose);
if (hs_state != NULL) {
- tor_asprintf(&hs_state_arg, "HS_STATE=%s",
- hs_state);
-
- smartlist_add(descparts, hs_state_arg);
+ smartlist_add_asprintf(descparts, "HS_STATE=%s", hs_state);
}
}
if (circ->rend_data != NULL) {
- char *rend_query_arg = NULL;
-
- tor_asprintf(&rend_query_arg, "REND_QUERY=%s",
+ smartlist_add_asprintf(descparts, "REND_QUERY=%s",
circ->rend_data->onion_address);
-
- smartlist_add(descparts, rend_query_arg);
}
{
- char *time_created_arg = NULL;
char tbuf[ISO_TIME_USEC_LEN+1];
format_iso_time_nospace_usec(tbuf, &circ->_base.timestamp_created);
- tor_asprintf(&time_created_arg, "TIME_CREATED=%s", tbuf);
-
- smartlist_add(descparts, time_created_arg);
+ smartlist_add_asprintf(descparts, "TIME_CREATED=%s", tbuf);
}
rv = smartlist_join_strings(descparts, " ", 0, NULL);
@@ -1885,8 +1845,7 @@ getinfo_helper_events(control_connection_t *control_conn,
smartlist_t *status = smartlist_create();
for (circ_ = _circuit_get_global_list(); circ_; circ_ = circ_->next) {
origin_circuit_t *circ;
- char *s, *circdesc;
- size_t slen;
+ char *circdesc;
const char *state;
if (! CIRCUIT_IS_ORIGIN(circ_) || circ_->marked_for_close)
continue;
@@ -1901,12 +1860,9 @@ getinfo_helper_events(control_connection_t *control_conn,
circdesc = circuit_describe_status_for_controller(circ);
- slen = strlen(circdesc)+strlen(state)+30;
- s = tor_malloc(slen+1);
- tor_snprintf(s, slen, "%lu %s%s%s",
+ smartlist_add_asprintf(status, "%lu %s%s%s",
(unsigned long)circ->global_identifier,
state, *circdesc ? " " : "", circdesc);
- smartlist_add(status, s);
tor_free(circdesc);
}
*answer = smartlist_join_strings(status, "\r\n", 0, NULL);
@@ -1919,8 +1875,6 @@ getinfo_helper_events(control_connection_t *control_conn,
SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
const char *state;
entry_connection_t *conn;
- char *s;
- size_t slen;
circuit_t *circ;
origin_circuit_t *origin_circ = NULL;
if (base_conn->type != CONN_TYPE_AP ||
@@ -1955,14 +1909,11 @@ getinfo_helper_events(control_connection_t *control_conn,
if (circ && CIRCUIT_IS_ORIGIN(circ))
origin_circ = TO_ORIGIN_CIRCUIT(circ);
write_stream_target_to_buf(conn, buf, sizeof(buf));
- slen = strlen(buf)+strlen(state)+32;
- s = tor_malloc(slen+1);
- tor_snprintf(s, slen, "%lu %s %lu %s",
+ smartlist_add_asprintf(status, "%lu %s %lu %s",
(unsigned long) base_conn->global_identifier,state,
origin_circ?
(unsigned long)origin_circ->global_identifier : 0ul,
buf);
- smartlist_add(status, s);
} SMARTLIST_FOREACH_END(base_conn);
*answer = smartlist_join_strings(status, "\r\n", 0, NULL);
SMARTLIST_FOREACH(status, char *, cp, tor_free(cp));
@@ -1972,9 +1923,7 @@ getinfo_helper_events(control_connection_t *control_conn,
smartlist_t *status = smartlist_create();
SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
const char *state;
- char *s;
char name[128];
- size_t slen;
or_connection_t *conn;
if (base_conn->type != CONN_TYPE_OR || base_conn->marked_for_close)
continue;
@@ -1986,10 +1935,7 @@ getinfo_helper_events(control_connection_t *control_conn,
else
state = "NEW";
orconn_target_get_name(name, sizeof(name), conn);
- slen = strlen(name)+strlen(state)+2;
- s = tor_malloc(slen+1);
- tor_snprintf(s, slen, "%s %s", name, state);
- smartlist_add(status, s);
+ smartlist_add_asprintf(status, "%s %s", name, state);
} SMARTLIST_FOREACH_END(base_conn);
*answer = smartlist_join_strings(status, "\r\n", 0, NULL);
SMARTLIST_FOREACH(status, char *, cp, tor_free(cp));
@@ -2033,8 +1979,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")) {
@@ -2070,9 +2015,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);
}
@@ -2218,18 +2161,16 @@ static char *
list_getinfo_options(void)
{
int i;
- char *buf=NULL;
smartlist_t *lines = smartlist_create();
char *ans;
for (i = 0; getinfo_items[i].varname; ++i) {
if (!getinfo_items[i].desc)
continue;
- tor_asprintf(&buf, "%s%s -- %s\n",
+ smartlist_add_asprintf(lines, "%s%s -- %s\n",
getinfo_items[i].varname,
getinfo_items[i].is_prefix ? "*" : "",
getinfo_items[i].desc);
- smartlist_add(lines, buf);
}
smartlist_sort_strings(lines);
@@ -3551,8 +3492,7 @@ control_event_stream_status(entry_connection_t *conn, stream_status_event_t tp,
const char *reason_str = stream_end_reason_to_control_string(reason_code);
char *r = NULL;
if (!reason_str) {
- r = tor_malloc(16);
- tor_snprintf(r, 16, " UNKNOWN_%d", reason_code);
+ tor_asprintf(&r, " UNKNOWN_%d", reason_code);
reason_str = r;
}
if (reason_code & END_STREAM_REASON_FLAG_REMOTE)
@@ -3837,16 +3777,13 @@ control_event_descriptors_changed(smartlist_t *routers)
{
smartlist_t *names = smartlist_create();
char *ids;
- size_t names_len;
SMARTLIST_FOREACH(routers, routerinfo_t *, ri, {
char *b = tor_malloc(MAX_VERBOSE_NICKNAME_LEN+1);
router_get_verbose_nickname(b, ri);
smartlist_add(names, b);
});
- ids = smartlist_join_strings(names, " ", 0, &names_len);
- names_len = strlen(ids)+32;
- msg = tor_malloc(names_len);
- tor_snprintf(msg, names_len, "650 NEWDESC %s\r\n", ids);
+ ids = smartlist_join_strings(names, " ", 0, NULL);
+ tor_asprintf(&msg, "650 NEWDESC %s\r\n", ids);
send_control_event_string(EVENT_NEW_DESC, ALL_FORMATS, msg);
tor_free(ids);
tor_free(msg);
@@ -4239,9 +4176,9 @@ control_event_conf_changed(smartlist_t *elements)
char *k = smartlist_get(elements, i);
char *v = smartlist_get(elements, i+1);
if (v == NULL) {
- smartlist_asprintf_add(lines, "650-%s", k);
+ smartlist_add_asprintf(lines, "650-%s", k);
} else {
- smartlist_asprintf_add(lines, "650-%s=%s", k, v);
+ smartlist_add_asprintf(lines, "650-%s=%s", k, v);
}
}
result = smartlist_join_strings(lines, "\r\n", 0, NULL);
diff --git a/src/or/directory.c b/src/or/directory.c
index 12636bac33..94c37f18d3 100644
--- a/src/or/directory.c
+++ b/src/or/directory.c
@@ -1124,9 +1124,7 @@ directory_send_command(dir_connection_t *conn,
smartlist_t *headers = smartlist_create();
char *url;
char request[8192];
- char *header;
const char *httpcommand = NULL;
- size_t len;
tor_assert(conn);
tor_assert(conn->_base.type == CONN_TYPE_DIR);
@@ -1147,8 +1145,7 @@ directory_send_command(dir_connection_t *conn,
if (if_modified_since) {
char b[RFC1123_TIME_LEN+1];
format_rfc1123_time(b, if_modified_since);
- tor_asprintf(&header, "If-Modified-Since: %s\r\n", b);
- smartlist_add(headers, header);
+ smartlist_add_asprintf(headers, "If-Modified-Since: %s\r\n", b);
}
/* come up with some proxy lines, if we're using one. */
@@ -1163,11 +1160,10 @@ directory_send_command(dir_connection_t *conn,
log_warn(LD_BUG, "Encoding http authenticator failed");
}
if (base64_authenticator) {
- tor_asprintf(&header,
+ smartlist_add_asprintf(headers,
"Proxy-Authorization: Basic %s\r\n",
base64_authenticator);
tor_free(base64_authenticator);
- smartlist_add(headers, header);
}
} else {
proxystring[0] = 0;
@@ -1177,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 */
@@ -1194,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);
@@ -1215,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);
@@ -1238,8 +1224,7 @@ directory_send_command(dir_connection_t *conn,
httpcommand = "POST";
url = tor_strdup("/tor/");
if (why) {
- tor_asprintf(&header, "X-Desc-Gen-Reason: %s\r\n", why);
- smartlist_add(headers, header);
+ smartlist_add_asprintf(headers, "X-Desc-Gen-Reason: %s\r\n", why);
}
break;
}
@@ -1260,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);
@@ -1294,15 +1277,16 @@ directory_send_command(dir_connection_t *conn,
tor_free(url);
if (!strcmp(httpcommand, "POST") || payload) {
- tor_asprintf(&header, "Content-Length: %lu\r\n",
+ smartlist_add_asprintf(headers, "Content-Length: %lu\r\n",
payload ? (unsigned long)payload_len : 0);
- smartlist_add(headers, header);
}
- header = smartlist_join_strings(headers, "", 0, NULL);
- tor_snprintf(request, sizeof(request), " HTTP/1.0\r\nHost: %s\r\n%s\r\n",
- hoststring, header);
- tor_free(header);
+ {
+ char *header = smartlist_join_strings(headers, "", 0, NULL);
+ tor_snprintf(request, sizeof(request), " HTTP/1.0\r\nHost: %s\r\n%s\r\n",
+ hoststring, header);
+ tor_free(header);
+ }
connection_write_to_buf(request, strlen(request), TO_CONN(conn));
@@ -2479,11 +2463,9 @@ note_client_request(int purpose, int compressed, size_t bytes)
case DIR_PURPOSE_UPLOAD_RENDDESC_V2: kind = "dl/ul-rend2"; break;
}
if (kind) {
- key = tor_malloc(256);
- tor_snprintf(key, 256, "%s%s", kind, compressed?".z":"");
+ tor_asprintf(&key, "%s%s", kind, compressed?".z":"");
} else {
- key = tor_malloc(256);
- tor_snprintf(key, 256, "unknown purpose (%d)%s",
+ tor_asprintf(&key, "unknown purpose (%d)%s",
purpose, compressed?".z":"");
}
note_request(key, bytes);
@@ -2522,7 +2504,6 @@ char *
directory_dump_request_log(void)
{
smartlist_t *lines;
- char tmp[256];
char *result;
strmap_iter_t *iter;
@@ -2538,9 +2519,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);
@@ -3294,8 +3274,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") &&
@@ -3304,7 +3283,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 e5995697dd..cc8dffb7ff 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 3bb165aeea..f81ffa00be 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,
@@ -1475,7 +1457,6 @@ networkstatus_compute_consensus(smartlist_t *votes,
chunks = smartlist_create();
{
- char *buf=NULL;
char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
vu_buf[ISO_TIME_LEN+1];
char *flaglist;
@@ -1484,20 +1465,17 @@ networkstatus_compute_consensus(smartlist_t *votes,
format_iso_time(vu_buf, valid_until);
flaglist = smartlist_join_strings(flags, " ", 0, NULL);
- tor_asprintf(&buf, "network-status-version 3%s%s\n"
+ smartlist_add_asprintf(chunks, "network-status-version 3%s%s\n"
"vote-status consensus\n",
flavor == FLAV_NS ? "" : " ",
flavor == FLAV_NS ? "" : flavor_name);
- smartlist_add(chunks, buf);
-
if (consensus_method >= 2) {
- tor_asprintf(&buf, "consensus-method %d\n",
+ smartlist_add_asprintf(chunks, "consensus-method %d\n",
consensus_method);
- smartlist_add(chunks, buf);
}
- tor_asprintf(&buf,
+ smartlist_add_asprintf(chunks,
"valid-after %s\n"
"fresh-until %s\n"
"valid-until %s\n"
@@ -1508,7 +1486,6 @@ networkstatus_compute_consensus(smartlist_t *votes,
va_buf, fu_buf, vu_buf,
vote_seconds, dist_seconds,
client_versions, server_versions, flaglist);
- smartlist_add(chunks, buf);
tor_free(flaglist);
}
@@ -1550,7 +1527,6 @@ networkstatus_compute_consensus(smartlist_t *votes,
char votedigest[HEX_DIGEST_LEN+1];
networkstatus_t *v = e->v;
networkstatus_voter_info_t *voter = get_voter(v);
- char *buf = NULL;
if (e->is_legacy)
tor_assert(consensus_method >= 2);
@@ -1559,20 +1535,18 @@ networkstatus_compute_consensus(smartlist_t *votes,
base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
DIGEST_LEN);
- tor_asprintf(&buf,
+ smartlist_add_asprintf(chunks,
"dir-source %s%s %s %s %s %d %d\n",
voter->nickname, e->is_legacy ? "-legacy" : "",
fingerprint, voter->address, fmt_addr32(voter->addr),
voter->dir_port,
voter->or_port);
- smartlist_add(chunks, buf);
if (! e->is_legacy) {
- tor_asprintf(&buf,
+ smartlist_add_asprintf(chunks,
"contact %s\n"
"vote-digest %s\n",
voter->contact,
votedigest);
- smartlist_add(chunks, buf);
}
} SMARTLIST_FOREACH_END(e);
SMARTLIST_FOREACH(dir_sources, dir_src_ent_t *, e, tor_free(e));
@@ -1709,7 +1683,6 @@ networkstatus_compute_consensus(smartlist_t *votes,
int naming_conflict = 0;
int n_listing = 0;
int i;
- char *buf=NULL;
char microdesc_digest[DIGEST256_LEN];
/* Of the next-to-be-considered digest in each voter, which is first? */
@@ -1977,10 +1950,9 @@ networkstatus_compute_consensus(smartlist_t *votes,
/* Now an m line, if applicable. */
if (flavor == FLAV_MICRODESC &&
!tor_digest256_is_zero(microdesc_digest)) {
- char m[BASE64_DIGEST256_LEN+1], *cp;
+ char m[BASE64_DIGEST256_LEN+1];
digest256_to_base64(m, microdesc_digest);
- tor_asprintf(&cp, "m %s\n", m);
- smartlist_add(chunks, cp);
+ smartlist_add_asprintf(chunks, "m %s\n", m);
}
/* Next line is all flags. The "\n" is missing. */
smartlist_add(chunks,
@@ -1993,15 +1965,12 @@ networkstatus_compute_consensus(smartlist_t *votes,
smartlist_add(chunks, tor_strdup("\n"));
/* Now the weight line. */
if (rs_out.has_bandwidth) {
- char *cp=NULL;
- tor_asprintf(&cp, "w Bandwidth=%d\n", rs_out.bandwidth);
- smartlist_add(chunks, cp);
+ smartlist_add_asprintf(chunks, "w Bandwidth=%d\n", rs_out.bandwidth);
}
/* Now the exitpolicy summary line. */
if (rs_out.has_exitsummary && flavor == FLAV_NS) {
- tor_asprintf(&buf, "p %s\n", rs_out.exitsummary);
- smartlist_add(chunks, buf);
+ smartlist_add_asprintf(chunks, "p %s\n", rs_out.exitsummary);
}
/* And the loop is over and we move on to the next router */
@@ -2083,7 +2052,6 @@ networkstatus_compute_consensus(smartlist_t *votes,
size_t digest_len =
flavor == FLAV_NS ? DIGEST_LEN : DIGEST256_LEN;
const char *algname = crypto_digest_algorithm_get_name(digest_alg);
- char *buf = NULL;
char sigbuf[4096];
smartlist_add(chunks, tor_strdup("directory-signature "));
@@ -2097,14 +2065,13 @@ networkstatus_compute_consensus(smartlist_t *votes,
/* add the junk that will go at the end of the line. */
if (flavor == FLAV_NS) {
- tor_asprintf(&buf, "%s %s\n", fingerprint,
+ smartlist_add_asprintf(chunks, "%s %s\n", fingerprint,
signing_key_fingerprint);
} else {
- tor_asprintf(&buf, "%s %s %s\n",
+ smartlist_add_asprintf(chunks, "%s %s %s\n",
algname, fingerprint,
signing_key_fingerprint);
}
- smartlist_add(chunks, buf);
/* And the signature. */
sigbuf[0] = '\0';
if (router_append_dirobj_signature(sigbuf, sizeof(sigbuf),
@@ -2122,14 +2089,13 @@ networkstatus_compute_consensus(smartlist_t *votes,
crypto_pk_get_fingerprint(legacy_signing_key,
signing_key_fingerprint, 0);
if (flavor == FLAV_NS) {
- tor_asprintf(&buf, "%s %s\n", fingerprint,
+ smartlist_add_asprintf(chunks, "%s %s\n", fingerprint,
signing_key_fingerprint);
} else {
- tor_asprintf(&buf, "%s %s %s\n",
+ smartlist_add_asprintf(chunks, "%s %s %s\n",
algname, fingerprint,
signing_key_fingerprint);
}
- smartlist_add(chunks, buf);
sigbuf[0] = '\0';
if (router_append_dirobj_signature(sigbuf, sizeof(sigbuf),
digest, digest_len,
@@ -2334,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));
@@ -2370,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);
@@ -2399,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 */
@@ -2423,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/geoip.c b/src/or/geoip.c
index 73194ae9c6..3e1ee5987f 100644
--- a/src/or/geoip.c
+++ b/src/or/geoip.c
@@ -856,9 +856,7 @@ geoip_get_client_history(geoip_client_action_t action)
/* Build the result. */
chunks = smartlist_create();
SMARTLIST_FOREACH(entries, c_hist_t *, ch, {
- char *buf=NULL;
- tor_asprintf(&buf, "%s=%u", ch->country, ch->total);
- smartlist_add(chunks, buf);
+ smartlist_add_asprintf(chunks, "%s=%u", ch->country, ch->total);
});
result = smartlist_join_strings(chunks, ",", 0, NULL);
done:
@@ -907,10 +905,8 @@ geoip_get_request_history(geoip_client_action_t action)
strings = smartlist_create();
SMARTLIST_FOREACH(entries, c_hist_t *, ent, {
- char *buf = NULL;
- tor_asprintf(&buf, "%s=%u", ent->country, ent->total);
- smartlist_add(strings, buf);
- });
+ smartlist_add_asprintf(strings, "%s=%u", ent->country, ent->total);
+ });
result = smartlist_join_strings(strings, ",", 0, NULL);
SMARTLIST_FOREACH(strings, char *, cp, tor_free(cp));
SMARTLIST_FOREACH(entries, c_hist_t *, ent, tor_free(ent));
diff --git a/src/or/hibernate.c b/src/or/hibernate.c
index 803f7f5764..917bd8205b 100644
--- a/src/or/hibernate.c
+++ b/src/or/hibernate.c
@@ -976,8 +976,7 @@ getinfo_helper_accounting(control_connection_t *conn,
else
*answer = tor_strdup("awake");
} else if (!strcmp(question, "accounting/bytes")) {
- *answer = tor_malloc(32);
- tor_snprintf(*answer, 32, U64_FORMAT" "U64_FORMAT,
+ tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
U64_PRINTF_ARG(n_bytes_read_in_interval),
U64_PRINTF_ARG(n_bytes_written_in_interval));
} else if (!strcmp(question, "accounting/bytes-left")) {
@@ -987,8 +986,7 @@ getinfo_helper_accounting(control_connection_t *conn,
read_left = limit - n_bytes_read_in_interval;
if (n_bytes_written_in_interval < limit)
write_left = limit - n_bytes_written_in_interval;
- *answer = tor_malloc(64);
- tor_snprintf(*answer, 64, U64_FORMAT" "U64_FORMAT,
+ tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
U64_PRINTF_ARG(read_left), U64_PRINTF_ARG(write_left));
} else if (!strcmp(question, "accounting/interval-start")) {
*answer = tor_malloc(ISO_TIME_LEN+1);
diff --git a/src/or/networkstatus.c b/src/or/networkstatus.c
index a835e78b85..a4e8644f23 100644
--- a/src/or/networkstatus.c
+++ b/src/or/networkstatus.c
@@ -598,39 +598,38 @@ networkstatus_check_consensus_signature(networkstatus_t *consensus,
hex_str(ds->v3_identity_digest, DIGEST_LEN));
});
{
+ char *joined;
smartlist_t *sl = smartlist_create();
- char *cp;
char *tmp = smartlist_join_strings(list_good, " ", 0, NULL);
- tor_asprintf(&cp, "A consensus needs %d good signatures from recognized "
+ smartlist_add_asprintf(sl,
+ "A consensus needs %d good signatures from recognized "
"authorities for us to accept it. This one has %d (%s).",
n_required, n_good, tmp);
tor_free(tmp);
- smartlist_add(sl,cp);
if (n_no_signature) {
tmp = smartlist_join_strings(list_no_signature, " ", 0, NULL);
- tor_asprintf(&cp, "%d (%s) of the authorities we know didn't sign it.",
+ smartlist_add_asprintf(sl,
+ "%d (%s) of the authorities we know didn't sign it.",
n_no_signature, tmp);
tor_free(tmp);
- smartlist_add(sl,cp);
}
if (n_unknown) {
- tor_asprintf(&cp, "It has %d signatures from authorities we don't "
+ smartlist_add_asprintf(sl,
+ "It has %d signatures from authorities we don't "
"recognize.", n_unknown);
- smartlist_add(sl,cp);
}
if (n_bad) {
- tor_asprintf(&cp, "%d of the signatures on it didn't verify "
+ smartlist_add_asprintf(sl, "%d of the signatures on it didn't verify "
"correctly.", n_bad);
- smartlist_add(sl,cp);
}
if (n_missing_key) {
- tor_asprintf(&cp, "We were unable to check %d of the signatures, "
+ smartlist_add_asprintf(sl,
+ "We were unable to check %d of the signatures, "
"because we were missing the keys.", n_missing_key);
- smartlist_add(sl,cp);
}
- cp = smartlist_join_strings(sl, " ", 0, NULL);
- log(severity, LD_DIR, "%s", cp);
- tor_free(cp);
+ joined = smartlist_join_strings(sl, " ", 0, NULL);
+ log(severity, LD_DIR, "%s", joined);
+ tor_free(joined);
SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
smartlist_free(sl);
}
@@ -2160,9 +2159,8 @@ networkstatus_dump_bridge_status_to_file(time_t now)
{
char *status = networkstatus_getinfo_by_purpose("bridge", now);
const or_options_t *options = get_options();
- size_t len = strlen(options->DataDirectory) + 32;
- char *fname = tor_malloc(len);
- tor_snprintf(fname, len, "%s"PATH_SEPARATOR"networkstatus-bridges",
+ char *fname = NULL;
+ tor_asprintf(&fname, "%s"PATH_SEPARATOR"networkstatus-bridges",
options->DataDirectory);
write_str_to_file(fname,status,0);
tor_free(fname);
diff --git a/src/or/ntmain.c b/src/or/ntmain.c
index 8d03ea8087..da19573a46 100644
--- a/src/or/ntmain.c
+++ b/src/or/ntmain.c
@@ -456,9 +456,9 @@ nt_service_command_line(int *using_default_torrc)
{
TCHAR tor_exe[MAX_PATH+1];
char tor_exe_ascii[MAX_PATH+1];
- char *command, *options=NULL;
+ char *command=NULL, *options=NULL;
smartlist_t *sl;
- int i, cmdlen;
+ int i;
*using_default_torrc = 1;
/* Get the location of tor.exe */
@@ -487,21 +487,13 @@ nt_service_command_line(int *using_default_torrc)
strlcpy(tor_exe_ascii, tor_exe, sizeof(tor_exe_ascii));
#endif
- /* Allocate a string for the NT service command line */
- cmdlen = strlen(tor_exe_ascii) + (options?strlen(options):0) + 32;
- command = tor_malloc(cmdlen);
-
+ /* Allocate a string for the NT service command line and */
/* Format the service command */
if (options) {
- if (tor_snprintf(command, cmdlen, "\"%s\" --nt-service \"%s\"",
- tor_exe_ascii, options)<0) {
- tor_free(command); /* sets command to NULL. */
- }
+ tor_asprintf(&command, "\"%s\" --nt-service \"%s\"",
+ tor_exe_ascii, options);
} else { /* ! options */
- if (tor_snprintf(command, cmdlen, "\"%s\" --nt-service",
- tor_exe_ascii)<0) {
- tor_free(command); /* sets command to NULL. */
- }
+ tor_asprintf(&command, "\"%s\" --nt-service", tor_exe_ascii);
}
tor_free(options);
diff --git a/src/or/policies.c b/src/or/policies.c
index 34cb64bd81..53cdf48403 100644
--- a/src/or/policies.c
+++ b/src/or/policies.c
@@ -1218,7 +1218,7 @@ policy_summarize(smartlist_t *policy)
smartlist_t *summary = policy_summary_create();
smartlist_t *accepts, *rejects;
int i, last, start_prt;
- size_t accepts_len, rejects_len, shorter_len, final_size;
+ size_t accepts_len, rejects_len;
char *accepts_str = NULL, *rejects_str = NULL, *shorter_str, *result;
const char *prefix;
@@ -1290,21 +1290,15 @@ policy_summarize(smartlist_t *policy)
tor_assert(*c == ',');
*c = '\0';
- shorter_len = strlen(shorter_str);
} else if (rejects_len < accepts_len) {
shorter_str = rejects_str;
- shorter_len = rejects_len;
prefix = "reject";
} else {
shorter_str = accepts_str;
- shorter_len = accepts_len;
prefix = "accept";
}
- final_size = strlen(prefix)+1+shorter_len+1;
- tor_assert(final_size <= MAX_EXITPOLICY_SUMMARY_LEN+1);
- result = tor_malloc(final_size);
- tor_snprintf(result, final_size, "%s %s", prefix, shorter_str);
+ tor_asprintf(&result, "%s %s", prefix, shorter_str);
cleanup:
/* cleanup */
diff --git a/src/or/rephist.c b/src/or/rephist.c
index 6bbb93b821..4129fe2bbd 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);
@@ -1588,7 +1586,6 @@ rep_hist_update_bwhist_state_section(or_state_t *state,
time_t *s_begins,
int *s_interval)
{
- char *cp;
int i,j;
uint64_t maxval;
@@ -1626,17 +1623,17 @@ rep_hist_update_bwhist_state_section(or_state_t *state,
for (j=0; j < b->num_maxes_set; ++j,++i) {
if (i >= NUM_TOTALS)
i = 0;
- tor_asprintf(&cp, U64_FORMAT, U64_PRINTF_ARG(b->totals[i] & ~0x3ff));
- smartlist_add(*s_values, cp);
+ smartlist_add_asprintf(*s_values, U64_FORMAT,
+ U64_PRINTF_ARG(b->totals[i] & ~0x3ff));
maxval = b->maxima[i] / NUM_SECS_ROLLING_MEASURE;
- tor_asprintf(&cp, U64_FORMAT, U64_PRINTF_ARG(maxval & ~0x3ff));
- smartlist_add(*s_maxima, cp);
+ smartlist_add_asprintf(*s_maxima, U64_FORMAT,
+ U64_PRINTF_ARG(maxval & ~0x3ff));
}
- tor_asprintf(&cp, U64_FORMAT, U64_PRINTF_ARG(b->total_in_period & ~0x3ff));
- smartlist_add(*s_values, cp);
+ smartlist_add_asprintf(*s_values, U64_FORMAT,
+ U64_PRINTF_ARG(b->total_in_period & ~0x3ff));
maxval = b->max_total / NUM_SECS_ROLLING_MEASURE;
- tor_asprintf(&cp, U64_FORMAT, U64_PRINTF_ARG(maxval & ~0x3ff));
- smartlist_add(*s_maxima, cp);
+ smartlist_add_asprintf(*s_maxima, U64_FORMAT,
+ U64_PRINTF_ARG(maxval & ~0x3ff));
}
/** Update <b>state</b> with the newest bandwidth history. Done before
@@ -2125,7 +2122,6 @@ rep_hist_format_exit_stats(time_t now)
uint64_t cur_bytes = 0, other_read = 0, other_written = 0,
total_read = 0, total_written = 0;
uint32_t total_streams = 0, other_streams = 0;
- char *buf;
smartlist_t *written_strings, *read_strings, *streams_strings;
char *written_string, *read_string, *streams_string;
char t[ISO_TIME_LEN+1];
@@ -2204,9 +2200,8 @@ rep_hist_format_exit_stats(time_t now)
exit_bytes_written[cur_port],
EXIT_STATS_ROUND_UP_BYTES);
num /= 1024;
- buf = NULL;
- tor_asprintf(&buf, "%d="U64_FORMAT, cur_port, U64_PRINTF_ARG(num));
- smartlist_add(written_strings, buf);
+ smartlist_add_asprintf(written_strings, "%d="U64_FORMAT,
+ cur_port, U64_PRINTF_ARG(num));
other_written -= exit_bytes_written[cur_port];
}
if (exit_bytes_read[cur_port] > 0) {
@@ -2214,18 +2209,15 @@ rep_hist_format_exit_stats(time_t now)
exit_bytes_read[cur_port],
EXIT_STATS_ROUND_UP_BYTES);
num /= 1024;
- buf = NULL;
- tor_asprintf(&buf, "%d="U64_FORMAT, cur_port, U64_PRINTF_ARG(num));
- smartlist_add(read_strings, buf);
+ smartlist_add_asprintf(read_strings, "%d="U64_FORMAT,
+ cur_port, U64_PRINTF_ARG(num));
other_read -= exit_bytes_read[cur_port];
}
if (exit_streams[cur_port] > 0) {
uint32_t num = round_uint32_to_next_multiple_of(
exit_streams[cur_port],
EXIT_STATS_ROUND_UP_STREAMS);
- buf = NULL;
- tor_asprintf(&buf, "%d=%u", cur_port, num);
- smartlist_add(streams_strings, buf);
+ smartlist_add_asprintf(streams_strings, "%d=%u", cur_port, num);
other_streams -= exit_streams[cur_port];
}
}
@@ -2234,20 +2226,16 @@ rep_hist_format_exit_stats(time_t now)
other_written = round_uint64_to_next_multiple_of(other_written,
EXIT_STATS_ROUND_UP_BYTES);
other_written /= 1024;
- buf = NULL;
- tor_asprintf(&buf, "other="U64_FORMAT, U64_PRINTF_ARG(other_written));
- smartlist_add(written_strings, buf);
+ smartlist_add_asprintf(written_strings, "other="U64_FORMAT,
+ U64_PRINTF_ARG(other_written));
other_read = round_uint64_to_next_multiple_of(other_read,
EXIT_STATS_ROUND_UP_BYTES);
other_read /= 1024;
- buf = NULL;
- tor_asprintf(&buf, "other="U64_FORMAT, U64_PRINTF_ARG(other_read));
- smartlist_add(read_strings, buf);
+ smartlist_add_asprintf(read_strings, "other="U64_FORMAT,
+ U64_PRINTF_ARG(other_read));
other_streams = round_uint32_to_next_multiple_of(other_streams,
EXIT_STATS_ROUND_UP_STREAMS);
- buf = NULL;
- tor_asprintf(&buf, "other=%u", other_streams);
- smartlist_add(streams_strings, buf);
+ smartlist_add_asprintf(streams_strings, "other=%u", other_streams);
/* Join all observations in single strings. */
written_string = smartlist_join_strings(written_strings, ",", 0, NULL);
@@ -2468,7 +2456,6 @@ rep_hist_format_buffer_stats(time_t now)
int processed_cells[SHARES], circs_in_share[SHARES],
number_of_circuits, i;
double queued_cells[SHARES], time_in_queue[SHARES];
- char *buf = NULL;
smartlist_t *processed_cells_strings, *queued_cells_strings,
*time_in_queue_strings;
char *processed_cells_string, *queued_cells_string,
@@ -2510,19 +2497,19 @@ rep_hist_format_buffer_stats(time_t now)
queued_cells_strings = smartlist_create();
time_in_queue_strings = smartlist_create();
for (i = 0; i < SHARES; i++) {
- tor_asprintf(&buf,"%d", !circs_in_share[i] ? 0 :
- processed_cells[i] / circs_in_share[i]);
- smartlist_add(processed_cells_strings, buf);
+ smartlist_add_asprintf(processed_cells_strings,
+ "%d", !circs_in_share[i] ? 0 :
+ processed_cells[i] / circs_in_share[i]);
}
for (i = 0; i < SHARES; i++) {
- tor_asprintf(&buf, "%.2f", circs_in_share[i] == 0 ? 0.0 :
- queued_cells[i] / (double) circs_in_share[i]);
- smartlist_add(queued_cells_strings, buf);
+ smartlist_add_asprintf(queued_cells_strings, "%.2f",
+ circs_in_share[i] == 0 ? 0.0 :
+ queued_cells[i] / (double) circs_in_share[i]);
}
for (i = 0; i < SHARES; i++) {
- tor_asprintf(&buf, "%.0f", circs_in_share[i] == 0 ? 0.0 :
- time_in_queue[i] / (double) circs_in_share[i]);
- smartlist_add(time_in_queue_strings, buf);
+ smartlist_add_asprintf(time_in_queue_strings, "%.0f",
+ circs_in_share[i] == 0 ? 0.0 :
+ time_in_queue[i] / (double) circs_in_share[i]);
}
/* Join all observations in single strings. */
diff --git a/src/or/router.c b/src/or/router.c
index b2df3834fe..0339e682a8 100644
--- a/src/or/router.c
+++ b/src/or/router.c
@@ -1973,11 +1973,9 @@ 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("");
@@ -2267,9 +2265,7 @@ extrainfo_dump_to_string(char **s_out, extrainfo_t *extrainfo,
smartlist_add(chunks, pre);
if (geoip_is_loaded()) {
- char *chunk=NULL;
- tor_asprintf(&chunk, "geoip-db-digest %s\n", geoip_db_digest());
- smartlist_add(chunks, chunk);
+ smartlist_add_asprintf(chunks, "geoip-db-digest %s\n", geoip_db_digest());
}
if (options->ExtraInfoStatistics && write_stats_to_extrainfo) {
diff --git a/src/or/routerlist.c b/src/or/routerlist.c
index 689df99c57..b81289e856 100644
--- a/src/or/routerlist.c
+++ b/src/or/routerlist.c
@@ -2408,8 +2408,6 @@ router_get_by_nickname(const char *nickname, int warn_if_unnamed)
int any_unwarned = 0;
SMARTLIST_FOREACH_BEGIN(routerlist->routers, routerinfo_t *, router) {
routerstatus_t *rs;
- char *desc;
- size_t dlen;
char fp[HEX_DIGEST_LEN+1];
if (strcasecmp(router->nickname, nickname))
continue;
@@ -2421,11 +2419,8 @@ router_get_by_nickname(const char *nickname, int warn_if_unnamed)
}
base16_encode(fp, sizeof(fp),
router->cache_info.identity_digest, DIGEST_LEN);
- dlen = 32 + HEX_DIGEST_LEN + strlen(router->address);
- desc = tor_malloc(dlen);
- tor_snprintf(desc, dlen, "\"$%s\" for the one at %s:%d",
+ smartlist_add_asprintf(fps, "\"$%s\" for the one at %s:%d",
fp, router->address, router->or_port);
- smartlist_add(fps, desc);
} SMARTLIST_FOREACH_END(router);
if (any_unwarned) {
char *alternatives = smartlist_join_strings(fps, "; ",0,NULL);
@@ -4071,7 +4066,6 @@ add_trusted_dir_server(const char *nickname, const char *address,
trusted_dir_server_t *ent;
uint32_t a;
char *hostname = NULL;
- size_t dlen;
if (!trusted_dir_servers)
trusted_dir_servers = smartlist_create();
@@ -4104,13 +4098,11 @@ add_trusted_dir_server(const char *nickname, const char *address,
if (v3_auth_digest && (type & V3_DIRINFO))
memcpy(ent->v3_identity_digest, v3_auth_digest, DIGEST_LEN);
- dlen = 64 + strlen(hostname) + (nickname?strlen(nickname):0);
- ent->description = tor_malloc(dlen);
if (nickname)
- tor_snprintf(ent->description, dlen, "directory server \"%s\" at %s:%d",
+ tor_asprintf(&ent->description, "directory server \"%s\" at %s:%d",
nickname, hostname, (int)dir_port);
else
- tor_snprintf(ent->description, dlen, "directory server at %s:%d",
+ tor_asprintf(&ent->description, "directory server at %s:%d",
hostname, (int)dir_port);
ent->fake_status.addr = ent->addr;
@@ -5333,7 +5325,6 @@ esc_router_info(const routerinfo_t *router)
{
static char *info=NULL;
char *esc_contact, *esc_platform;
- size_t len;
tor_free(info);
if (!router)
@@ -5342,10 +5333,7 @@ esc_router_info(const routerinfo_t *router)
esc_contact = esc_for_log(router->contact_info);
esc_platform = esc_for_log(router->platform);
- len = strlen(esc_contact)+strlen(esc_platform)+32;
- info = tor_malloc(len);
- tor_snprintf(info, len, "Contact %s, Platform %s", esc_contact,
- esc_platform);
+ tor_asprintf(&info, "Contact %s, Platform %s", esc_contact, esc_platform);
tor_free(esc_contact);
tor_free(esc_platform);
diff --git a/src/or/transports.c b/src/or/transports.c
index abf9d884f7..d8d2ff6aa6 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -918,7 +918,7 @@ parse_cmethod_line(const char *line, managed_proxy_t *mp)
static char *
get_bindaddr_for_proxy(const managed_proxy_t *mp)
{
- char *bindaddr = NULL;
+ char *bindaddr_result = NULL;
char *bindaddr_tmp = NULL;
smartlist_t *string_tmp = smartlist_create();
@@ -927,18 +927,17 @@ get_bindaddr_for_proxy(const managed_proxy_t *mp)
SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, char *, t) {
bindaddr_tmp = get_bindaddr_for_transport(t);
- tor_asprintf(&bindaddr, "%s-%s", t, bindaddr_tmp);
- smartlist_add(string_tmp, bindaddr);
+ smartlist_add_asprintf(string_tmp, "%s-%s", t, bindaddr_tmp);
tor_free(bindaddr_tmp);
} SMARTLIST_FOREACH_END(t);
- bindaddr = smartlist_join_strings(string_tmp, ",", 0, NULL);
+ bindaddr_result = smartlist_join_strings(string_tmp, ",", 0, NULL);
SMARTLIST_FOREACH(string_tmp, char *, t, tor_free(t));
smartlist_free(string_tmp);
- return bindaddr;
+ return bindaddr_result;
}
#ifdef MS_WINDOWS