Merge branch 'bug4889_v2'

This commit is contained in:
Nick Mathewson 2012-01-16 15:07:58 -05:00
commit 2bd40a8eb0
20 changed files with 201 additions and 438 deletions

5
changes/clean_asprintf Normal file
View File

@ -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.

View File

@ -1351,31 +1351,19 @@ log_credential_status(void)
return -1; return -1;
} else { } else {
int i, retval = 0; int i, retval = 0;
char *strgid;
char *s = NULL; char *s = NULL;
smartlist_t *elts = smartlist_create(); smartlist_t *elts = smartlist_create();
for (i = 0; i<ngids; i++) { for (i = 0; i<ngids; i++) {
strgid = tor_malloc(11); smartlist_add_asprintf(elts, "%u", (unsigned)sup_gids[i]);
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);
} }
s = smartlist_join_strings(elts, " ", 0, NULL); s = smartlist_join_strings(elts, " ", 0, NULL);
log_fn(CREDENTIAL_LOG_LEVEL, LD_GENERAL, "Supplementary groups are: %s",s); log_fn(CREDENTIAL_LOG_LEVEL, LD_GENERAL, "Supplementary groups are: %s",s);
error:
tor_free(s); tor_free(s);
SMARTLIST_FOREACH(elts, char *, cp, SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
{
tor_free(cp);
});
smartlist_free(elts); smartlist_free(elts);
tor_free(sup_gids); tor_free(sup_gids);

View File

@ -1964,7 +1964,6 @@ int
start_writing_to_file(const char *fname, int open_flags, int mode, start_writing_to_file(const char *fname, int open_flags, int mode,
open_file_t **data_out) open_file_t **data_out)
{ {
size_t tempname_len = strlen(fname)+16;
open_file_t *new_file = tor_malloc_zero(sizeof(open_file_t)); open_file_t *new_file = tor_malloc_zero(sizeof(open_file_t));
const char *open_name; const char *open_name;
int append = 0; 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); tor_assert((open_flags & (O_BINARY|O_TEXT)) != 0);
#endif #endif
new_file->fd = -1; new_file->fd = -1;
tor_assert(tempname_len > strlen(fname)); /*check for overflow*/
new_file->filename = tor_strdup(fname); new_file->filename = tor_strdup(fname);
if (open_flags & O_APPEND) { if (open_flags & O_APPEND) {
open_name = fname; open_name = fname;
@ -1983,11 +1981,8 @@ start_writing_to_file(const char *fname, int open_flags, int mode,
append = 1; append = 1;
open_flags &= ~O_APPEND; open_flags &= ~O_APPEND;
} else { } else {
open_name = new_file->tempname = tor_malloc(tempname_len); tor_asprintf(&new_file->tempname, "%s.tmp", fname);
if (tor_snprintf(new_file->tempname, tempname_len, "%s.tmp", fname)<0) { open_name = new_file->tempname;
log_warn(LD_GENERAL, "Failed to generate filename");
goto err;
}
/* We always replace an existing temporary file if there is one. */ /* We always replace an existing temporary file if there is one. */
open_flags |= O_CREAT|O_TRUNC; open_flags |= O_CREAT|O_TRUNC;
open_flags &= ~O_EXCL; 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>) /** Append the string produced by tor_asprintf(<b>pattern</b>, <b>...</b>)
* to <b>sl</b>. */ * to <b>sl</b>. */
void 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_list ap;
va_start(ap, pattern); va_start(ap, pattern);
smartlist_vasprintf_add(sl, pattern, ap); smartlist_add_vasprintf(sl, pattern, ap);
va_end(ap); va_end(ap);
} }
/** va_list-based backend of smartlist_asprintf_add. */ /** va_list-based backend of smartlist_add_asprintf. */
void void
smartlist_vasprintf_add(struct smartlist_t *sl, const char *pattern, smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern,
va_list args) va_list args)
{ {
char *str = NULL; char *str = NULL;
@ -2786,14 +2781,12 @@ tor_listdir(const char *dirname)
{ {
smartlist_t *result; smartlist_t *result;
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
char *pattern; char *pattern=NULL;
TCHAR tpattern[MAX_PATH] = {0}; TCHAR tpattern[MAX_PATH] = {0};
char name[MAX_PATH] = {0}; char name[MAX_PATH] = {0};
HANDLE handle; HANDLE handle;
WIN32_FIND_DATA findData; WIN32_FIND_DATA findData;
size_t pattern_len = strlen(dirname)+16; tor_asprintf(&pattern, "%s\\*", dirname);
pattern = tor_malloc(pattern_len);
tor_snprintf(pattern, pattern_len, "%s\\*", dirname);
#ifdef UNICODE #ifdef UNICODE
mbstowcs(tpattern,pattern,MAX_PATH); mbstowcs(tpattern,pattern,MAX_PATH);
#else #else

View File

@ -218,9 +218,9 @@ int tor_sscanf(const char *buf, const char *pattern, ...)
#endif #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); 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); va_list args);
int hex_decode_digit(char c); int hex_decode_digit(char c);

View File

@ -675,8 +675,7 @@ circuit_build_times_update_state(circuit_build_times_t *cbt,
if (histogram[i] == 0) continue; if (histogram[i] == 0) continue;
*next = line = tor_malloc_zero(sizeof(config_line_t)); *next = line = tor_malloc_zero(sizeof(config_line_t));
line->key = tor_strdup("CircuitBuildTimeBin"); line->key = tor_strdup("CircuitBuildTimeBin");
line->value = tor_malloc(25); tor_asprintf(&line->value, "%d %d",
tor_snprintf(line->value, 25, "%d %d",
CBT_BIN_TO_MS(i), histogram[i]); CBT_BIN_TO_MS(i), histogram[i]);
next = &(line->next); next = &(line->next);
} }
@ -1545,15 +1544,13 @@ circuit_list_path_impl(origin_circuit_t *circ, int verbose, int verbose_names)
if (verbose) { if (verbose) {
const char *nickname = build_state_get_exit_nickname(circ->build_state); const char *nickname = build_state_get_exit_nickname(circ->build_state);
char *cp; smartlist_add_asprintf(elements, "%s%s circ (length %d%s%s):",
tor_asprintf(&cp, "%s%s circ (length %d%s%s):",
circ->build_state->is_internal ? "internal" : "exit", circ->build_state->is_internal ? "internal" : "exit",
circ->build_state->need_uptime ? " (high-uptime)" : "", circ->build_state->need_uptime ? " (high-uptime)" : "",
circ->build_state->desired_path_len, circ->build_state->desired_path_len,
circ->_base.state == CIRCUIT_STATE_OPEN ? "" : ", last hop ", circ->_base.state == CIRCUIT_STATE_OPEN ? "" : ", last hop ",
circ->_base.state == CIRCUIT_STATE_OPEN ? "" : circ->_base.state == CIRCUIT_STATE_OPEN ? "" :
(nickname?nickname:"*unnamed*")); (nickname?nickname:"*unnamed*"));
smartlist_add(elements, cp);
} }
hop = circ->cpath; hop = circ->cpath;
@ -1594,11 +1591,8 @@ circuit_list_path_impl(origin_circuit_t *circ, int verbose, int verbose_names)
} }
tor_assert(elt); tor_assert(elt);
if (verbose) { if (verbose) {
size_t len = strlen(elt)+2+strlen(states[hop->state])+1;
char *v = tor_malloc(len);
tor_assert(hop->state <= 2); tor_assert(hop->state <= 2);
tor_snprintf(v,len,"%s(%s)",elt,states[hop->state]); smartlist_add_asprintf(elements,"%s(%s)",elt,states[hop->state]);
smartlist_add(elements, v);
tor_free(elt); tor_free(elt);
} else { } else {
smartlist_add(elements, elt); smartlist_add(elements, elt);
@ -3599,19 +3593,17 @@ log_entry_guards(int severity)
SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e) SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e)
{ {
const char *msg = NULL; const char *msg = NULL;
char *cp;
if (entry_is_live(e, 0, 1, 0, &msg)) 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, e->nickname,
hex_str(e->identity, DIGEST_LEN), hex_str(e->identity, DIGEST_LEN),
e->made_contact ? "made-contact" : "never-contacted"); e->made_contact ? "made-contact" : "never-contacted");
else else
tor_asprintf(&cp, "%s [%s] (%s, %s)", smartlist_add_asprintf(elements, "%s [%s] (%s, %s)",
e->nickname, e->nickname,
hex_str(e->identity, DIGEST_LEN), hex_str(e->identity, DIGEST_LEN),
msg, msg,
e->made_contact ? "made-contact" : "never-contacted"); e->made_contact ? "made-contact" : "never-contacted");
smartlist_add(elements, cp);
} }
SMARTLIST_FOREACH_END(e); 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"; msg = "does not seem to be from any recognized version of Tor";
version_is_bad = 1; version_is_bad = 1;
} else { } else {
size_t len = strlen(ver)+5; char *tor_ver = NULL;
char *tor_ver = tor_malloc(len); tor_asprintf(&tor_ver, "Tor %s", ver);
tor_snprintf(tor_ver, len, "Tor %s", ver);
if ((tor_version_as_new_as(tor_ver, "0.1.0.10-alpha") && 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.1.2.16-dev")) ||
(tor_version_as_new_as(tor_ver, "0.2.0.0-alpha") && (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 */ continue; /* don't write this one to disk */
*next = line = tor_malloc_zero(sizeof(config_line_t)); *next = line = tor_malloc_zero(sizeof(config_line_t));
line->key = tor_strdup("EntryGuard"); 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); base16_encode(dbuf, sizeof(dbuf), e->identity, DIGEST_LEN);
tor_snprintf(line->value,HEX_DIGEST_LEN+MAX_NICKNAME_LEN+2, tor_asprintf(&line->value, "%s %s", e->nickname, dbuf);
"%s %s", e->nickname, dbuf);
next = &(line->next); next = &(line->next);
if (e->unreachable_since) { if (e->unreachable_since) {
*next = line = tor_malloc_zero(sizeof(config_line_t)); *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, ' ')) { !strchr(e->chosen_by_version, ' ')) {
char d[HEX_DIGEST_LEN+1]; char d[HEX_DIGEST_LEN+1];
char t[ISO_TIME_LEN+1]; char t[ISO_TIME_LEN+1];
size_t val_len;
*next = line = tor_malloc_zero(sizeof(config_line_t)); *next = line = tor_malloc_zero(sizeof(config_line_t));
line->key = tor_strdup("EntryGuardAddedBy"); 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); base16_encode(d, sizeof(d), e->identity, DIGEST_LEN);
format_iso_time(t, e->chosen_on_date); 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); d, e->chosen_by_version, t);
next = &(line->next); next = &(line->next);
} }
@ -4512,8 +4497,6 @@ getinfo_helper_entry_guards(control_connection_t *conn,
if (!entry_guards) if (!entry_guards)
entry_guards = smartlist_create(); entry_guards = smartlist_create();
SMARTLIST_FOREACH_BEGIN(entry_guards, entry_guard_t *, e) { 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; const char *status = NULL;
time_t when = 0; time_t when = 0;
const node_t *node; const node_t *node;
@ -4539,11 +4522,10 @@ getinfo_helper_entry_guards(control_connection_t *conn,
if (when) { if (when) {
format_iso_time(tbuf, 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 { } 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); } SMARTLIST_FOREACH_END(e);
*answer = smartlist_join_strings(sl, "", 0, NULL); *answer = smartlist_join_strings(sl, "", 0, NULL);
SMARTLIST_FOREACH(sl, char *, c, tor_free(c)); SMARTLIST_FOREACH(sl, char *, c, tor_free(c));

View File

@ -781,22 +781,20 @@ set_options(or_options_t *new_val, char **msg)
extern const char tor_git_revision[]; /* from tor_main.c */ extern const char tor_git_revision[]; /* from tor_main.c */
/** The version of this Tor process, as parsed. */ /** The version of this Tor process, as parsed. */
static char *_version = NULL; static char *the_tor_version = NULL;
/** Return the current Tor version. */ /** Return the current Tor version. */
const char * const char *
get_version(void) get_version(void)
{ {
if (_version == NULL) { if (the_tor_version == NULL) {
if (strlen(tor_git_revision)) { if (strlen(tor_git_revision)) {
size_t len = strlen(VERSION)+strlen(tor_git_revision)+16; tor_asprintf(&the_tor_version, "%s (git-%s)", VERSION, tor_git_revision);
_version = tor_malloc(len);
tor_snprintf(_version, len, "%s (git-%s)", VERSION, tor_git_revision);
} else { } else {
_version = tor_strdup(VERSION); the_tor_version = tor_strdup(VERSION);
} }
} }
return _version; return the_tor_version;
} }
/** Release additional memory allocated in options /** Release additional memory allocated in options
@ -841,7 +839,7 @@ config_free_all(void)
tor_free(torrc_fname); tor_free(torrc_fname);
tor_free(torrc_defaults_fname); tor_free(torrc_defaults_fname);
tor_free(_version); tor_free(the_tor_version);
tor_free(global_dirfrontpagecontents); tor_free(global_dirfrontpagecontents);
} }
@ -1174,9 +1172,8 @@ options_act_reversible(const or_options_t *old_options, char **msg)
control_ports_write_to_file(); control_ports_write_to_file();
if (directory_caches_v2_dir_info(options)) { if (directory_caches_v2_dir_info(options)) {
size_t len = strlen(options->DataDirectory)+32; char *fn = NULL;
char *fn = tor_malloc(len); tor_asprintf(&fn, "%s"PATH_SEPARATOR"cached-status",
tor_snprintf(fn, len, "%s"PATH_SEPARATOR"cached-status",
options->DataDirectory); options->DataDirectory);
if (check_private_dir(fn, running_tor ? CPD_CREATE : CPD_CHECK, if (check_private_dir(fn, running_tor ? CPD_CREATE : CPD_CHECK,
options->User) < 0) { options->User) < 0) {
@ -1650,10 +1647,8 @@ options_act(const or_options_t *old_options)
#ifdef WIN32 #ifdef WIN32
if (!strcmp(actual_fname, "<default>")) { if (!strcmp(actual_fname, "<default>")) {
const char *conf_root = get_windows_conf_root(); const char *conf_root = get_windows_conf_root();
size_t len = strlen(conf_root)+16;
tor_free(actual_fname); tor_free(actual_fname);
actual_fname = tor_malloc(len+1); tor_asprintf(&actual_fname, "%s\\geoip", conf_root);
tor_snprintf(actual_fname, len, "%s\\geoip", conf_root);
} }
#endif #endif
geoip_load_file(actual_fname, options); 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); line = assigned = get_assigned_option(fmt, options, fmt->vars[i].name, 1);
for (; line; line = line->next) { for (; line; line = line->next) {
char *tmp; smartlist_add_asprintf(elements, "%s%s %s\n",
tor_asprintf(&tmp, "%s%s %s\n",
comment_option ? "# " : "", comment_option ? "# " : "",
line->key, line->value); line->key, line->value);
smartlist_add(elements, tmp);
} }
config_free_lines(assigned); config_free_lines(assigned);
} }
@ -3159,9 +3152,7 @@ config_dump(const config_format_t *fmt, const void *default_options,
if (fmt->extra) { if (fmt->extra) {
line = *(config_line_t**)STRUCT_VAR_P(options, fmt->extra->var_offset); line = *(config_line_t**)STRUCT_VAR_P(options, fmt->extra->var_offset);
for (; line; line = line->next) { for (; line; line = line->next) {
char *tmp; smartlist_add_asprintf(elements, "%s %s\n", line->key, line->value);
tor_asprintf(&tmp, "%s %s\n", line->key, line->value);
smartlist_add(elements, tmp);
} }
} }
@ -3510,11 +3501,8 @@ options_validate(or_options_t *old_options, or_options_t *options,
SMARTLIST_FOREACH(options->FirewallPorts, const char *, portno, SMARTLIST_FOREACH(options->FirewallPorts, const char *, portno,
{ {
int p = atoi(portno); int p = atoi(portno);
char *s;
if (p<0) continue; if (p<0) continue;
s = tor_malloc(16); smartlist_add_asprintf(instead, "*:%d", p);
tor_snprintf(s, 16, "*:%d", p);
smartlist_add(instead, s);
}); });
new_line->value = smartlist_join_strings(instead,",",0,NULL); new_line->value = smartlist_join_strings(instead,",",0,NULL);
/* These have been deprecated since 0.1.1.5-alpha-cvs */ /* 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) { if (rename_old) {
int i = 1; int i = 1;
size_t fn_tmp_len = strlen(fname)+32; char *fn_tmp = NULL;
char *fn_tmp;
tor_assert(fn_tmp_len > strlen(fname)); /*check for overflow*/
fn_tmp = tor_malloc(fn_tmp_len);
while (1) { while (1) {
if (tor_snprintf(fn_tmp, fn_tmp_len, "%s.orig.%d", fname, i)<0) { tor_asprintf(&fn_tmp, "%s.orig.%d", fname, i);
log_warn(LD_BUG, "tor_snprintf failed inexplicably");
tor_free(fn_tmp);
goto err;
}
if (file_status(fn_tmp) == FN_NOENT) if (file_status(fn_tmp) == FN_NOENT)
break; break;
tor_free(fn_tmp);
++i; ++i;
} }
log_notice(LD_CONFIG, "Renaming old configuration file to \"%s\"", fn_tmp); log_notice(LD_CONFIG, "Renaming old configuration file to \"%s\"", fn_tmp);
@ -6612,13 +6594,13 @@ or_state_save_broken(char *fname)
{ {
int i; int i;
file_status_t status; file_status_t status;
size_t len = strlen(fname)+16; char *fname2 = NULL;
char *fname2 = tor_malloc(len);
for (i = 0; i < 100; ++i) { 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); status = file_status(fname2);
if (status == FN_NOENT) if (status == FN_NOENT)
break; break;
tor_free(fname2);
} }
if (i == 100) { if (i == 100) {
log_warn(LD_BUG, "Unable to parse state in \"%s\"; too many saved bad " 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) { for (i = 0; _option_vars[i].name; ++i) {
const config_var_t *var = &_option_vars[i]; const config_var_t *var = &_option_vars[i];
const char *type; const char *type;
char *line;
switch (var->type) { switch (var->type) {
case CONFIG_TYPE_STRING: type = "String"; break; case CONFIG_TYPE_STRING: type = "String"; break;
case CONFIG_TYPE_FILENAME: type = "Filename"; break; case CONFIG_TYPE_FILENAME: type = "Filename"; break;
@ -7018,8 +6999,7 @@ getinfo_helper_config(control_connection_t *conn,
} }
if (!type) if (!type)
continue; continue;
tor_asprintf(&line, "%s %s\n",var->name,type); smartlist_add_asprintf(sl, "%s %s\n",var->name,type);
smartlist_add(sl, line);
} }
*answer = smartlist_join_strings(sl, "", 0, NULL); *answer = smartlist_join_strings(sl, "", 0, NULL);
SMARTLIST_FOREACH(sl, char *, c, tor_free(c)); SMARTLIST_FOREACH(sl, char *, c, tor_free(c));

View File

@ -909,13 +909,10 @@ addressmap_ent_remove(const char *address, addressmap_entry_t *ent)
static void static void
clear_trackexithost_mappings(const char *exitname) clear_trackexithost_mappings(const char *exitname)
{ {
char *suffix; char *suffix = NULL;
size_t suffix_len;
if (!addressmap || !exitname) if (!addressmap || !exitname)
return; return;
suffix_len = strlen(exitname) + 16; tor_asprintf(&suffix, ".%s.exit", exitname);
suffix = tor_malloc(suffix_len);
tor_snprintf(suffix, suffix_len, ".%s.exit", exitname);
tor_strlower(suffix); tor_strlower(suffix);
STRMAP_FOREACH_MODIFY(addressmap, address, addressmap_entry_t *, ent) { 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 static int
addressmap_rewrite_reverse(char *address, size_t maxlen, time_t *expires_out) addressmap_rewrite_reverse(char *address, size_t maxlen, time_t *expires_out)
{ {
size_t len = maxlen + 16; char *s, *cp;
char *s = tor_malloc(len), *cp;
addressmap_entry_t *ent; addressmap_entry_t *ent;
int r = 0; int r = 0;
tor_snprintf(s, len, "REVERSE[%s]", address); tor_asprintf(&s, "REVERSE[%s]", address);
ent = strmap_get(addressmap, s); ent = strmap_get(addressmap, s);
if (ent) { if (ent) {
cp = tor_strdup(escaped_safe_str_client(ent->new_address)); 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, const char *exitname,
int ttl) int ttl)
{ {
size_t len = strlen(address) + 16; char *s = NULL;
char *s = tor_malloc(len); tor_asprintf(&s, "REVERSE[%s]", address);
tor_snprintf(s, len, "REVERSE[%s]", address);
client_dns_set_addressmap_impl(s, v, exitname, ttl); client_dns_set_addressmap_impl(s, v, exitname, ttl);
tor_free(s); tor_free(s);
} }
@ -1689,21 +1684,18 @@ addressmap_get_mappings(smartlist_t *sl, time_t min_expires,
addressmap_ent_remove(key, val); addressmap_ent_remove(key, val);
continue; continue;
} else if (val->new_address) { } 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 (want_expiry) {
if (val->expires < 3 || val->expires == TIME_MAX) 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 { else {
char time[ISO_TIME_LEN+1]; char time[ISO_TIME_LEN+1];
format_iso_time(time, val->expires); 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); time);
} }
} else { } 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); iter = strmap_iter_next(addressmap,iter);

View File

@ -525,18 +525,15 @@ control_ports_write_to_file(void)
lines = smartlist_create(); lines = smartlist_create();
SMARTLIST_FOREACH_BEGIN(get_connection_array(), const connection_t *, conn) { 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) if (conn->type != CONN_TYPE_CONTROL_LISTENER || conn->marked_for_close)
continue; continue;
#ifdef AF_UNIX #ifdef AF_UNIX
if (conn->socket_family == AF_UNIX) { if (conn->socket_family == AF_UNIX) {
tor_asprintf(&port_str, "UNIX_PORT=%s\n", conn->address); smartlist_add_asprintf(lines, "UNIX_PORT=%s\n", conn->address);
smartlist_add(lines, port_str);
continue; continue;
} }
#endif #endif
tor_asprintf(&port_str, "PORT=%s:%d\n", conn->address, conn->port); smartlist_add_asprintf(lines, "PORT=%s:%d\n", conn->address, conn->port);
smartlist_add(lines, port_str);
} SMARTLIST_FOREACH_END(conn); } SMARTLIST_FOREACH_END(conn);
joined = smartlist_join_strings(lines, "", 0, NULL); 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 == '=') { if (*eq == '=') {
char *val=NULL; char *val=NULL;
size_t val_len=0; size_t val_len=0;
size_t ent_len;
if (*body != '\"') { if (*body != '\"') {
char *val_start = body; char *val_start = body;
while (!TOR_ISSPACE(*body)) while (!TOR_ISSPACE(*body))
@ -718,9 +714,7 @@ control_setconf_helper(control_connection_t *conn, uint32_t len, char *body,
return 0; return 0;
} }
} }
ent_len = strlen(key)+val_len+3; tor_asprintf(&entry, "%s %s", key, val);
entry = tor_malloc(ent_len+1);
tor_snprintf(entry, ent_len, "%s %s", key, val);
tor_free(key); tor_free(key);
tor_free(val); tor_free(val);
} else { } 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); config_line_t *answer = option_get_assignment(options,q);
if (!answer) { if (!answer) {
const char *name = option_get_canonical_name(q); const char *name = option_get_canonical_name(q);
size_t alen = strlen(name)+8; smartlist_add_asprintf(answers, "250-%s\r\n", name);
char *astr = tor_malloc(alen);
tor_snprintf(astr, alen, "250-%s\r\n", name);
smartlist_add(answers, astr);
} }
while (answer) { while (answer) {
config_line_t *next; config_line_t *next;
size_t alen = strlen(answer->key)+strlen(answer->value)+8; smartlist_add_asprintf(answers, "250-%s=%s\r\n",
char *astr = tor_malloc(alen);
tor_snprintf(astr, alen, "250-%s=%s\r\n",
answer->key, answer->value); answer->key, answer->value);
smartlist_add(answers, astr);
next = answer->next; next = answer->next;
tor_free(answer->key); tor_free(answer->key);
@ -1307,12 +1295,9 @@ handle_control_mapaddress(control_connection_t *conn, uint32_t len,
if (smartlist_len(elts) == 2) { if (smartlist_len(elts) == 2) {
const char *from = smartlist_get(elts,0); const char *from = smartlist_get(elts,0);
const char *to = smartlist_get(elts,1); 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)) { if (address_is_invalid_destination(to, 1)) {
tor_snprintf(ans, anslen, smartlist_add_asprintf(reply,
"512-syntax error: invalid address '%s'", to); "512-syntax error: invalid address '%s'", to);
smartlist_add(reply, ans);
log_warn(LD_CONTROL, log_warn(LD_CONTROL,
"Skipping invalid argument '%s' in MapAddress msg", to); "Skipping invalid argument '%s' in MapAddress msg", to);
} else if (!strcmp(from, ".") || !strcmp(from, "0.0.0.0")) { } 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, !strcmp(from,".") ? RESOLVED_TYPE_HOSTNAME : RESOLVED_TYPE_IPV4,
tor_strdup(to)); tor_strdup(to));
if (!address) { if (!address) {
tor_snprintf(ans, anslen, smartlist_add_asprintf(reply,
"451-resource exhausted: skipping '%s'", line); "451-resource exhausted: skipping '%s'", line);
smartlist_add(reply, ans);
log_warn(LD_CONTROL, log_warn(LD_CONTROL,
"Unable to allocate address for '%s' in MapAddress msg", "Unable to allocate address for '%s' in MapAddress msg",
safe_str_client(line)); safe_str_client(line));
} else { } else {
tor_snprintf(ans, anslen, "250-%s=%s", address, to); smartlist_add_asprintf(reply, "250-%s=%s", address, to);
smartlist_add(reply, ans);
} }
} else { } else {
addressmap_register(from, tor_strdup(to), 1, addressmap_register(from, tor_strdup(to), 1,
ADDRMAPSRC_CONTROLLER, 0, 0); ADDRMAPSRC_CONTROLLER, 0, 0);
tor_snprintf(ans, anslen, "250-%s", line); smartlist_add_asprintf(reply, "250-%s", line);
smartlist_add(reply, ans);
} }
} else { } else {
size_t anslen = strlen(line)+256; smartlist_add_asprintf(reply, "512-syntax error: mapping '%s' is "
char *ans = tor_malloc(anslen);
tor_snprintf(ans, anslen, "512-syntax error: mapping '%s' is "
"not of expected form 'foo=bar'.", line); "not of expected form 'foo=bar'.", line);
smartlist_add(reply, ans);
log_info(LD_CONTROL, "Skipping MapAddress '%s': wrong " log_info(LD_CONTROL, "Skipping MapAddress '%s': wrong "
"number of items.", "number of items.",
safe_str_client(line)); safe_str_client(line));
@ -1558,7 +1537,6 @@ getinfo_helper_listeners(control_connection_t *control_conn,
res = smartlist_create(); res = smartlist_create();
SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) { SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
char *addr;
struct sockaddr_storage ss; struct sockaddr_storage ss;
socklen_t ss_len = sizeof(ss); socklen_t ss_len = sizeof(ss);
@ -1566,14 +1544,13 @@ getinfo_helper_listeners(control_connection_t *control_conn,
continue; continue;
if (getsockname(conn->s, (struct sockaddr *)&ss, &ss_len) < 0) { 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 { } else {
char *tmp = tor_sockaddr_to_str((struct sockaddr *)&ss); char *tmp = tor_sockaddr_to_str((struct sockaddr *)&ss);
addr = esc_for_log(tmp); smartlist_add(res, esc_for_log(tmp));
tor_free(tmp); tor_free(tmp);
} }
if (addr)
smartlist_add(res, addr);
} SMARTLIST_FOREACH_END(conn); } SMARTLIST_FOREACH_END(conn);
*answer = smartlist_join_strings(res, " ", 0, NULL); *answer = smartlist_join_strings(res, " ", 0, NULL);
@ -1675,13 +1652,13 @@ getinfo_helper_dir(control_connection_t *control_conn,
ri->cache_info.annotations_len); ri->cache_info.annotations_len);
} }
} else if (!strcmpstart(question, "dir/server/")) { } else if (!strcmpstart(question, "dir/server/")) {
size_t answer_len = 0, url_len = strlen(question)+2; size_t answer_len = 0;
char *url = tor_malloc(url_len); char *url = NULL;
smartlist_t *descs = smartlist_create(); smartlist_t *descs = smartlist_create();
const char *msg; const char *msg;
int res; int res;
char *cp; 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); res = dirserv_get_routerdescs(descs, url, &msg);
if (res) { if (res) {
log_warn(LD_CONTROL, "getinfo '%s': %s", question, msg); 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; cpath_build_state_t *build_state = circ->build_state;
smartlist_t *flaglist = smartlist_create(); smartlist_t *flaglist = smartlist_create();
char *flaglist_joined; char *flaglist_joined;
@ -1816,8 +1792,7 @@ circuit_describe_status_for_controller(origin_circuit_t *circ)
if (smartlist_len(flaglist)) { if (smartlist_len(flaglist)) {
flaglist_joined = smartlist_join_strings(flaglist, ",", 0, NULL); flaglist_joined = smartlist_join_strings(flaglist, ",", 0, NULL);
tor_asprintf(&buildflags, "BUILD_FLAGS=%s", flaglist_joined); smartlist_add_asprintf(descparts, "BUILD_FLAGS=%s", flaglist_joined);
smartlist_add(descparts, buildflags);
tor_free(flaglist_joined); tor_free(flaglist_joined);
} }
@ -1825,43 +1800,28 @@ circuit_describe_status_for_controller(origin_circuit_t *circ)
smartlist_free(flaglist); smartlist_free(flaglist);
} }
{ smartlist_add_asprintf(descparts, "PURPOSE=%s",
char *purpose = NULL;
tor_asprintf(&purpose, "PURPOSE=%s",
circuit_purpose_to_controller_string(circ->_base.purpose)); circuit_purpose_to_controller_string(circ->_base.purpose));
smartlist_add(descparts, purpose);
}
{ {
char *hs_state_arg = NULL;
const char *hs_state = const char *hs_state =
circuit_purpose_to_controller_hs_state_string(circ->_base.purpose); circuit_purpose_to_controller_hs_state_string(circ->_base.purpose);
if (hs_state != NULL) { if (hs_state != NULL) {
tor_asprintf(&hs_state_arg, "HS_STATE=%s", smartlist_add_asprintf(descparts, "HS_STATE=%s", hs_state);
hs_state);
smartlist_add(descparts, hs_state_arg);
} }
} }
if (circ->rend_data != NULL) { if (circ->rend_data != NULL) {
char *rend_query_arg = NULL; smartlist_add_asprintf(descparts, "REND_QUERY=%s",
tor_asprintf(&rend_query_arg, "REND_QUERY=%s",
circ->rend_data->onion_address); circ->rend_data->onion_address);
smartlist_add(descparts, rend_query_arg);
} }
{ {
char *time_created_arg = NULL;
char tbuf[ISO_TIME_USEC_LEN+1]; char tbuf[ISO_TIME_USEC_LEN+1];
format_iso_time_nospace_usec(tbuf, &circ->_base.timestamp_created); format_iso_time_nospace_usec(tbuf, &circ->_base.timestamp_created);
tor_asprintf(&time_created_arg, "TIME_CREATED=%s", tbuf); smartlist_add_asprintf(descparts, "TIME_CREATED=%s", tbuf);
smartlist_add(descparts, time_created_arg);
} }
rv = smartlist_join_strings(descparts, " ", 0, NULL); rv = smartlist_join_strings(descparts, " ", 0, NULL);
@ -1885,8 +1845,7 @@ getinfo_helper_events(control_connection_t *control_conn,
smartlist_t *status = smartlist_create(); smartlist_t *status = smartlist_create();
for (circ_ = _circuit_get_global_list(); circ_; circ_ = circ_->next) { for (circ_ = _circuit_get_global_list(); circ_; circ_ = circ_->next) {
origin_circuit_t *circ; origin_circuit_t *circ;
char *s, *circdesc; char *circdesc;
size_t slen;
const char *state; const char *state;
if (! CIRCUIT_IS_ORIGIN(circ_) || circ_->marked_for_close) if (! CIRCUIT_IS_ORIGIN(circ_) || circ_->marked_for_close)
continue; continue;
@ -1901,12 +1860,9 @@ getinfo_helper_events(control_connection_t *control_conn,
circdesc = circuit_describe_status_for_controller(circ); circdesc = circuit_describe_status_for_controller(circ);
slen = strlen(circdesc)+strlen(state)+30; smartlist_add_asprintf(status, "%lu %s%s%s",
s = tor_malloc(slen+1);
tor_snprintf(s, slen, "%lu %s%s%s",
(unsigned long)circ->global_identifier, (unsigned long)circ->global_identifier,
state, *circdesc ? " " : "", circdesc); state, *circdesc ? " " : "", circdesc);
smartlist_add(status, s);
tor_free(circdesc); tor_free(circdesc);
} }
*answer = smartlist_join_strings(status, "\r\n", 0, NULL); *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) { SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
const char *state; const char *state;
entry_connection_t *conn; entry_connection_t *conn;
char *s;
size_t slen;
circuit_t *circ; circuit_t *circ;
origin_circuit_t *origin_circ = NULL; origin_circuit_t *origin_circ = NULL;
if (base_conn->type != CONN_TYPE_AP || 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)) if (circ && CIRCUIT_IS_ORIGIN(circ))
origin_circ = TO_ORIGIN_CIRCUIT(circ); origin_circ = TO_ORIGIN_CIRCUIT(circ);
write_stream_target_to_buf(conn, buf, sizeof(buf)); write_stream_target_to_buf(conn, buf, sizeof(buf));
slen = strlen(buf)+strlen(state)+32; smartlist_add_asprintf(status, "%lu %s %lu %s",
s = tor_malloc(slen+1);
tor_snprintf(s, slen, "%lu %s %lu %s",
(unsigned long) base_conn->global_identifier,state, (unsigned long) base_conn->global_identifier,state,
origin_circ? origin_circ?
(unsigned long)origin_circ->global_identifier : 0ul, (unsigned long)origin_circ->global_identifier : 0ul,
buf); buf);
smartlist_add(status, s);
} SMARTLIST_FOREACH_END(base_conn); } SMARTLIST_FOREACH_END(base_conn);
*answer = smartlist_join_strings(status, "\r\n", 0, NULL); *answer = smartlist_join_strings(status, "\r\n", 0, NULL);
SMARTLIST_FOREACH(status, char *, cp, tor_free(cp)); 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_t *status = smartlist_create();
SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) { SMARTLIST_FOREACH_BEGIN(conns, connection_t *, base_conn) {
const char *state; const char *state;
char *s;
char name[128]; char name[128];
size_t slen;
or_connection_t *conn; or_connection_t *conn;
if (base_conn->type != CONN_TYPE_OR || base_conn->marked_for_close) if (base_conn->type != CONN_TYPE_OR || base_conn->marked_for_close)
continue; continue;
@ -1986,10 +1935,7 @@ getinfo_helper_events(control_connection_t *control_conn,
else else
state = "NEW"; state = "NEW";
orconn_target_get_name(name, sizeof(name), conn); orconn_target_get_name(name, sizeof(name), conn);
slen = strlen(name)+strlen(state)+2; smartlist_add_asprintf(status, "%s %s", name, state);
s = tor_malloc(slen+1);
tor_snprintf(s, slen, "%s %s", name, state);
smartlist_add(status, s);
} SMARTLIST_FOREACH_END(base_conn); } SMARTLIST_FOREACH_END(base_conn);
*answer = smartlist_join_strings(status, "\r\n", 0, NULL); *answer = smartlist_join_strings(status, "\r\n", 0, NULL);
SMARTLIST_FOREACH(status, char *, cp, tor_free(cp)); 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")) { } else if (!strcmp(question, "status/reachability-succeeded/dir")) {
*answer = tor_strdup(check_whether_dirport_reachable() ? "1" : "0"); *answer = tor_strdup(check_whether_dirport_reachable() ? "1" : "0");
} else if (!strcmp(question, "status/reachability-succeeded")) { } else if (!strcmp(question, "status/reachability-succeeded")) {
*answer = tor_malloc(16); tor_asprintf(answer, "OR=%d DIR=%d",
tor_snprintf(*answer, 16, "OR=%d DIR=%d",
check_whether_orport_reachable() ? 1 : 0, check_whether_orport_reachable() ? 1 : 0,
check_whether_dirport_reachable() ? 1 : 0); check_whether_dirport_reachable() ? 1 : 0);
} else if (!strcmp(question, "status/bootstrap-phase")) { } 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") || } else if (!strcmp(question, "status/version/num-versioning") ||
!strcmp(question, "status/version/num-concurring")) { !strcmp(question, "status/version/num-concurring")) {
char s[33]; tor_asprintf(answer, "%d", get_n_authorities(V3_DIRINFO));
tor_snprintf(s, sizeof(s), "%d", get_n_authorities(V3_DIRINFO));
*answer = tor_strdup(s);
log_warn(LD_GENERAL, "%s is deprecated; it no longer gives useful " log_warn(LD_GENERAL, "%s is deprecated; it no longer gives useful "
"information", question); "information", question);
} }
@ -2218,18 +2161,16 @@ static char *
list_getinfo_options(void) list_getinfo_options(void)
{ {
int i; int i;
char *buf=NULL;
smartlist_t *lines = smartlist_create(); smartlist_t *lines = smartlist_create();
char *ans; char *ans;
for (i = 0; getinfo_items[i].varname; ++i) { for (i = 0; getinfo_items[i].varname; ++i) {
if (!getinfo_items[i].desc) if (!getinfo_items[i].desc)
continue; continue;
tor_asprintf(&buf, "%s%s -- %s\n", smartlist_add_asprintf(lines, "%s%s -- %s\n",
getinfo_items[i].varname, getinfo_items[i].varname,
getinfo_items[i].is_prefix ? "*" : "", getinfo_items[i].is_prefix ? "*" : "",
getinfo_items[i].desc); getinfo_items[i].desc);
smartlist_add(lines, buf);
} }
smartlist_sort_strings(lines); 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); const char *reason_str = stream_end_reason_to_control_string(reason_code);
char *r = NULL; char *r = NULL;
if (!reason_str) { if (!reason_str) {
r = tor_malloc(16); tor_asprintf(&r, " UNKNOWN_%d", reason_code);
tor_snprintf(r, 16, " UNKNOWN_%d", reason_code);
reason_str = r; reason_str = r;
} }
if (reason_code & END_STREAM_REASON_FLAG_REMOTE) if (reason_code & END_STREAM_REASON_FLAG_REMOTE)
@ -3837,16 +3777,13 @@ control_event_descriptors_changed(smartlist_t *routers)
{ {
smartlist_t *names = smartlist_create(); smartlist_t *names = smartlist_create();
char *ids; char *ids;
size_t names_len;
SMARTLIST_FOREACH(routers, routerinfo_t *, ri, { SMARTLIST_FOREACH(routers, routerinfo_t *, ri, {
char *b = tor_malloc(MAX_VERBOSE_NICKNAME_LEN+1); char *b = tor_malloc(MAX_VERBOSE_NICKNAME_LEN+1);
router_get_verbose_nickname(b, ri); router_get_verbose_nickname(b, ri);
smartlist_add(names, b); smartlist_add(names, b);
}); });
ids = smartlist_join_strings(names, " ", 0, &names_len); ids = smartlist_join_strings(names, " ", 0, NULL);
names_len = strlen(ids)+32; tor_asprintf(&msg, "650 NEWDESC %s\r\n", ids);
msg = tor_malloc(names_len);
tor_snprintf(msg, names_len, "650 NEWDESC %s\r\n", ids);
send_control_event_string(EVENT_NEW_DESC, ALL_FORMATS, msg); send_control_event_string(EVENT_NEW_DESC, ALL_FORMATS, msg);
tor_free(ids); tor_free(ids);
tor_free(msg); tor_free(msg);
@ -4239,9 +4176,9 @@ control_event_conf_changed(smartlist_t *elements)
char *k = smartlist_get(elements, i); char *k = smartlist_get(elements, i);
char *v = smartlist_get(elements, i+1); char *v = smartlist_get(elements, i+1);
if (v == NULL) { if (v == NULL) {
smartlist_asprintf_add(lines, "650-%s", k); smartlist_add_asprintf(lines, "650-%s", k);
} else { } 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); result = smartlist_join_strings(lines, "\r\n", 0, NULL);

View File

@ -1124,9 +1124,7 @@ directory_send_command(dir_connection_t *conn,
smartlist_t *headers = smartlist_create(); smartlist_t *headers = smartlist_create();
char *url; char *url;
char request[8192]; char request[8192];
char *header;
const char *httpcommand = NULL; const char *httpcommand = NULL;
size_t len;
tor_assert(conn); tor_assert(conn);
tor_assert(conn->_base.type == CONN_TYPE_DIR); tor_assert(conn->_base.type == CONN_TYPE_DIR);
@ -1147,8 +1145,7 @@ directory_send_command(dir_connection_t *conn,
if (if_modified_since) { if (if_modified_since) {
char b[RFC1123_TIME_LEN+1]; char b[RFC1123_TIME_LEN+1];
format_rfc1123_time(b, if_modified_since); format_rfc1123_time(b, if_modified_since);
tor_asprintf(&header, "If-Modified-Since: %s\r\n", b); smartlist_add_asprintf(headers, "If-Modified-Since: %s\r\n", b);
smartlist_add(headers, header);
} }
/* come up with some proxy lines, if we're using one. */ /* 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"); log_warn(LD_BUG, "Encoding http authenticator failed");
} }
if (base64_authenticator) { if (base64_authenticator) {
tor_asprintf(&header, smartlist_add_asprintf(headers,
"Proxy-Authorization: Basic %s\r\n", "Proxy-Authorization: Basic %s\r\n",
base64_authenticator); base64_authenticator);
tor_free(base64_authenticator); tor_free(base64_authenticator);
smartlist_add(headers, header);
} }
} else { } else {
proxystring[0] = 0; proxystring[0] = 0;
@ -1177,9 +1173,7 @@ directory_send_command(dir_connection_t *conn,
case DIR_PURPOSE_FETCH_V2_NETWORKSTATUS: case DIR_PURPOSE_FETCH_V2_NETWORKSTATUS:
tor_assert(resource); tor_assert(resource);
httpcommand = "GET"; httpcommand = "GET";
len = strlen(resource)+32; tor_asprintf(&url, "/tor/status/%s", resource);
url = tor_malloc(len);
tor_snprintf(url, len, "/tor/status/%s", resource);
break; break;
case DIR_PURPOSE_FETCH_CONSENSUS: case DIR_PURPOSE_FETCH_CONSENSUS:
/* resource is optional. If present, it's a flavor name */ /* 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(resource);
tor_assert(!payload); tor_assert(!payload);
httpcommand = "GET"; httpcommand = "GET";
len = strlen(resource)+32; tor_asprintf(&url, "/tor/keys/%s", resource);
url = tor_malloc(len);
tor_snprintf(url, len, "/tor/keys/%s", resource);
break; break;
case DIR_PURPOSE_FETCH_STATUS_VOTE: case DIR_PURPOSE_FETCH_STATUS_VOTE:
tor_assert(resource); tor_assert(resource);
tor_assert(!payload); tor_assert(!payload);
httpcommand = "GET"; httpcommand = "GET";
len = strlen(resource)+32; tor_asprintf(&url, "/tor/status-vote/next/%s.z", resource);
url = tor_malloc(len);
tor_snprintf(url, len, "/tor/status-vote/next/%s.z", resource);
break; break;
case DIR_PURPOSE_FETCH_DETACHED_SIGNATURES: case DIR_PURPOSE_FETCH_DETACHED_SIGNATURES:
tor_assert(!resource); tor_assert(!resource);
@ -1215,16 +1205,12 @@ directory_send_command(dir_connection_t *conn,
case DIR_PURPOSE_FETCH_SERVERDESC: case DIR_PURPOSE_FETCH_SERVERDESC:
tor_assert(resource); tor_assert(resource);
httpcommand = "GET"; httpcommand = "GET";
len = strlen(resource)+32; tor_asprintf(&url, "/tor/server/%s", resource);
url = tor_malloc(len);
tor_snprintf(url, len, "/tor/server/%s", resource);
break; break;
case DIR_PURPOSE_FETCH_EXTRAINFO: case DIR_PURPOSE_FETCH_EXTRAINFO:
tor_assert(resource); tor_assert(resource);
httpcommand = "GET"; httpcommand = "GET";
len = strlen(resource)+32; tor_asprintf(&url, "/tor/extra/%s", resource);
url = tor_malloc(len);
tor_snprintf(url, len, "/tor/extra/%s", resource);
break; break;
case DIR_PURPOSE_FETCH_MICRODESC: case DIR_PURPOSE_FETCH_MICRODESC:
tor_assert(resource); tor_assert(resource);
@ -1238,8 +1224,7 @@ directory_send_command(dir_connection_t *conn,
httpcommand = "POST"; httpcommand = "POST";
url = tor_strdup("/tor/"); url = tor_strdup("/tor/");
if (why) { if (why) {
tor_asprintf(&header, "X-Desc-Gen-Reason: %s\r\n", why); smartlist_add_asprintf(headers, "X-Desc-Gen-Reason: %s\r\n", why);
smartlist_add(headers, header);
} }
break; break;
} }
@ -1260,9 +1245,7 @@ directory_send_command(dir_connection_t *conn,
tor_assert(strlen(resource) <= REND_DESC_ID_V2_LEN_BASE32); tor_assert(strlen(resource) <= REND_DESC_ID_V2_LEN_BASE32);
tor_assert(!payload); tor_assert(!payload);
httpcommand = "GET"; httpcommand = "GET";
len = strlen(resource) + 32; tor_asprintf(&url, "/tor/rendezvous2/%s", resource);
url = tor_malloc(len);
tor_snprintf(url, len, "/tor/rendezvous2/%s", resource);
break; break;
case DIR_PURPOSE_UPLOAD_RENDDESC: case DIR_PURPOSE_UPLOAD_RENDDESC:
tor_assert(!resource); tor_assert(!resource);
@ -1294,15 +1277,16 @@ directory_send_command(dir_connection_t *conn,
tor_free(url); tor_free(url);
if (!strcmp(httpcommand, "POST") || payload) { 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); payload ? (unsigned long)payload_len : 0);
smartlist_add(headers, header);
} }
header = smartlist_join_strings(headers, "", 0, NULL); {
char *header = smartlist_join_strings(headers, "", 0, NULL);
tor_snprintf(request, sizeof(request), " HTTP/1.0\r\nHost: %s\r\n%s\r\n", tor_snprintf(request, sizeof(request), " HTTP/1.0\r\nHost: %s\r\n%s\r\n",
hoststring, header); hoststring, header);
tor_free(header); tor_free(header);
}
connection_write_to_buf(request, strlen(request), TO_CONN(conn)); 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; case DIR_PURPOSE_UPLOAD_RENDDESC_V2: kind = "dl/ul-rend2"; break;
} }
if (kind) { if (kind) {
key = tor_malloc(256); tor_asprintf(&key, "%s%s", kind, compressed?".z":"");
tor_snprintf(key, 256, "%s%s", kind, compressed?".z":"");
} else { } else {
key = tor_malloc(256); tor_asprintf(&key, "unknown purpose (%d)%s",
tor_snprintf(key, 256, "unknown purpose (%d)%s",
purpose, compressed?".z":""); purpose, compressed?".z":"");
} }
note_request(key, bytes); note_request(key, bytes);
@ -2522,7 +2504,6 @@ char *
directory_dump_request_log(void) directory_dump_request_log(void)
{ {
smartlist_t *lines; smartlist_t *lines;
char tmp[256];
char *result; char *result;
strmap_iter_t *iter; strmap_iter_t *iter;
@ -2538,9 +2519,8 @@ directory_dump_request_log(void)
request_t *r; request_t *r;
strmap_iter_get(iter, &key, &val); strmap_iter_get(iter, &key, &val);
r = 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)); key, U64_PRINTF_ARG(r->bytes), U64_PRINTF_ARG(r->count));
smartlist_add(lines, tor_strdup(tmp));
} }
smartlist_sort_strings(lines); smartlist_sort_strings(lines);
result = smartlist_join_strings(lines, "", 0, NULL); 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) #if defined(EXPORTMALLINFO) && defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
#define ADD_MALLINFO_LINE(x) do { \ #define ADD_MALLINFO_LINE(x) do { \
tor_snprintf(tmp, sizeof(tmp), "%s %d\n", #x, mi.x); \ smartlist_add_asprintf(lines, "%s %d\n", #x, mi.x); \
smartlist_add(lines, tor_strdup(tmp)); \
}while(0); }while(0);
if (!strcmp(url,"/tor/mallinfo.txt") && if (!strcmp(url,"/tor/mallinfo.txt") &&
@ -3304,7 +3283,6 @@ directory_handle_command_get(dir_connection_t *conn, const char *headers,
size_t len; size_t len;
struct mallinfo mi; struct mallinfo mi;
smartlist_t *lines; smartlist_t *lines;
char tmp[256];
memset(&mi, 0, sizeof(mi)); memset(&mi, 0, sizeof(mi));
mi = mallinfo(); mi = mallinfo();

View File

@ -2906,9 +2906,7 @@ generate_v2_networkstatus_opinion(void)
contact = "(none)"; contact = "(none)";
if (versioning) { if (versioning) {
size_t v_len = 64+strlen(client_versions)+strlen(server_versions); tor_asprintf(&version_lines,
version_lines = tor_malloc(v_len);
tor_snprintf(version_lines, v_len,
"client-versions %s\nserver-versions %s\n", "client-versions %s\nserver-versions %s\n",
client_versions, server_versions); client_versions, server_versions);
} else { } else {

View File

@ -581,15 +581,13 @@ make_consensus_method_list(int low, int high, const char *separator)
{ {
char *list; char *list;
char b[32];
int i; int i;
smartlist_t *lst; smartlist_t *lst;
lst = smartlist_create(); lst = smartlist_create();
for (i = low; i <= high; ++i) { for (i = low; i <= high; ++i) {
if (!consensus_method_is_supported(i)) if (!consensus_method_is_supported(i))
continue; continue;
tor_snprintf(b, sizeof(b), "%d", i); smartlist_add_asprintf(lst, "%d", i);
smartlist_add(lst, tor_strdup(b));
} }
list = smartlist_join_strings(lst, separator, 0, NULL); list = smartlist_join_strings(lst, separator, 0, NULL);
tor_assert(list); 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 Wmg = -1, Wme = -1, Wmd = -1;
int64_t Wed = -1, Wee = -1; int64_t Wed = -1, Wee = -1;
const char *casename; const char *casename;
char buf[512];
int r;
if (G <= 0 || M <= 0 || E <= 0 || D <= 0) { if (G <= 0 || M <= 0 || E <= 0 || D <= 0) {
log_warn(LD_DIR, "Consensus with empty bandwidth: " 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. * 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 " "bandwidth-weights Wbd=%d Wbe=%d Wbg=%d Wbm=%d "
"Wdb=%d " "Wdb=%d "
"Web=%d Wed=%d Wee=%d Weg=%d Wem=%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)Wed, (int)Wee, (int)Wed, (int)Wee,
(int)weight_scale, (int)Wgd, (int)Wgg, (int)Wgg, (int)weight_scale, (int)Wgd, (int)Wgg, (int)Wgg,
(int)weight_scale, (int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale); (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: " log_notice(LD_CIRC, "Computed bandwidth weights for %s with v10: "
"G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT "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 Wmg = -1, Wme = -1, Wmd = -1;
int64_t Wed = -1, Wee = -1; int64_t Wed = -1, Wee = -1;
const char *casename; const char *casename;
char buf[512];
int r;
if (G <= 0 || M <= 0 || E <= 0 || D <= 0) { if (G <= 0 || M <= 0 || E <= 0 || D <= 0) {
log_warn(LD_DIR, "Consensus with empty bandwidth: " 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. * NOTE: This list is sorted.
*/ */
r = tor_snprintf(buf, sizeof(buf), smartlist_add_asprintf(chunks,
"Wbd=%d Wbe=%d Wbg=%d Wbm=%d " "Wbd=%d Wbe=%d Wbg=%d Wbm=%d "
"Wdb=%d " "Wdb=%d "
"Web=%d Wed=%d Wee=%d Weg=%d Wem=%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)Wed, (int)Wee, (int)Wed, (int)Wee,
(int)weight_scale, (int)Wgd, (int)Wgg, (int)Wgg, (int)weight_scale, (int)Wgd, (int)Wgg, (int)Wgg,
(int)weight_scale, (int)Wmd, (int)Wme, (int)Wmg, (int)weight_scale); (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: " log_notice(LD_CIRC, "Computed bandwidth weights for %s with v9: "
"G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT "G="I64_FORMAT" M="I64_FORMAT" E="I64_FORMAT" D="I64_FORMAT
" T="I64_FORMAT, " T="I64_FORMAT,
@ -1475,7 +1457,6 @@ networkstatus_compute_consensus(smartlist_t *votes,
chunks = smartlist_create(); chunks = smartlist_create();
{ {
char *buf=NULL;
char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1], char va_buf[ISO_TIME_LEN+1], fu_buf[ISO_TIME_LEN+1],
vu_buf[ISO_TIME_LEN+1]; vu_buf[ISO_TIME_LEN+1];
char *flaglist; char *flaglist;
@ -1484,20 +1465,17 @@ networkstatus_compute_consensus(smartlist_t *votes,
format_iso_time(vu_buf, valid_until); format_iso_time(vu_buf, valid_until);
flaglist = smartlist_join_strings(flags, " ", 0, NULL); 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", "vote-status consensus\n",
flavor == FLAV_NS ? "" : " ", flavor == FLAV_NS ? "" : " ",
flavor == FLAV_NS ? "" : flavor_name); flavor == FLAV_NS ? "" : flavor_name);
smartlist_add(chunks, buf);
if (consensus_method >= 2) { if (consensus_method >= 2) {
tor_asprintf(&buf, "consensus-method %d\n", smartlist_add_asprintf(chunks, "consensus-method %d\n",
consensus_method); consensus_method);
smartlist_add(chunks, buf);
} }
tor_asprintf(&buf, smartlist_add_asprintf(chunks,
"valid-after %s\n" "valid-after %s\n"
"fresh-until %s\n" "fresh-until %s\n"
"valid-until %s\n" "valid-until %s\n"
@ -1508,7 +1486,6 @@ networkstatus_compute_consensus(smartlist_t *votes,
va_buf, fu_buf, vu_buf, va_buf, fu_buf, vu_buf,
vote_seconds, dist_seconds, vote_seconds, dist_seconds,
client_versions, server_versions, flaglist); client_versions, server_versions, flaglist);
smartlist_add(chunks, buf);
tor_free(flaglist); tor_free(flaglist);
} }
@ -1550,7 +1527,6 @@ networkstatus_compute_consensus(smartlist_t *votes,
char votedigest[HEX_DIGEST_LEN+1]; char votedigest[HEX_DIGEST_LEN+1];
networkstatus_t *v = e->v; networkstatus_t *v = e->v;
networkstatus_voter_info_t *voter = get_voter(v); networkstatus_voter_info_t *voter = get_voter(v);
char *buf = NULL;
if (e->is_legacy) if (e->is_legacy)
tor_assert(consensus_method >= 2); tor_assert(consensus_method >= 2);
@ -1559,20 +1535,18 @@ networkstatus_compute_consensus(smartlist_t *votes,
base16_encode(votedigest, sizeof(votedigest), voter->vote_digest, base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
DIGEST_LEN); DIGEST_LEN);
tor_asprintf(&buf, smartlist_add_asprintf(chunks,
"dir-source %s%s %s %s %s %d %d\n", "dir-source %s%s %s %s %s %d %d\n",
voter->nickname, e->is_legacy ? "-legacy" : "", voter->nickname, e->is_legacy ? "-legacy" : "",
fingerprint, voter->address, fmt_addr32(voter->addr), fingerprint, voter->address, fmt_addr32(voter->addr),
voter->dir_port, voter->dir_port,
voter->or_port); voter->or_port);
smartlist_add(chunks, buf);
if (! e->is_legacy) { if (! e->is_legacy) {
tor_asprintf(&buf, smartlist_add_asprintf(chunks,
"contact %s\n" "contact %s\n"
"vote-digest %s\n", "vote-digest %s\n",
voter->contact, voter->contact,
votedigest); votedigest);
smartlist_add(chunks, buf);
} }
} SMARTLIST_FOREACH_END(e); } SMARTLIST_FOREACH_END(e);
SMARTLIST_FOREACH(dir_sources, dir_src_ent_t *, e, tor_free(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 naming_conflict = 0;
int n_listing = 0; int n_listing = 0;
int i; int i;
char *buf=NULL;
char microdesc_digest[DIGEST256_LEN]; char microdesc_digest[DIGEST256_LEN];
/* Of the next-to-be-considered digest in each voter, which is first? */ /* 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. */ /* Now an m line, if applicable. */
if (flavor == FLAV_MICRODESC && if (flavor == FLAV_MICRODESC &&
!tor_digest256_is_zero(microdesc_digest)) { !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); digest256_to_base64(m, microdesc_digest);
tor_asprintf(&cp, "m %s\n", m); smartlist_add_asprintf(chunks, "m %s\n", m);
smartlist_add(chunks, cp);
} }
/* Next line is all flags. The "\n" is missing. */ /* Next line is all flags. The "\n" is missing. */
smartlist_add(chunks, smartlist_add(chunks,
@ -1993,15 +1965,12 @@ networkstatus_compute_consensus(smartlist_t *votes,
smartlist_add(chunks, tor_strdup("\n")); smartlist_add(chunks, tor_strdup("\n"));
/* Now the weight line. */ /* Now the weight line. */
if (rs_out.has_bandwidth) { if (rs_out.has_bandwidth) {
char *cp=NULL; smartlist_add_asprintf(chunks, "w Bandwidth=%d\n", rs_out.bandwidth);
tor_asprintf(&cp, "w Bandwidth=%d\n", rs_out.bandwidth);
smartlist_add(chunks, cp);
} }
/* Now the exitpolicy summary line. */ /* Now the exitpolicy summary line. */
if (rs_out.has_exitsummary && flavor == FLAV_NS) { if (rs_out.has_exitsummary && flavor == FLAV_NS) {
tor_asprintf(&buf, "p %s\n", rs_out.exitsummary); smartlist_add_asprintf(chunks, "p %s\n", rs_out.exitsummary);
smartlist_add(chunks, buf);
} }
/* And the loop is over and we move on to the next router */ /* 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 = size_t digest_len =
flavor == FLAV_NS ? DIGEST_LEN : DIGEST256_LEN; flavor == FLAV_NS ? DIGEST_LEN : DIGEST256_LEN;
const char *algname = crypto_digest_algorithm_get_name(digest_alg); const char *algname = crypto_digest_algorithm_get_name(digest_alg);
char *buf = NULL;
char sigbuf[4096]; char sigbuf[4096];
smartlist_add(chunks, tor_strdup("directory-signature ")); 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. */ /* add the junk that will go at the end of the line. */
if (flavor == FLAV_NS) { if (flavor == FLAV_NS) {
tor_asprintf(&buf, "%s %s\n", fingerprint, smartlist_add_asprintf(chunks, "%s %s\n", fingerprint,
signing_key_fingerprint); signing_key_fingerprint);
} else { } else {
tor_asprintf(&buf, "%s %s %s\n", smartlist_add_asprintf(chunks, "%s %s %s\n",
algname, fingerprint, algname, fingerprint,
signing_key_fingerprint); signing_key_fingerprint);
} }
smartlist_add(chunks, buf);
/* And the signature. */ /* And the signature. */
sigbuf[0] = '\0'; sigbuf[0] = '\0';
if (router_append_dirobj_signature(sigbuf, sizeof(sigbuf), 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, crypto_pk_get_fingerprint(legacy_signing_key,
signing_key_fingerprint, 0); signing_key_fingerprint, 0);
if (flavor == FLAV_NS) { if (flavor == FLAV_NS) {
tor_asprintf(&buf, "%s %s\n", fingerprint, smartlist_add_asprintf(chunks, "%s %s\n", fingerprint,
signing_key_fingerprint); signing_key_fingerprint);
} else { } else {
tor_asprintf(&buf, "%s %s %s\n", smartlist_add_asprintf(chunks, "%s %s %s\n",
algname, fingerprint, algname, fingerprint,
signing_key_fingerprint); signing_key_fingerprint);
} }
smartlist_add(chunks, buf);
sigbuf[0] = '\0'; sigbuf[0] = '\0';
if (router_append_dirobj_signature(sigbuf, sizeof(sigbuf), if (router_append_dirobj_signature(sigbuf, sizeof(sigbuf),
digest, digest_len, 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(sk, sizeof(sk), sig->signing_key_digest, DIGEST_LEN);
base16_encode(id, sizeof(id), sig->identity_digest, DIGEST_LEN); base16_encode(id, sizeof(id), sig->identity_digest, DIGEST_LEN);
if (flavor == FLAV_NS) { if (flavor == FLAV_NS) {
tor_snprintf(buf, sizeof(buf), smartlist_add_asprintf(elements,
"%s %s %s\n-----BEGIN SIGNATURE-----\n", "%s %s %s\n-----BEGIN SIGNATURE-----\n",
keyword, id, sk); keyword, id, sk);
} else { } else {
const char *digest_name = const char *digest_name =
crypto_digest_algorithm_get_name(sig->alg); 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", "%s%s%s %s %s %s\n-----BEGIN SIGNATURE-----\n",
keyword, keyword,
for_detached_signatures ? " " : "", for_detached_signatures ? " " : "",
for_detached_signatures ? flavor_name : "", for_detached_signatures ? flavor_name : "",
digest_name, id, sk); digest_name, id, sk);
} }
smartlist_add(elements, tor_strdup(buf));
base64_encode(buf, sizeof(buf), sig->signature, sig->signature_len); base64_encode(buf, sizeof(buf), sig->signature, sig->signature_len);
strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf)); strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
smartlist_add(elements, tor_strdup(buf)); smartlist_add(elements, tor_strdup(buf));
@ -2370,7 +2335,6 @@ char *
networkstatus_get_detached_signatures(smartlist_t *consensuses) networkstatus_get_detached_signatures(smartlist_t *consensuses)
{ {
smartlist_t *elements; smartlist_t *elements;
char buf[4096];
char *result = NULL, *sigs = NULL; char *result = NULL, *sigs = NULL;
networkstatus_t *consensus_ns = NULL; networkstatus_t *consensus_ns = NULL;
tor_assert(consensuses); 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(fu_buf, consensus_ns->fresh_until);
format_iso_time(vu_buf, consensus_ns->valid_until); format_iso_time(vu_buf, consensus_ns->valid_until);
tor_snprintf(buf, sizeof(buf), smartlist_add_asprintf(elements,
"consensus-digest %s\n" "consensus-digest %s\n"
"valid-after %s\n" "valid-after %s\n"
"fresh-until %s\n" "fresh-until %s\n"
"valid-until %s\n", d, va_buf, fu_buf, vu_buf); "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 */ /* 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)) if (tor_mem_is_zero(ns->digests.d[alg], DIGEST256_LEN))
continue; continue;
base16_encode(d, sizeof(d), ns->digests.d[alg], DIGEST256_LEN); 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); flavor_name, alg_name, d);
smartlist_add(elements, tor_strdup(buf));
} }
} SMARTLIST_FOREACH_END(ns); } SMARTLIST_FOREACH_END(ns);

View File

@ -856,9 +856,7 @@ geoip_get_client_history(geoip_client_action_t action)
/* Build the result. */ /* Build the result. */
chunks = smartlist_create(); chunks = smartlist_create();
SMARTLIST_FOREACH(entries, c_hist_t *, ch, { SMARTLIST_FOREACH(entries, c_hist_t *, ch, {
char *buf=NULL; smartlist_add_asprintf(chunks, "%s=%u", ch->country, ch->total);
tor_asprintf(&buf, "%s=%u", ch->country, ch->total);
smartlist_add(chunks, buf);
}); });
result = smartlist_join_strings(chunks, ",", 0, NULL); result = smartlist_join_strings(chunks, ",", 0, NULL);
done: done:
@ -907,9 +905,7 @@ geoip_get_request_history(geoip_client_action_t action)
strings = smartlist_create(); strings = smartlist_create();
SMARTLIST_FOREACH(entries, c_hist_t *, ent, { SMARTLIST_FOREACH(entries, c_hist_t *, ent, {
char *buf = NULL; smartlist_add_asprintf(strings, "%s=%u", ent->country, ent->total);
tor_asprintf(&buf, "%s=%u", ent->country, ent->total);
smartlist_add(strings, buf);
}); });
result = smartlist_join_strings(strings, ",", 0, NULL); result = smartlist_join_strings(strings, ",", 0, NULL);
SMARTLIST_FOREACH(strings, char *, cp, tor_free(cp)); SMARTLIST_FOREACH(strings, char *, cp, tor_free(cp));

View File

@ -976,8 +976,7 @@ getinfo_helper_accounting(control_connection_t *conn,
else else
*answer = tor_strdup("awake"); *answer = tor_strdup("awake");
} else if (!strcmp(question, "accounting/bytes")) { } else if (!strcmp(question, "accounting/bytes")) {
*answer = tor_malloc(32); tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
tor_snprintf(*answer, 32, U64_FORMAT" "U64_FORMAT,
U64_PRINTF_ARG(n_bytes_read_in_interval), U64_PRINTF_ARG(n_bytes_read_in_interval),
U64_PRINTF_ARG(n_bytes_written_in_interval)); U64_PRINTF_ARG(n_bytes_written_in_interval));
} else if (!strcmp(question, "accounting/bytes-left")) { } 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; read_left = limit - n_bytes_read_in_interval;
if (n_bytes_written_in_interval < limit) if (n_bytes_written_in_interval < limit)
write_left = limit - n_bytes_written_in_interval; write_left = limit - n_bytes_written_in_interval;
*answer = tor_malloc(64); tor_asprintf(answer, U64_FORMAT" "U64_FORMAT,
tor_snprintf(*answer, 64, U64_FORMAT" "U64_FORMAT,
U64_PRINTF_ARG(read_left), U64_PRINTF_ARG(write_left)); U64_PRINTF_ARG(read_left), U64_PRINTF_ARG(write_left));
} else if (!strcmp(question, "accounting/interval-start")) { } else if (!strcmp(question, "accounting/interval-start")) {
*answer = tor_malloc(ISO_TIME_LEN+1); *answer = tor_malloc(ISO_TIME_LEN+1);

View File

@ -598,39 +598,38 @@ networkstatus_check_consensus_signature(networkstatus_t *consensus,
hex_str(ds->v3_identity_digest, DIGEST_LEN)); hex_str(ds->v3_identity_digest, DIGEST_LEN));
}); });
{ {
char *joined;
smartlist_t *sl = smartlist_create(); smartlist_t *sl = smartlist_create();
char *cp;
char *tmp = smartlist_join_strings(list_good, " ", 0, NULL); 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).", "authorities for us to accept it. This one has %d (%s).",
n_required, n_good, tmp); n_required, n_good, tmp);
tor_free(tmp); tor_free(tmp);
smartlist_add(sl,cp);
if (n_no_signature) { if (n_no_signature) {
tmp = smartlist_join_strings(list_no_signature, " ", 0, NULL); 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); n_no_signature, tmp);
tor_free(tmp); tor_free(tmp);
smartlist_add(sl,cp);
} }
if (n_unknown) { 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); "recognize.", n_unknown);
smartlist_add(sl,cp);
} }
if (n_bad) { 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); "correctly.", n_bad);
smartlist_add(sl,cp);
} }
if (n_missing_key) { 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); "because we were missing the keys.", n_missing_key);
smartlist_add(sl,cp);
} }
cp = smartlist_join_strings(sl, " ", 0, NULL); joined = smartlist_join_strings(sl, " ", 0, NULL);
log(severity, LD_DIR, "%s", cp); log(severity, LD_DIR, "%s", joined);
tor_free(cp); tor_free(joined);
SMARTLIST_FOREACH(sl, char *, c, tor_free(c)); SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
smartlist_free(sl); smartlist_free(sl);
} }
@ -2160,9 +2159,8 @@ networkstatus_dump_bridge_status_to_file(time_t now)
{ {
char *status = networkstatus_getinfo_by_purpose("bridge", now); char *status = networkstatus_getinfo_by_purpose("bridge", now);
const or_options_t *options = get_options(); const or_options_t *options = get_options();
size_t len = strlen(options->DataDirectory) + 32; char *fname = NULL;
char *fname = tor_malloc(len); tor_asprintf(&fname, "%s"PATH_SEPARATOR"networkstatus-bridges",
tor_snprintf(fname, len, "%s"PATH_SEPARATOR"networkstatus-bridges",
options->DataDirectory); options->DataDirectory);
write_str_to_file(fname,status,0); write_str_to_file(fname,status,0);
tor_free(fname); tor_free(fname);

View File

@ -456,9 +456,9 @@ nt_service_command_line(int *using_default_torrc)
{ {
TCHAR tor_exe[MAX_PATH+1]; TCHAR tor_exe[MAX_PATH+1];
char tor_exe_ascii[MAX_PATH+1]; char tor_exe_ascii[MAX_PATH+1];
char *command, *options=NULL; char *command=NULL, *options=NULL;
smartlist_t *sl; smartlist_t *sl;
int i, cmdlen; int i;
*using_default_torrc = 1; *using_default_torrc = 1;
/* Get the location of tor.exe */ /* 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)); strlcpy(tor_exe_ascii, tor_exe, sizeof(tor_exe_ascii));
#endif #endif
/* Allocate a string for the NT service command line */ /* Allocate a string for the NT service command line and */
cmdlen = strlen(tor_exe_ascii) + (options?strlen(options):0) + 32;
command = tor_malloc(cmdlen);
/* Format the service command */ /* Format the service command */
if (options) { if (options) {
if (tor_snprintf(command, cmdlen, "\"%s\" --nt-service \"%s\"", tor_asprintf(&command, "\"%s\" --nt-service \"%s\"",
tor_exe_ascii, options)<0) { tor_exe_ascii, options);
tor_free(command); /* sets command to NULL. */
}
} else { /* ! options */ } else { /* ! options */
if (tor_snprintf(command, cmdlen, "\"%s\" --nt-service", tor_asprintf(&command, "\"%s\" --nt-service", tor_exe_ascii);
tor_exe_ascii)<0) {
tor_free(command); /* sets command to NULL. */
}
} }
tor_free(options); tor_free(options);

View File

@ -1218,7 +1218,7 @@ policy_summarize(smartlist_t *policy)
smartlist_t *summary = policy_summary_create(); smartlist_t *summary = policy_summary_create();
smartlist_t *accepts, *rejects; smartlist_t *accepts, *rejects;
int i, last, start_prt; 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; char *accepts_str = NULL, *rejects_str = NULL, *shorter_str, *result;
const char *prefix; const char *prefix;
@ -1290,21 +1290,15 @@ policy_summarize(smartlist_t *policy)
tor_assert(*c == ','); tor_assert(*c == ',');
*c = '\0'; *c = '\0';
shorter_len = strlen(shorter_str);
} else if (rejects_len < accepts_len) { } else if (rejects_len < accepts_len) {
shorter_str = rejects_str; shorter_str = rejects_str;
shorter_len = rejects_len;
prefix = "reject"; prefix = "reject";
} else { } else {
shorter_str = accepts_str; shorter_str = accepts_str;
shorter_len = accepts_len;
prefix = "accept"; prefix = "accept";
} }
final_size = strlen(prefix)+1+shorter_len+1; tor_asprintf(&result, "%s %s", prefix, shorter_str);
tor_assert(final_size <= MAX_EXITPOLICY_SUMMARY_LEN+1);
result = tor_malloc(final_size);
tor_snprintf(result, final_size, "%s %s", prefix, shorter_str);
cleanup: cleanup:
/* cleanup */ /* cleanup */

View File

@ -941,7 +941,6 @@ rep_hist_get_router_stability_doc(time_t now)
DIGESTMAP_FOREACH(history_map, id, or_history_t *, hist) { DIGESTMAP_FOREACH(history_map, id, or_history_t *, hist) {
const node_t *node; const node_t *node;
char dbuf[BASE64_DIGEST_LEN+1]; char dbuf[BASE64_DIGEST_LEN+1];
char header_buf[512];
char *info; char *info;
digest_to_base64(dbuf, id); digest_to_base64(dbuf, id);
node = node_get_by_id(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); format_iso_time(tbuf, published);
else else
strlcpy(tbuf, "???", sizeof(tbuf)); strlcpy(tbuf, "???", sizeof(tbuf));
tor_snprintf(header_buf, sizeof(header_buf), smartlist_add_asprintf(chunks,
"router %s %s %s\n" "router %s %s %s\n"
"published %s\n" "published %s\n"
"relevant-flags %s%s%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->ri && node->ri->is_hibernating ? "Hibernating " : "",
node_get_declared_uptime(node)); node_get_declared_uptime(node));
} else { } else {
tor_snprintf(header_buf, sizeof(header_buf), smartlist_add_asprintf(chunks,
"router %s {no descriptor}\n", dbuf); "router %s {no descriptor}\n", dbuf);
} }
smartlist_add(chunks, tor_strdup(header_buf));
info = rep_hist_format_router_status(hist, now); info = rep_hist_format_router_status(hist, now);
if (info) if (info)
smartlist_add(chunks, info); smartlist_add(chunks, info);
@ -1588,7 +1586,6 @@ rep_hist_update_bwhist_state_section(or_state_t *state,
time_t *s_begins, time_t *s_begins,
int *s_interval) int *s_interval)
{ {
char *cp;
int i,j; int i,j;
uint64_t maxval; 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) { for (j=0; j < b->num_maxes_set; ++j,++i) {
if (i >= NUM_TOTALS) if (i >= NUM_TOTALS)
i = 0; i = 0;
tor_asprintf(&cp, U64_FORMAT, U64_PRINTF_ARG(b->totals[i] & ~0x3ff)); smartlist_add_asprintf(*s_values, U64_FORMAT,
smartlist_add(*s_values, cp); U64_PRINTF_ARG(b->totals[i] & ~0x3ff));
maxval = b->maxima[i] / NUM_SECS_ROLLING_MEASURE; maxval = b->maxima[i] / NUM_SECS_ROLLING_MEASURE;
tor_asprintf(&cp, U64_FORMAT, U64_PRINTF_ARG(maxval & ~0x3ff)); smartlist_add_asprintf(*s_maxima, U64_FORMAT,
smartlist_add(*s_maxima, cp); U64_PRINTF_ARG(maxval & ~0x3ff));
} }
tor_asprintf(&cp, U64_FORMAT, U64_PRINTF_ARG(b->total_in_period & ~0x3ff)); smartlist_add_asprintf(*s_values, U64_FORMAT,
smartlist_add(*s_values, cp); U64_PRINTF_ARG(b->total_in_period & ~0x3ff));
maxval = b->max_total / NUM_SECS_ROLLING_MEASURE; maxval = b->max_total / NUM_SECS_ROLLING_MEASURE;
tor_asprintf(&cp, U64_FORMAT, U64_PRINTF_ARG(maxval & ~0x3ff)); smartlist_add_asprintf(*s_maxima, U64_FORMAT,
smartlist_add(*s_maxima, cp); U64_PRINTF_ARG(maxval & ~0x3ff));
} }
/** Update <b>state</b> with the newest bandwidth history. Done before /** 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, uint64_t cur_bytes = 0, other_read = 0, other_written = 0,
total_read = 0, total_written = 0; total_read = 0, total_written = 0;
uint32_t total_streams = 0, other_streams = 0; uint32_t total_streams = 0, other_streams = 0;
char *buf;
smartlist_t *written_strings, *read_strings, *streams_strings; smartlist_t *written_strings, *read_strings, *streams_strings;
char *written_string, *read_string, *streams_string; char *written_string, *read_string, *streams_string;
char t[ISO_TIME_LEN+1]; char t[ISO_TIME_LEN+1];
@ -2204,9 +2200,8 @@ rep_hist_format_exit_stats(time_t now)
exit_bytes_written[cur_port], exit_bytes_written[cur_port],
EXIT_STATS_ROUND_UP_BYTES); EXIT_STATS_ROUND_UP_BYTES);
num /= 1024; num /= 1024;
buf = NULL; smartlist_add_asprintf(written_strings, "%d="U64_FORMAT,
tor_asprintf(&buf, "%d="U64_FORMAT, cur_port, U64_PRINTF_ARG(num)); cur_port, U64_PRINTF_ARG(num));
smartlist_add(written_strings, buf);
other_written -= exit_bytes_written[cur_port]; other_written -= exit_bytes_written[cur_port];
} }
if (exit_bytes_read[cur_port] > 0) { 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_bytes_read[cur_port],
EXIT_STATS_ROUND_UP_BYTES); EXIT_STATS_ROUND_UP_BYTES);
num /= 1024; num /= 1024;
buf = NULL; smartlist_add_asprintf(read_strings, "%d="U64_FORMAT,
tor_asprintf(&buf, "%d="U64_FORMAT, cur_port, U64_PRINTF_ARG(num)); cur_port, U64_PRINTF_ARG(num));
smartlist_add(read_strings, buf);
other_read -= exit_bytes_read[cur_port]; other_read -= exit_bytes_read[cur_port];
} }
if (exit_streams[cur_port] > 0) { if (exit_streams[cur_port] > 0) {
uint32_t num = round_uint32_to_next_multiple_of( uint32_t num = round_uint32_to_next_multiple_of(
exit_streams[cur_port], exit_streams[cur_port],
EXIT_STATS_ROUND_UP_STREAMS); EXIT_STATS_ROUND_UP_STREAMS);
buf = NULL; smartlist_add_asprintf(streams_strings, "%d=%u", cur_port, num);
tor_asprintf(&buf, "%d=%u", cur_port, num);
smartlist_add(streams_strings, buf);
other_streams -= exit_streams[cur_port]; 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, other_written = round_uint64_to_next_multiple_of(other_written,
EXIT_STATS_ROUND_UP_BYTES); EXIT_STATS_ROUND_UP_BYTES);
other_written /= 1024; other_written /= 1024;
buf = NULL; smartlist_add_asprintf(written_strings, "other="U64_FORMAT,
tor_asprintf(&buf, "other="U64_FORMAT, U64_PRINTF_ARG(other_written)); U64_PRINTF_ARG(other_written));
smartlist_add(written_strings, buf);
other_read = round_uint64_to_next_multiple_of(other_read, other_read = round_uint64_to_next_multiple_of(other_read,
EXIT_STATS_ROUND_UP_BYTES); EXIT_STATS_ROUND_UP_BYTES);
other_read /= 1024; other_read /= 1024;
buf = NULL; smartlist_add_asprintf(read_strings, "other="U64_FORMAT,
tor_asprintf(&buf, "other="U64_FORMAT, U64_PRINTF_ARG(other_read)); U64_PRINTF_ARG(other_read));
smartlist_add(read_strings, buf);
other_streams = round_uint32_to_next_multiple_of(other_streams, other_streams = round_uint32_to_next_multiple_of(other_streams,
EXIT_STATS_ROUND_UP_STREAMS); EXIT_STATS_ROUND_UP_STREAMS);
buf = NULL; smartlist_add_asprintf(streams_strings, "other=%u", other_streams);
tor_asprintf(&buf, "other=%u", other_streams);
smartlist_add(streams_strings, buf);
/* Join all observations in single strings. */ /* Join all observations in single strings. */
written_string = smartlist_join_strings(written_strings, ",", 0, NULL); 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], int processed_cells[SHARES], circs_in_share[SHARES],
number_of_circuits, i; number_of_circuits, i;
double queued_cells[SHARES], time_in_queue[SHARES]; double queued_cells[SHARES], time_in_queue[SHARES];
char *buf = NULL;
smartlist_t *processed_cells_strings, *queued_cells_strings, smartlist_t *processed_cells_strings, *queued_cells_strings,
*time_in_queue_strings; *time_in_queue_strings;
char *processed_cells_string, *queued_cells_string, char *processed_cells_string, *queued_cells_string,
@ -2510,19 +2497,19 @@ rep_hist_format_buffer_stats(time_t now)
queued_cells_strings = smartlist_create(); queued_cells_strings = smartlist_create();
time_in_queue_strings = smartlist_create(); time_in_queue_strings = smartlist_create();
for (i = 0; i < SHARES; i++) { for (i = 0; i < SHARES; i++) {
tor_asprintf(&buf,"%d", !circs_in_share[i] ? 0 : smartlist_add_asprintf(processed_cells_strings,
"%d", !circs_in_share[i] ? 0 :
processed_cells[i] / circs_in_share[i]); processed_cells[i] / circs_in_share[i]);
smartlist_add(processed_cells_strings, buf);
} }
for (i = 0; i < SHARES; i++) { for (i = 0; i < SHARES; i++) {
tor_asprintf(&buf, "%.2f", circs_in_share[i] == 0 ? 0.0 : smartlist_add_asprintf(queued_cells_strings, "%.2f",
circs_in_share[i] == 0 ? 0.0 :
queued_cells[i] / (double) circs_in_share[i]); queued_cells[i] / (double) circs_in_share[i]);
smartlist_add(queued_cells_strings, buf);
} }
for (i = 0; i < SHARES; i++) { for (i = 0; i < SHARES; i++) {
tor_asprintf(&buf, "%.0f", circs_in_share[i] == 0 ? 0.0 : smartlist_add_asprintf(time_in_queue_strings, "%.0f",
circs_in_share[i] == 0 ? 0.0 :
time_in_queue[i] / (double) circs_in_share[i]); time_in_queue[i] / (double) circs_in_share[i]);
smartlist_add(time_in_queue_strings, buf);
} }
/* Join all observations in single strings. */ /* Join all observations in single strings. */

View File

@ -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); format_iso_time(published, router->cache_info.published_on);
if (router->declared_family && smartlist_len(router->declared_family)) { if (router->declared_family && smartlist_len(router->declared_family)) {
size_t n; char *family = smartlist_join_strings(router->declared_family,
char *family = smartlist_join_strings(router->declared_family, " ", 0, &n); " ", 0, NULL);
n += strlen("family ") + 2; /* 1 for \n, 1 for \0. */ tor_asprintf(&family_line, "family %s\n", family);
family_line = tor_malloc(n);
tor_snprintf(family_line, n, "family %s\n", family);
tor_free(family); tor_free(family);
} else { } else {
family_line = tor_strdup(""); family_line = tor_strdup("");
@ -2267,9 +2265,7 @@ extrainfo_dump_to_string(char **s_out, extrainfo_t *extrainfo,
smartlist_add(chunks, pre); smartlist_add(chunks, pre);
if (geoip_is_loaded()) { if (geoip_is_loaded()) {
char *chunk=NULL; smartlist_add_asprintf(chunks, "geoip-db-digest %s\n", geoip_db_digest());
tor_asprintf(&chunk, "geoip-db-digest %s\n", geoip_db_digest());
smartlist_add(chunks, chunk);
} }
if (options->ExtraInfoStatistics && write_stats_to_extrainfo) { if (options->ExtraInfoStatistics && write_stats_to_extrainfo) {

View File

@ -2408,8 +2408,6 @@ router_get_by_nickname(const char *nickname, int warn_if_unnamed)
int any_unwarned = 0; int any_unwarned = 0;
SMARTLIST_FOREACH_BEGIN(routerlist->routers, routerinfo_t *, router) { SMARTLIST_FOREACH_BEGIN(routerlist->routers, routerinfo_t *, router) {
routerstatus_t *rs; routerstatus_t *rs;
char *desc;
size_t dlen;
char fp[HEX_DIGEST_LEN+1]; char fp[HEX_DIGEST_LEN+1];
if (strcasecmp(router->nickname, nickname)) if (strcasecmp(router->nickname, nickname))
continue; continue;
@ -2421,11 +2419,8 @@ router_get_by_nickname(const char *nickname, int warn_if_unnamed)
} }
base16_encode(fp, sizeof(fp), base16_encode(fp, sizeof(fp),
router->cache_info.identity_digest, DIGEST_LEN); router->cache_info.identity_digest, DIGEST_LEN);
dlen = 32 + HEX_DIGEST_LEN + strlen(router->address); smartlist_add_asprintf(fps, "\"$%s\" for the one at %s:%d",
desc = tor_malloc(dlen);
tor_snprintf(desc, dlen, "\"$%s\" for the one at %s:%d",
fp, router->address, router->or_port); fp, router->address, router->or_port);
smartlist_add(fps, desc);
} SMARTLIST_FOREACH_END(router); } SMARTLIST_FOREACH_END(router);
if (any_unwarned) { if (any_unwarned) {
char *alternatives = smartlist_join_strings(fps, "; ",0,NULL); 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; trusted_dir_server_t *ent;
uint32_t a; uint32_t a;
char *hostname = NULL; char *hostname = NULL;
size_t dlen;
if (!trusted_dir_servers) if (!trusted_dir_servers)
trusted_dir_servers = smartlist_create(); 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)) if (v3_auth_digest && (type & V3_DIRINFO))
memcpy(ent->v3_identity_digest, v3_auth_digest, DIGEST_LEN); 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) 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); nickname, hostname, (int)dir_port);
else else
tor_snprintf(ent->description, dlen, "directory server at %s:%d", tor_asprintf(&ent->description, "directory server at %s:%d",
hostname, (int)dir_port); hostname, (int)dir_port);
ent->fake_status.addr = ent->addr; ent->fake_status.addr = ent->addr;
@ -5333,7 +5325,6 @@ esc_router_info(const routerinfo_t *router)
{ {
static char *info=NULL; static char *info=NULL;
char *esc_contact, *esc_platform; char *esc_contact, *esc_platform;
size_t len;
tor_free(info); tor_free(info);
if (!router) if (!router)
@ -5342,10 +5333,7 @@ esc_router_info(const routerinfo_t *router)
esc_contact = esc_for_log(router->contact_info); esc_contact = esc_for_log(router->contact_info);
esc_platform = esc_for_log(router->platform); esc_platform = esc_for_log(router->platform);
len = strlen(esc_contact)+strlen(esc_platform)+32; tor_asprintf(&info, "Contact %s, Platform %s", esc_contact, esc_platform);
info = tor_malloc(len);
tor_snprintf(info, len, "Contact %s, Platform %s", esc_contact,
esc_platform);
tor_free(esc_contact); tor_free(esc_contact);
tor_free(esc_platform); tor_free(esc_platform);

View File

@ -918,7 +918,7 @@ parse_cmethod_line(const char *line, managed_proxy_t *mp)
static char * static char *
get_bindaddr_for_proxy(const managed_proxy_t *mp) get_bindaddr_for_proxy(const managed_proxy_t *mp)
{ {
char *bindaddr = NULL; char *bindaddr_result = NULL;
char *bindaddr_tmp = NULL; char *bindaddr_tmp = NULL;
smartlist_t *string_tmp = smartlist_create(); 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) { SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, char *, t) {
bindaddr_tmp = get_bindaddr_for_transport(t); bindaddr_tmp = get_bindaddr_for_transport(t);
tor_asprintf(&bindaddr, "%s-%s", t, bindaddr_tmp); smartlist_add_asprintf(string_tmp, "%s-%s", t, bindaddr_tmp);
smartlist_add(string_tmp, bindaddr);
tor_free(bindaddr_tmp); tor_free(bindaddr_tmp);
} SMARTLIST_FOREACH_END(t); } 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_FOREACH(string_tmp, char *, t, tor_free(t));
smartlist_free(string_tmp); smartlist_free(string_tmp);
return bindaddr; return bindaddr_result;
} }
#ifdef MS_WINDOWS #ifdef MS_WINDOWS