*_free functions now accept NULL

Some *_free functions threw asserts when passed NULL. Now all of them
accept NULL as input and perform no action when called that way.

This gains us consistence for our free functions, and allows some
code simplifications where an explicit null check is no longer necessary.
This commit is contained in:
Sebastian Hahn 2009-09-28 16:37:01 +02:00
parent 4afdb79051
commit 3807db001d
26 changed files with 168 additions and 76 deletions

View File

@ -263,7 +263,8 @@ aes_set_key(aes_cnt_cipher_t *cipher, const char *key, int key_bits)
void void
aes_free_cipher(aes_cnt_cipher_t *cipher) aes_free_cipher(aes_cnt_cipher_t *cipher)
{ {
tor_assert(cipher); if (!cipher)
return;
#ifdef USE_OPENSSL_EVP #ifdef USE_OPENSSL_EVP
EVP_CIPHER_CTX_cleanup(&cipher->key); EVP_CIPHER_CTX_cleanup(&cipher->key);
#endif #endif

View File

@ -2044,6 +2044,8 @@ tor_mutex_new(void)
void void
tor_mutex_free(tor_mutex_t *m) tor_mutex_free(tor_mutex_t *m)
{ {
if (!m)
return;
tor_mutex_uninit(m); tor_mutex_uninit(m);
tor_free(m); tor_free(m);
} }
@ -2071,7 +2073,8 @@ tor_cond_new(void)
void void
tor_cond_free(tor_cond_t *cond) tor_cond_free(tor_cond_t *cond)
{ {
tor_assert(cond); if (!cond)
return;
if (pthread_cond_destroy(&cond->cond)) { if (pthread_cond_destroy(&cond->cond)) {
log_warn(LD_GENERAL,"Error freeing condition: %s", strerror(errno)); log_warn(LD_GENERAL,"Error freeing condition: %s", strerror(errno));
return; return;
@ -2128,7 +2131,8 @@ tor_cond_new(void)
void void
tor_cond_free(tor_cond_t *cond) tor_cond_free(tor_cond_t *cond)
{ {
tor_assert(cond); if (!cond)
return;
DeleteCriticalSection(&cond->mutex); DeleteCriticalSection(&cond->mutex);
/* XXXX notify? */ /* XXXX notify? */
smartlist_free(cond->events); smartlist_free(cond->events);

View File

@ -44,7 +44,8 @@ smartlist_create(void)
void void
smartlist_free(smartlist_t *sl) smartlist_free(smartlist_t *sl)
{ {
tor_assert(sl != NULL); if (!sl)
return;
tor_free(sl->list); tor_free(sl->list);
tor_free(sl); tor_free(sl);
} }
@ -1187,6 +1188,9 @@ void
strmap_free(strmap_t *map, void (*free_val)(void*)) strmap_free(strmap_t *map, void (*free_val)(void*))
{ {
strmap_entry_t **ent, **next, *this; strmap_entry_t **ent, **next, *this;
if (!map)
return;
for (ent = HT_START(strmap_impl, &map->head); ent != NULL; ent = next) { for (ent = HT_START(strmap_impl, &map->head); ent != NULL; ent = next) {
this = *ent; this = *ent;
next = HT_NEXT_RMV(strmap_impl, &map->head, ent); next = HT_NEXT_RMV(strmap_impl, &map->head, ent);
@ -1208,6 +1212,8 @@ void
digestmap_free(digestmap_t *map, void (*free_val)(void*)) digestmap_free(digestmap_t *map, void (*free_val)(void*))
{ {
digestmap_entry_t **ent, **next, *this; digestmap_entry_t **ent, **next, *this;
if (!map)
return;
for (ent = HT_START(digestmap_impl, &map->head); ent != NULL; ent = next) { for (ent = HT_START(digestmap_impl, &map->head); ent != NULL; ent = next) {
this = *ent; this = *ent;
next = HT_NEXT_RMV(digestmap_impl, &map->head, ent); next = HT_NEXT_RMV(digestmap_impl, &map->head, ent);
@ -1323,6 +1329,8 @@ digestset_new(int max_elements)
void void
digestset_free(digestset_t *set) digestset_free(digestset_t *set)
{ {
if (!set)
return;
bitarray_free(set->ba); bitarray_free(set->ba);
tor_free(set); tor_free(set);
} }

View File

@ -400,7 +400,8 @@ crypto_new_pk_env(void)
void void
crypto_free_pk_env(crypto_pk_env_t *env) crypto_free_pk_env(crypto_pk_env_t *env)
{ {
tor_assert(env); if (!env)
return;
if (--env->refs > 0) if (--env->refs > 0)
return; return;
@ -463,7 +464,8 @@ crypto_new_cipher_env(void)
void void
crypto_free_cipher_env(crypto_cipher_env_t *env) crypto_free_cipher_env(crypto_cipher_env_t *env)
{ {
tor_assert(env); if (!env)
return;
tor_assert(env->cipher); tor_assert(env->cipher);
aes_free_cipher(env->cipher); aes_free_cipher(env->cipher);
@ -1528,6 +1530,8 @@ crypto_new_digest256_env(digest_algorithm_t algorithm)
void void
crypto_free_digest_env(crypto_digest_env_t *digest) crypto_free_digest_env(crypto_digest_env_t *digest)
{ {
if (!digest)
return;
memset(digest, 0, sizeof(crypto_digest_env_t)); memset(digest, 0, sizeof(crypto_digest_env_t));
tor_free(digest); tor_free(digest);
} }
@ -1899,7 +1903,8 @@ crypto_expand_key_material(const char *key_in, size_t key_in_len,
void void
crypto_dh_free(crypto_dh_env_t *dh) crypto_dh_free(crypto_dh_env_t *dh)
{ {
tor_assert(dh); if (!dh)
return;
tor_assert(dh->dh); tor_assert(dh->dh);
DH_free(dh->dh); DH_free(dh->dh);
tor_free(dh); tor_free(dh);

View File

@ -426,6 +426,8 @@ _log_err(log_domain_mask_t domain, const char *format, ...)
static void static void
log_free(logfile_t *victim) log_free(logfile_t *victim)
{ {
if (!victim)
return;
tor_free(victim->severities); tor_free(victim->severities);
tor_free(victim->filename); tor_free(victim->filename);
tor_free(victim); tor_free(victim);

View File

@ -121,7 +121,7 @@ alloc_chunk(size_t sz, int freelist_ok)
/** Release <b>chunk</b> from a memarea, either by adding it to the freelist /** Release <b>chunk</b> from a memarea, either by adding it to the freelist
* or by freeing it if the freelist is already too big. */ * or by freeing it if the freelist is already too big. */
static void static void
chunk_free(memarea_chunk_t *chunk) chunk_free_unchecked(memarea_chunk_t *chunk)
{ {
CHECK_SENTINEL(chunk); CHECK_SENTINEL(chunk);
if (freelist_len < MAX_FREELIST_LEN) { if (freelist_len < MAX_FREELIST_LEN) {
@ -151,7 +151,7 @@ memarea_drop_all(memarea_t *area)
memarea_chunk_t *chunk, *next; memarea_chunk_t *chunk, *next;
for (chunk = area->first; chunk; chunk = next) { for (chunk = area->first; chunk; chunk = next) {
next = chunk->next_chunk; next = chunk->next_chunk;
chunk_free(chunk); chunk_free_unchecked(chunk);
} }
area->first = NULL; /*fail fast on */ area->first = NULL; /*fail fast on */
tor_free(area); tor_free(area);
@ -167,7 +167,7 @@ memarea_clear(memarea_t *area)
if (area->first->next_chunk) { if (area->first->next_chunk) {
for (chunk = area->first->next_chunk; chunk; chunk = next) { for (chunk = area->first->next_chunk; chunk; chunk = next) {
next = chunk->next_chunk; next = chunk->next_chunk;
chunk_free(chunk); chunk_free_unchecked(chunk);
} }
area->first->next_chunk = NULL; area->first->next_chunk = NULL;
} }

View File

@ -423,7 +423,8 @@ tor_zlib_process(tor_zlib_state_t *state,
void void
tor_zlib_free(tor_zlib_state_t *state) tor_zlib_free(tor_zlib_state_t *state)
{ {
tor_assert(state); if (!state)
return;
if (state->compress) if (state->compress)
deflateEnd(&state->stream); deflateEnd(&state->stream);

View File

@ -986,7 +986,9 @@ void
tor_tls_free(tor_tls_t *tls) tor_tls_free(tor_tls_t *tls)
{ {
tor_tls_t *removed; tor_tls_t *removed;
tor_assert(tls && tls->ssl); if (!tls)
return;
tor_assert(tls->ssl);
removed = HT_REMOVE(tlsmap, &tlsmap_root, tls); removed = HT_REMOVE(tlsmap, &tlsmap_root, tls);
if (!removed) { if (!removed) {
log_warn(LD_BUG, "Freeing a TLS that was not in the ssl->tls map."); log_warn(LD_BUG, "Freeing a TLS that was not in the ssl->tls map.");

View File

@ -147,10 +147,13 @@ get_freelist(size_t alloc)
/** Deallocate a chunk or put it on a freelist */ /** Deallocate a chunk or put it on a freelist */
static void static void
chunk_free(chunk_t *chunk) chunk_free_unchecked(chunk_t *chunk)
{ {
size_t alloc = CHUNK_ALLOC_SIZE(chunk->memlen); size_t alloc;
chunk_freelist_t *freelist = get_freelist(alloc); chunk_freelist_t *freelist;
alloc = CHUNK_ALLOC_SIZE(chunk->memlen);
freelist = get_freelist(alloc);
if (freelist && freelist->cur_length < freelist->max_length) { if (freelist && freelist->cur_length < freelist->max_length) {
chunk->next = freelist->head; chunk->next = freelist->head;
freelist->head = chunk; freelist->head = chunk;
@ -195,7 +198,7 @@ chunk_new_with_alloc_size(size_t alloc)
} }
#else #else
static void static void
chunk_free(chunk_t *chunk) chunk_free_unchecked(chunk_t *chunk)
{ {
tor_free(chunk); tor_free(chunk);
} }
@ -403,7 +406,7 @@ buf_pullup(buf_t *buf, size_t bytes, int nulterminate)
dest->next = src->next; dest->next = src->next;
if (buf->tail == src) if (buf->tail == src)
buf->tail = dest; buf->tail = dest;
chunk_free(src); chunk_free_unchecked(src);
} else { } else {
memcpy(CHUNK_WRITE_PTR(dest), src->data, n); memcpy(CHUNK_WRITE_PTR(dest), src->data, n);
dest->datalen += n; dest->datalen += n;
@ -449,7 +452,7 @@ buf_remove_from_front(buf_t *buf, size_t n)
buf->head = victim->next; buf->head = victim->next;
if (buf->tail == victim) if (buf->tail == victim)
buf->tail = NULL; buf->tail = NULL;
chunk_free(victim); chunk_free_unchecked(victim);
} }
} }
check(); check();
@ -483,7 +486,7 @@ buf_clear(buf_t *buf)
buf->datalen = 0; buf->datalen = 0;
for (chunk = buf->head; chunk; chunk = next) { for (chunk = buf->head; chunk; chunk = next) {
next = chunk->next; next = chunk->next;
chunk_free(chunk); chunk_free_unchecked(chunk);
} }
buf->head = buf->tail = NULL; buf->head = buf->tail = NULL;
} }
@ -522,6 +525,8 @@ buf_slack(const buf_t *buf)
void void
buf_free(buf_t *buf) buf_free(buf_t *buf)
{ {
if (!buf)
return;
buf_clear(buf); buf_clear(buf);
buf->magic = 0xdeadbeef; buf->magic = 0xdeadbeef;
tor_free(buf); tor_free(buf);

View File

@ -2744,7 +2744,8 @@ extend_info_from_router(routerinfo_t *r)
void void
extend_info_free(extend_info_t *info) extend_info_free(extend_info_t *info)
{ {
tor_assert(info); if (!info)
return;
if (info->onion_key) if (info->onion_key)
crypto_free_pk_env(info->onion_key); crypto_free_pk_env(info->onion_key);
tor_free(info); tor_free(info);
@ -3053,7 +3054,8 @@ pick_entry_guards(void)
static void static void
entry_guard_free(entry_guard_t *e) entry_guard_free(entry_guard_t *e)
{ {
tor_assert(e); if (!e)
return;
tor_free(e->chosen_by_version); tor_free(e->chosen_by_version);
tor_free(e); tor_free(e);
} }

View File

@ -442,7 +442,9 @@ circuit_free(circuit_t *circ)
{ {
void *mem; void *mem;
size_t memlen; size_t memlen;
tor_assert(circ); if (!circ)
return;
if (CIRCUIT_IS_ORIGIN(circ)) { if (CIRCUIT_IS_ORIGIN(circ)) {
origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ); origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
mem = ocirc; mem = ocirc;
@ -558,6 +560,9 @@ circuit_free_all(void)
static void static void
circuit_free_cpath_node(crypt_path_t *victim) circuit_free_cpath_node(crypt_path_t *victim)
{ {
if (!victim)
return;
if (victim->f_crypto) if (victim->f_crypto)
crypto_free_cipher_env(victim->f_crypto); crypto_free_cipher_env(victim->f_crypto);
if (victim->b_crypto) if (victim->b_crypto)

View File

@ -859,6 +859,9 @@ get_version(void)
static void static void
or_options_free(or_options_t *options) or_options_free(or_options_t *options)
{ {
if (!options)
return;
if (options->_ExcludeExitNodesUnion) if (options->_ExcludeExitNodesUnion)
routerset_free(options->_ExcludeExitNodesUnion); routerset_free(options->_ExcludeExitNodesUnion);
config_free(&options_format, options); config_free(&options_format, options);
@ -2609,7 +2612,10 @@ config_free(config_format_t *fmt, void *options)
{ {
int i; int i;
tor_assert(options); if (!options)
return;
tor_assert(fmt);
for (i=0; fmt->vars[i].name; ++i) for (i=0; fmt->vars[i].name; ++i)
option_clear(fmt, options, &(fmt->vars[i])); option_clear(fmt, options, &(fmt->vars[i]));

View File

@ -311,6 +311,9 @@ _connection_free(connection_t *conn)
{ {
void *mem; void *mem;
size_t memlen; size_t memlen;
if (!conn)
return;
switch (conn->type) { switch (conn->type) {
case CONN_TYPE_OR: case CONN_TYPE_OR:
tor_assert(conn->magic == OR_CONNECTION_MAGIC); tor_assert(conn->magic == OR_CONNECTION_MAGIC);
@ -432,7 +435,8 @@ _connection_free(connection_t *conn)
void void
connection_free(connection_t *conn) connection_free(connection_t *conn)
{ {
tor_assert(conn); if (!conn)
return;
tor_assert(!connection_is_on_closeable_list(conn)); tor_assert(!connection_is_on_closeable_list(conn));
tor_assert(!connection_in_array(conn)); tor_assert(!connection_in_array(conn));
if (conn->linked_conn) { if (conn->linked_conn) {

View File

@ -688,7 +688,11 @@ addressmap_init(void)
static void static void
addressmap_ent_free(void *_ent) addressmap_ent_free(void *_ent)
{ {
addressmap_entry_t *ent = _ent; addressmap_entry_t *ent;
if (!_ent)
return;
ent = _ent;
tor_free(ent->new_address); tor_free(ent->new_address);
tor_free(ent); tor_free(ent);
} }
@ -697,7 +701,11 @@ addressmap_ent_free(void *_ent)
static void static void
addressmap_virtaddress_ent_free(void *_ent) addressmap_virtaddress_ent_free(void *_ent)
{ {
virtaddress_entry_t *ent = _ent; virtaddress_entry_t *ent;
if (!_ent)
return;
ent = _ent;
tor_free(ent->ipv4_address); tor_free(ent->ipv4_address);
tor_free(ent->hostname_address); tor_free(ent->hostname_address);
tor_free(ent); tor_free(ent);

View File

@ -1075,7 +1075,8 @@ connection_init_or_handshake_state(or_connection_t *conn, int started_here)
void void
or_handshake_state_free(or_handshake_state_t *state) or_handshake_state_free(or_handshake_state_t *state)
{ {
tor_assert(state); if (!state)
return;
memset(state, 0xBE, sizeof(or_handshake_state_t)); memset(state, 0xBE, sizeof(or_handshake_state_t));
tor_free(state); tor_free(state);
} }

View File

@ -1292,7 +1292,11 @@ clear_cached_dir(cached_dir_t *d)
static void static void
_free_cached_dir(void *_d) _free_cached_dir(void *_d)
{ {
cached_dir_t *d = (cached_dir_t *)_d; cached_dir_t *d;
if (!_d)
return;
d = (cached_dir_t *)_d;
cached_dir_decref(d); cached_dir_decref(d);
} }

View File

@ -1697,6 +1697,8 @@ get_detached_signatures_from_pending_consensuses(pending_consensus_t *pending,
void void
ns_detached_signatures_free(ns_detached_signatures_t *s) ns_detached_signatures_free(ns_detached_signatures_t *s)
{ {
if (!s)
return;
if (s->signatures) { if (s->signatures) {
STRMAP_FOREACH(s->signatures, flavor, smartlist_t *, sigs) { STRMAP_FOREACH(s->signatures, flavor, smartlist_t *, sigs) {
SMARTLIST_FOREACH(sigs, document_signature_t *, sig, SMARTLIST_FOREACH(sigs, document_signature_t *, sig,

View File

@ -301,6 +301,8 @@ dns_get_expiry_ttl(uint32_t ttl)
static void static void
_free_cached_resolve(cached_resolve_t *r) _free_cached_resolve(cached_resolve_t *r)
{ {
if (!r)
return;
while (r->pending_connections) { while (r->pending_connections) {
pending_connection_t *victim = r->pending_connections; pending_connection_t *victim = r->pending_connections;
r->pending_connections = victim->next; r->pending_connections = victim->next;

View File

@ -266,6 +266,8 @@ static void
vote_routerstatus_free(vote_routerstatus_t *rs) vote_routerstatus_free(vote_routerstatus_t *rs)
{ {
vote_microdesc_hash_t *h, *next; vote_microdesc_hash_t *h, *next;
if (!rs)
return;
tor_free(rs->version); tor_free(rs->version);
tor_free(rs->status.exitsummary); tor_free(rs->status.exitsummary);
for (h = rs->microdesc; h; h = next) { for (h = rs->microdesc; h; h = next) {
@ -280,6 +282,8 @@ vote_routerstatus_free(vote_routerstatus_t *rs)
void void
routerstatus_free(routerstatus_t *rs) routerstatus_free(routerstatus_t *rs)
{ {
if (!rs)
return;
tor_free(rs->exitsummary); tor_free(rs->exitsummary);
tor_free(rs); tor_free(rs);
} }
@ -288,6 +292,8 @@ routerstatus_free(routerstatus_t *rs)
void void
networkstatus_v2_free(networkstatus_v2_t *ns) networkstatus_v2_free(networkstatus_v2_t *ns)
{ {
if (!ns)
return;
tor_free(ns->source_address); tor_free(ns->source_address);
tor_free(ns->contact); tor_free(ns->contact);
if (ns->signing_key) if (ns->signing_key)

View File

@ -1276,7 +1276,8 @@ getinfo_helper_policies(control_connection_t *conn,
void void
addr_policy_list_free(smartlist_t *lst) addr_policy_list_free(smartlist_t *lst)
{ {
if (!lst) return; if (!lst)
return;
SMARTLIST_FOREACH(lst, addr_policy_t *, policy, addr_policy_free(policy)); SMARTLIST_FOREACH(lst, addr_policy_t *, policy, addr_policy_free(policy));
smartlist_free(lst); smartlist_free(lst);
} }
@ -1285,7 +1286,9 @@ addr_policy_list_free(smartlist_t *lst)
void void
addr_policy_free(addr_policy_t *p) addr_policy_free(addr_policy_t *p)
{ {
if (p) { if (!p)
return;
if (--p->refcnt <= 0) { if (--p->refcnt <= 0) {
if (p->is_canonical) { if (p->is_canonical) {
policy_map_ent_t search, *found; policy_map_ent_t search, *found;
@ -1299,7 +1302,6 @@ addr_policy_free(addr_policy_t *p)
tor_free(p); tor_free(p);
} }
} }
}
/** Release all storage held by policy variables. */ /** Release all storage held by policy variables. */
void void

View File

@ -1563,7 +1563,7 @@ clean_cell_pool(void)
/** Release storage held by <b>cell</b>. */ /** Release storage held by <b>cell</b>. */
static INLINE void static INLINE void
packed_cell_free(packed_cell_t *cell) packed_cell_free_unchecked(packed_cell_t *cell)
{ {
--total_cells_allocated; --total_cells_allocated;
mp_pool_release(cell); mp_pool_release(cell);
@ -1667,7 +1667,7 @@ cell_queue_clear(cell_queue_t *queue)
cell = queue->head; cell = queue->head;
while (cell) { while (cell) {
next = cell->next; next = cell->next;
packed_cell_free(cell); packed_cell_free_unchecked(cell);
cell = next; cell = next;
} }
queue->head = queue->tail = NULL; queue->head = queue->tail = NULL;
@ -1913,7 +1913,7 @@ connection_or_flush_from_first_active_circuit(or_connection_t *conn, int max,
connection_write_to_buf(cell->body, CELL_NETWORK_SIZE, TO_CONN(conn)); connection_write_to_buf(cell->body, CELL_NETWORK_SIZE, TO_CONN(conn));
packed_cell_free(cell); packed_cell_free_unchecked(cell);
++n_flushed; ++n_flushed;
if (circ != conn->active_circuits) { if (circ != conn->active_circuits) {
/* If this happens, the current circuit just got made inactive by /* If this happens, the current circuit just got made inactive by

View File

@ -22,6 +22,8 @@ rend_cmp_service_ids(const char *one, const char *two)
void void
rend_service_descriptor_free(rend_service_descriptor_t *desc) rend_service_descriptor_free(rend_service_descriptor_t *desc)
{ {
if (!desc)
return;
if (desc->pk) if (desc->pk)
crypto_free_pk_env(desc->pk); crypto_free_pk_env(desc->pk);
if (desc->intro_nodes) { if (desc->intro_nodes) {
@ -414,6 +416,8 @@ void
rend_encoded_v2_service_descriptor_free( rend_encoded_v2_service_descriptor_free(
rend_encoded_v2_service_descriptor_t *desc) rend_encoded_v2_service_descriptor_t *desc)
{ {
if (!desc)
return;
tor_free(desc->desc_str); tor_free(desc->desc_str);
tor_free(desc); tor_free(desc);
} }
@ -422,6 +426,8 @@ rend_encoded_v2_service_descriptor_free(
void void
rend_intro_point_free(rend_intro_point_t *intro) rend_intro_point_free(rend_intro_point_t *intro)
{ {
if (!intro)
return;
if (intro->extend_info) if (intro->extend_info)
extend_info_free(intro->extend_info); extend_info_free(intro->extend_info);
if (intro->intro_key) if (intro->intro_key)

View File

@ -87,7 +87,8 @@ num_rend_services(void)
static void static void
rend_authorized_client_free(rend_authorized_client_t *client) rend_authorized_client_free(rend_authorized_client_t *client)
{ {
if (!client) return; if (!client)
return;
if (client->client_key) if (client->client_key)
crypto_free_pk_env(client->client_key); crypto_free_pk_env(client->client_key);
tor_free(client->client_name); tor_free(client->client_name);
@ -106,7 +107,9 @@ rend_authorized_client_strmap_item_free(void *authorized_client)
static void static void
rend_service_free(rend_service_t *service) rend_service_free(rend_service_t *service)
{ {
if (!service) return; if (!service)
return;
tor_free(service->directory); tor_free(service->directory);
SMARTLIST_FOREACH(service->ports, void*, p, tor_free(p)); SMARTLIST_FOREACH(service->ports, void*, p, tor_free(p));
smartlist_free(service->ports); smartlist_free(service->ports);
@ -134,9 +137,9 @@ rend_service_free(rend_service_t *service)
void void
rend_service_free_all(void) rend_service_free_all(void)
{ {
if (!rend_service_list) { if (!rend_service_list)
return; return;
}
SMARTLIST_FOREACH(rend_service_list, rend_service_t*, ptr, SMARTLIST_FOREACH(rend_service_list, rend_service_t*, ptr,
rend_service_free(ptr)); rend_service_free(ptr));
smartlist_free(rend_service_list); smartlist_free(rend_service_list);

View File

@ -2272,6 +2272,8 @@ static void
hs_usage_general_period_related_observations_free( hs_usage_general_period_related_observations_free(
hs_usage_general_period_related_observations_t *s) hs_usage_general_period_related_observations_t *s)
{ {
if (!s)
return;
rephist_total_alloc-=sizeof(hs_usage_general_period_related_observations_t); rephist_total_alloc-=sizeof(hs_usage_general_period_related_observations_t);
tor_free(s); tor_free(s);
} }
@ -2281,6 +2283,8 @@ static void
hs_usage_current_observation_period_free( hs_usage_current_observation_period_free(
hs_usage_current_observation_period_t *s) hs_usage_current_observation_period_t *s)
{ {
if (!s)
return;
rephist_total_alloc -= sizeof(hs_usage_current_observation_period_t); rephist_total_alloc -= sizeof(hs_usage_current_observation_period_t);
tor_free(s); tor_free(s);
} }

View File

@ -2378,6 +2378,9 @@ extrainfo_free(extrainfo_t *extrainfo)
static void static void
signed_descriptor_free(signed_descriptor_t *sd) signed_descriptor_free(signed_descriptor_t *sd)
{ {
if (!sd)
return;
tor_free(sd->signed_descriptor_body); tor_free(sd->signed_descriptor_body);
/* XXXX remove this once more bugs go away. */ /* XXXX remove this once more bugs go away. */
@ -2409,7 +2412,8 @@ _extrainfo_free(void *e)
void void
routerlist_free(routerlist_t *rl) routerlist_free(routerlist_t *rl)
{ {
tor_assert(rl); if (!rl)
return;
rimap_free(rl->identity_map, NULL); rimap_free(rl->identity_map, NULL);
sdmap_free(rl->desc_digest_map, NULL); sdmap_free(rl->desc_digest_map, NULL);
sdmap_free(rl->desc_by_eid_map, NULL); sdmap_free(rl->desc_by_eid_map, NULL);
@ -3779,6 +3783,9 @@ authority_cert_free(authority_cert_t *cert)
static void static void
trusted_dir_server_free(trusted_dir_server_t *ds) trusted_dir_server_free(trusted_dir_server_t *ds)
{ {
if (!ds)
return;
tor_free(ds->nickname); tor_free(ds->nickname);
tor_free(ds->description); tor_free(ds->description);
tor_free(ds->address); tor_free(ds->address);
@ -5305,6 +5312,9 @@ routerset_equal(const routerset_t *old, const routerset_t *new)
void void
routerset_free(routerset_t *routerset) routerset_free(routerset_t *routerset)
{ {
if (!routerset)
return;
SMARTLIST_FOREACH(routerset->list, char *, cp, tor_free(cp)); SMARTLIST_FOREACH(routerset->list, char *, cp, tor_free(cp));
smartlist_free(routerset->list); smartlist_free(routerset->list);
SMARTLIST_FOREACH(routerset->policies, addr_policy_t *, p, SMARTLIST_FOREACH(routerset->policies, addr_policy_t *, p,

View File

@ -151,7 +151,7 @@ typedef enum {
* type. * type.
* *
* This structure is only allocated in memareas; do not allocate it on * This structure is only allocated in memareas; do not allocate it on
* the heap, or token_free() won't work. * the heap, or token_clear() won't work.
*/ */
typedef struct directory_token_t { typedef struct directory_token_t {
directory_keyword tp; /**< Type of the token. */ directory_keyword tp; /**< Type of the token. */
@ -523,7 +523,7 @@ static int router_get_hash_impl(const char *s, char *digest,
static int router_get_hashes_impl(const char *s, digests_t *digests, static int router_get_hashes_impl(const char *s, digests_t *digests,
const char *start_str, const char *end_str, const char *start_str, const char *end_str,
char end_char); char end_char);
static void token_free(directory_token_t *tok); static void token_clear(directory_token_t *tok);
static smartlist_t *find_all_exitpolicy(smartlist_t *s); static smartlist_t *find_all_exitpolicy(smartlist_t *s);
static directory_token_t *_find_by_keyword(smartlist_t *s, static directory_token_t *_find_by_keyword(smartlist_t *s,
directory_keyword keyword, directory_keyword keyword,
@ -844,7 +844,7 @@ router_parse_directory(const char *str)
CST_CHECK_AUTHORITY, "directory")<0) CST_CHECK_AUTHORITY, "directory")<0)
goto err; goto err;
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_clear(tokens); smartlist_clear(tokens);
memarea_clear(area); memarea_clear(area);
@ -882,7 +882,7 @@ router_parse_directory(const char *str)
done: done:
if (declared_key) crypto_free_pk_env(declared_key); if (declared_key) crypto_free_pk_env(declared_key);
if (tokens) { if (tokens) {
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_free(tokens); smartlist_free(tokens);
} }
if (area) { if (area) {
@ -948,7 +948,7 @@ router_parse_runningrouters(const char *str)
dump_desc(str_dup, "v1 running-routers"); dump_desc(str_dup, "v1 running-routers");
if (declared_key) crypto_free_pk_env(declared_key); if (declared_key) crypto_free_pk_env(declared_key);
if (tokens) { if (tokens) {
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_free(tokens); smartlist_free(tokens);
} }
if (area) { if (area) {
@ -998,7 +998,7 @@ find_dir_signing_key(const char *str, const char *eos)
} }
done: done:
if (tok) token_free(tok); if (tok) token_clear(tok);
if (area) { if (area) {
DUMP_AREA(area, "dir-signing-key token"); DUMP_AREA(area, "dir-signing-key token");
memarea_drop_all(area); memarea_drop_all(area);
@ -1551,7 +1551,7 @@ router_parse_entry_from_string(const char *s, const char *end,
router = NULL; router = NULL;
done: done:
if (tokens) { if (tokens) {
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_free(tokens); smartlist_free(tokens);
} }
if (exit_policy_tokens) { if (exit_policy_tokens) {
@ -1677,7 +1677,7 @@ extrainfo_parse_entry_from_string(const char *s, const char *end,
extrainfo = NULL; extrainfo = NULL;
done: done:
if (tokens) { if (tokens) {
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_free(tokens); smartlist_free(tokens);
} }
if (area) { if (area) {
@ -1848,7 +1848,7 @@ authority_cert_parse_from_string(const char *s, const char **end_of_string)
if (end_of_string) { if (end_of_string) {
*end_of_string = eat_whitespace(eos); *end_of_string = eat_whitespace(eos);
} }
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_free(tokens); smartlist_free(tokens);
if (area) { if (area) {
DUMP_AREA(area, "authority cert"); DUMP_AREA(area, "authority cert");
@ -1858,7 +1858,7 @@ authority_cert_parse_from_string(const char *s, const char **end_of_string)
err: err:
dump_desc(s_dup, "authority cert"); dump_desc(s_dup, "authority cert");
authority_cert_free(cert); authority_cert_free(cert);
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_free(tokens); smartlist_free(tokens);
if (area) { if (area) {
DUMP_AREA(area, "authority cert"); DUMP_AREA(area, "authority cert");
@ -2129,7 +2129,7 @@ routerstatus_parse_entry_from_string(memarea_t *area,
routerstatus_free(rs); routerstatus_free(rs);
rs = NULL; rs = NULL;
done: done:
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_clear(tokens); smartlist_clear(tokens);
if (area) { if (area) {
DUMP_AREA(area, "routerstatus entry"); DUMP_AREA(area, "routerstatus entry");
@ -2280,7 +2280,7 @@ networkstatus_v2_parse_from_string(const char *s)
ns->entries = smartlist_create(); ns->entries = smartlist_create();
s = eos; s = eos;
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_clear(tokens); smartlist_clear(tokens);
memarea_clear(area); memarea_clear(area);
while (!strcmpstart(s, "r ")) { while (!strcmpstart(s, "r ")) {
@ -2320,9 +2320,9 @@ networkstatus_v2_parse_from_string(const char *s)
networkstatus_v2_free(ns); networkstatus_v2_free(ns);
ns = NULL; ns = NULL;
done: done:
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_free(tokens); smartlist_free(tokens);
SMARTLIST_FOREACH(footer_tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(footer_tokens, directory_token_t *, t, token_clear(t));
smartlist_free(footer_tokens); smartlist_free(footer_tokens);
if (area) { if (area) {
DUMP_AREA(area, "v2 networkstatus"); DUMP_AREA(area, "v2 networkstatus");
@ -2799,7 +2799,7 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
ns = NULL; ns = NULL;
done: done:
if (tokens) { if (tokens) {
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_free(tokens); smartlist_free(tokens);
} }
if (voter) { if (voter) {
@ -2814,11 +2814,11 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
tor_free(voter); tor_free(voter);
} }
if (rs_tokens) { if (rs_tokens) {
SMARTLIST_FOREACH(rs_tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(rs_tokens, directory_token_t *, t, token_clear(t));
smartlist_free(rs_tokens); smartlist_free(rs_tokens);
} }
if (footer_tokens) { if (footer_tokens) {
SMARTLIST_FOREACH(footer_tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(footer_tokens, directory_token_t *, t, token_clear(t));
smartlist_free(footer_tokens); smartlist_free(footer_tokens);
} }
if (area) { if (area) {
@ -3052,7 +3052,7 @@ networkstatus_parse_detached_signatures(const char *s, const char *eos)
ns_detached_signatures_free(sigs); ns_detached_signatures_free(sigs);
sigs = NULL; sigs = NULL;
done: done:
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_free(tokens); smartlist_free(tokens);
if (area) { if (area) {
DUMP_AREA(area, "detached signatures"); DUMP_AREA(area, "detached signatures");
@ -3108,7 +3108,7 @@ router_parse_addr_policy_item_from_string(const char *s, int assume_action)
err: err:
r = NULL; r = NULL;
done: done:
token_free(tok); token_clear(tok);
if (area) { if (area) {
DUMP_AREA(area, "policy item"); DUMP_AREA(area, "policy item");
memarea_drop_all(area); memarea_drop_all(area);
@ -3231,9 +3231,8 @@ assert_addr_policy_ok(smartlist_t *lst)
/** Free all resources allocated for <b>tok</b> */ /** Free all resources allocated for <b>tok</b> */
static void static void
token_free(directory_token_t *tok) token_clear(directory_token_t *tok)
{ {
tor_assert(tok);
if (tok->key) if (tok->key)
crypto_free_pk_env(tok->key); crypto_free_pk_env(tok->key);
} }
@ -3245,7 +3244,7 @@ token_free(directory_token_t *tok)
#define RET_ERR(msg) \ #define RET_ERR(msg) \
STMT_BEGIN \ STMT_BEGIN \
if (tok) token_free(tok); \ if (tok) token_clear(tok); \
tok = ALLOC_ZERO(sizeof(directory_token_t)); \ tok = ALLOC_ZERO(sizeof(directory_token_t)); \
tok->tp = _ERR; \ tok->tp = _ERR; \
tok->error = STRDUP(msg); \ tok->error = STRDUP(msg); \
@ -3523,7 +3522,7 @@ tokenize_string(memarea_t *area,
tok = get_next_token(area, s, end, table); tok = get_next_token(area, s, end, table);
if (tok->tp == _ERR) { if (tok->tp == _ERR) {
log_warn(LD_DIR, "parse error: %s", tok->error); log_warn(LD_DIR, "parse error: %s", tok->error);
token_free(tok); token_clear(tok);
return -1; return -1;
} }
++counts[tok->tp]; ++counts[tok->tp];
@ -4270,7 +4269,7 @@ rend_parse_v2_service_descriptor(rend_service_descriptor_t **parsed_out,
result = NULL; result = NULL;
done: done:
if (tokens) { if (tokens) {
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_free(tokens); smartlist_free(tokens);
} }
if (area) if (area)
@ -4428,7 +4427,7 @@ rend_parse_introduction_points(rend_service_descriptor_t *parsed,
eos = eos+1; eos = eos+1;
tor_assert(eos <= intro_points_encoded+intro_points_encoded_size); tor_assert(eos <= intro_points_encoded+intro_points_encoded_size);
/* Free tokens and clear token list. */ /* Free tokens and clear token list. */
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_clear(tokens); smartlist_clear(tokens);
memarea_clear(area); memarea_clear(area);
/* Tokenize string. */ /* Tokenize string. */
@ -4501,7 +4500,7 @@ rend_parse_introduction_points(rend_service_descriptor_t *parsed,
done: done:
/* Free tokens and clear token list. */ /* Free tokens and clear token list. */
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_free(tokens); smartlist_free(tokens);
if (area) if (area)
memarea_drop_all(area); memarea_drop_all(area);
@ -4540,7 +4539,7 @@ rend_parse_client_keys(strmap_t *parsed_clients, const char *ckstr)
else else
eos = eos + 1; eos = eos + 1;
/* Free tokens and clear token list. */ /* Free tokens and clear token list. */
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_clear(tokens); smartlist_clear(tokens);
memarea_clear(area); memarea_clear(area);
/* Tokenize string. */ /* Tokenize string. */
@ -4612,7 +4611,7 @@ rend_parse_client_keys(strmap_t *parsed_clients, const char *ckstr)
result = -1; result = -1;
done: done:
/* Free tokens and clear token list. */ /* Free tokens and clear token list. */
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t)); SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_free(tokens); smartlist_free(tokens);
if (area) if (area)
memarea_drop_all(area); memarea_drop_all(area);