Commit 3807db00 authored by Sebastian Hahn's avatar Sebastian Hahn
Browse files

*_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.
parent 4afdb790
......@@ -263,7 +263,8 @@ aes_set_key(aes_cnt_cipher_t *cipher, const char *key, int key_bits)
void
aes_free_cipher(aes_cnt_cipher_t *cipher)
{
tor_assert(cipher);
if (!cipher)
return;
#ifdef USE_OPENSSL_EVP
EVP_CIPHER_CTX_cleanup(&cipher->key);
#endif
......
......@@ -2044,6 +2044,8 @@ tor_mutex_new(void)
void
tor_mutex_free(tor_mutex_t *m)
{
if (!m)
return;
tor_mutex_uninit(m);
tor_free(m);
}
......@@ -2071,7 +2073,8 @@ tor_cond_new(void)
void
tor_cond_free(tor_cond_t *cond)
{
tor_assert(cond);
if (!cond)
return;
if (pthread_cond_destroy(&cond->cond)) {
log_warn(LD_GENERAL,"Error freeing condition: %s", strerror(errno));
return;
......@@ -2128,7 +2131,8 @@ tor_cond_new(void)
void
tor_cond_free(tor_cond_t *cond)
{
tor_assert(cond);
if (!cond)
return;
DeleteCriticalSection(&cond->mutex);
/* XXXX notify? */
smartlist_free(cond->events);
......
......@@ -44,7 +44,8 @@ smartlist_create(void)
void
smartlist_free(smartlist_t *sl)
{
tor_assert(sl != NULL);
if (!sl)
return;
tor_free(sl->list);
tor_free(sl);
}
......@@ -1187,6 +1188,9 @@ void
strmap_free(strmap_t *map, void (*free_val)(void*))
{
strmap_entry_t **ent, **next, *this;
if (!map)
return;
for (ent = HT_START(strmap_impl, &map->head); ent != NULL; ent = next) {
this = *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_entry_t **ent, **next, *this;
if (!map)
return;
for (ent = HT_START(digestmap_impl, &map->head); ent != NULL; ent = next) {
this = *ent;
next = HT_NEXT_RMV(digestmap_impl, &map->head, ent);
......@@ -1323,6 +1329,8 @@ digestset_new(int max_elements)
void
digestset_free(digestset_t *set)
{
if (!set)
return;
bitarray_free(set->ba);
tor_free(set);
}
......
......@@ -400,7 +400,8 @@ crypto_new_pk_env(void)
void
crypto_free_pk_env(crypto_pk_env_t *env)
{
tor_assert(env);
if (!env)
return;
if (--env->refs > 0)
return;
......@@ -463,7 +464,8 @@ crypto_new_cipher_env(void)
void
crypto_free_cipher_env(crypto_cipher_env_t *env)
{
tor_assert(env);
if (!env)
return;
tor_assert(env->cipher);
aes_free_cipher(env->cipher);
......@@ -1528,6 +1530,8 @@ crypto_new_digest256_env(digest_algorithm_t algorithm)
void
crypto_free_digest_env(crypto_digest_env_t *digest)
{
if (!digest)
return;
memset(digest, 0, sizeof(crypto_digest_env_t));
tor_free(digest);
}
......@@ -1899,7 +1903,8 @@ crypto_expand_key_material(const char *key_in, size_t key_in_len,
void
crypto_dh_free(crypto_dh_env_t *dh)
{
tor_assert(dh);
if (!dh)
return;
tor_assert(dh->dh);
DH_free(dh->dh);
tor_free(dh);
......
......@@ -426,6 +426,8 @@ _log_err(log_domain_mask_t domain, const char *format, ...)
static void
log_free(logfile_t *victim)
{
if (!victim)
return;
tor_free(victim->severities);
tor_free(victim->filename);
tor_free(victim);
......
......@@ -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
* or by freeing it if the freelist is already too big. */
static void
chunk_free(memarea_chunk_t *chunk)
chunk_free_unchecked(memarea_chunk_t *chunk)
{
CHECK_SENTINEL(chunk);
if (freelist_len < MAX_FREELIST_LEN) {
......@@ -151,7 +151,7 @@ memarea_drop_all(memarea_t *area)
memarea_chunk_t *chunk, *next;
for (chunk = area->first; chunk; chunk = next) {
next = chunk->next_chunk;
chunk_free(chunk);
chunk_free_unchecked(chunk);
}
area->first = NULL; /*fail fast on */
tor_free(area);
......@@ -167,7 +167,7 @@ memarea_clear(memarea_t *area)
if (area->first->next_chunk) {
for (chunk = area->first->next_chunk; chunk; chunk = next) {
next = chunk->next_chunk;
chunk_free(chunk);
chunk_free_unchecked(chunk);
}
area->first->next_chunk = NULL;
}
......
......@@ -423,7 +423,8 @@ tor_zlib_process(tor_zlib_state_t *state,
void
tor_zlib_free(tor_zlib_state_t *state)
{
tor_assert(state);
if (!state)
return;
if (state->compress)
deflateEnd(&state->stream);
......
......@@ -986,7 +986,9 @@ void
tor_tls_free(tor_tls_t *tls)
{
tor_tls_t *removed;
tor_assert(tls && tls->ssl);
if (!tls)
return;
tor_assert(tls->ssl);
removed = HT_REMOVE(tlsmap, &tlsmap_root, tls);
if (!removed) {
log_warn(LD_BUG, "Freeing a TLS that was not in the ssl->tls map.");
......
......@@ -147,10 +147,13 @@ get_freelist(size_t alloc)
/** Deallocate a chunk or put it on a freelist */
static void
chunk_free(chunk_t *chunk)
chunk_free_unchecked(chunk_t *chunk)
{
size_t alloc = CHUNK_ALLOC_SIZE(chunk->memlen);
chunk_freelist_t *freelist = get_freelist(alloc);
size_t alloc;
chunk_freelist_t *freelist;
alloc = CHUNK_ALLOC_SIZE(chunk->memlen);
freelist = get_freelist(alloc);
if (freelist && freelist->cur_length < freelist->max_length) {
chunk->next = freelist->head;
freelist->head = chunk;
......@@ -195,7 +198,7 @@ chunk_new_with_alloc_size(size_t alloc)
}
#else
static void
chunk_free(chunk_t *chunk)
chunk_free_unchecked(chunk_t *chunk)
{
tor_free(chunk);
}
......@@ -403,7 +406,7 @@ buf_pullup(buf_t *buf, size_t bytes, int nulterminate)
dest->next = src->next;
if (buf->tail == src)
buf->tail = dest;
chunk_free(src);
chunk_free_unchecked(src);
} else {
memcpy(CHUNK_WRITE_PTR(dest), src->data, n);
dest->datalen += n;
......@@ -449,7 +452,7 @@ buf_remove_from_front(buf_t *buf, size_t n)
buf->head = victim->next;
if (buf->tail == victim)
buf->tail = NULL;
chunk_free(victim);
chunk_free_unchecked(victim);
}
}
check();
......@@ -483,7 +486,7 @@ buf_clear(buf_t *buf)
buf->datalen = 0;
for (chunk = buf->head; chunk; chunk = next) {
next = chunk->next;
chunk_free(chunk);
chunk_free_unchecked(chunk);
}
buf->head = buf->tail = NULL;
}
......@@ -522,6 +525,8 @@ buf_slack(const buf_t *buf)
void
buf_free(buf_t *buf)
{
if (!buf)
return;
buf_clear(buf);
buf->magic = 0xdeadbeef;
tor_free(buf);
......
......@@ -2744,7 +2744,8 @@ extend_info_from_router(routerinfo_t *r)
void
extend_info_free(extend_info_t *info)
{
tor_assert(info);
if (!info)
return;
if (info->onion_key)
crypto_free_pk_env(info->onion_key);
tor_free(info);
......@@ -3053,7 +3054,8 @@ pick_entry_guards(void)
static void
entry_guard_free(entry_guard_t *e)
{
tor_assert(e);
if (!e)
return;
tor_free(e->chosen_by_version);
tor_free(e);
}
......
......@@ -442,7 +442,9 @@ circuit_free(circuit_t *circ)
{
void *mem;
size_t memlen;
tor_assert(circ);
if (!circ)
return;
if (CIRCUIT_IS_ORIGIN(circ)) {
origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
mem = ocirc;
......@@ -558,6 +560,9 @@ circuit_free_all(void)
static void
circuit_free_cpath_node(crypt_path_t *victim)
{
if (!victim)
return;
if (victim->f_crypto)
crypto_free_cipher_env(victim->f_crypto);
if (victim->b_crypto)
......
......@@ -859,6 +859,9 @@ get_version(void)
static void
or_options_free(or_options_t *options)
{
if (!options)
return;
if (options->_ExcludeExitNodesUnion)
routerset_free(options->_ExcludeExitNodesUnion);
config_free(&options_format, options);
......@@ -2609,7 +2612,10 @@ config_free(config_format_t *fmt, void *options)
{
int i;
tor_assert(options);
if (!options)
return;
tor_assert(fmt);
for (i=0; fmt->vars[i].name; ++i)
option_clear(fmt, options, &(fmt->vars[i]));
......
......@@ -311,6 +311,9 @@ _connection_free(connection_t *conn)
{
void *mem;
size_t memlen;
if (!conn)
return;
switch (conn->type) {
case CONN_TYPE_OR:
tor_assert(conn->magic == OR_CONNECTION_MAGIC);
......@@ -432,7 +435,8 @@ _connection_free(connection_t *conn)
void
connection_free(connection_t *conn)
{
tor_assert(conn);
if (!conn)
return;
tor_assert(!connection_is_on_closeable_list(conn));
tor_assert(!connection_in_array(conn));
if (conn->linked_conn) {
......
......@@ -688,7 +688,11 @@ addressmap_init(void)
static void
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);
}
......@@ -697,7 +701,11 @@ addressmap_ent_free(void *_ent)
static void
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->hostname_address);
tor_free(ent);
......
......@@ -1075,7 +1075,8 @@ connection_init_or_handshake_state(or_connection_t *conn, int started_here)
void
or_handshake_state_free(or_handshake_state_t *state)
{
tor_assert(state);
if (!state)
return;
memset(state, 0xBE, sizeof(or_handshake_state_t));
tor_free(state);
}
......
......@@ -1292,7 +1292,11 @@ clear_cached_dir(cached_dir_t *d)
static void
_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);
}
......
......@@ -1697,6 +1697,8 @@ get_detached_signatures_from_pending_consensuses(pending_consensus_t *pending,
void
ns_detached_signatures_free(ns_detached_signatures_t *s)
{
if (!s)
return;
if (s->signatures) {
STRMAP_FOREACH(s->signatures, flavor, smartlist_t *, sigs) {
SMARTLIST_FOREACH(sigs, document_signature_t *, sig,
......
......@@ -301,6 +301,8 @@ dns_get_expiry_ttl(uint32_t ttl)
static void
_free_cached_resolve(cached_resolve_t *r)
{
if (!r)
return;
while (r->pending_connections) {
pending_connection_t *victim = r->pending_connections;
r->pending_connections = victim->next;
......
......@@ -266,6 +266,8 @@ static void
vote_routerstatus_free(vote_routerstatus_t *rs)
{
vote_microdesc_hash_t *h, *next;
if (!rs)
return;
tor_free(rs->version);
tor_free(rs->status.exitsummary);
for (h = rs->microdesc; h; h = next) {
......@@ -280,6 +282,8 @@ vote_routerstatus_free(vote_routerstatus_t *rs)
void
routerstatus_free(routerstatus_t *rs)
{
if (!rs)
return;
tor_free(rs->exitsummary);
tor_free(rs);
}
......@@ -288,6 +292,8 @@ routerstatus_free(routerstatus_t *rs)
void
networkstatus_v2_free(networkstatus_v2_t *ns)
{
if (!ns)
return;
tor_free(ns->source_address);
tor_free(ns->contact);
if (ns->signing_key)
......
......@@ -1276,7 +1276,8 @@ getinfo_helper_policies(control_connection_t *conn,
void
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_free(lst);
}
......@@ -1285,19 +1286,20 @@ addr_policy_list_free(smartlist_t *lst)
void
addr_policy_free(addr_policy_t *p)
{
if (p) {
if (--p->refcnt <= 0) {
if (p->is_canonical) {
policy_map_ent_t search, *found;
search.policy = p;
found = HT_REMOVE(policy_map, &policy_root, &search);
if (found) {
tor_assert(p == found->policy);
tor_free(found);
}
if (!p)
return;
if (--p->refcnt <= 0) {
if (p->is_canonical) {
policy_map_ent_t search, *found;
search.policy = p;
found = HT_REMOVE(policy_map, &policy_root, &search);
if (found) {
tor_assert(p == found->policy);
tor_free(found);
}
tor_free(p);
}
tor_free(p);
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment