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) ...@@ -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
......
...@@ -2044,6 +2044,8 @@ tor_mutex_new(void) ...@@ -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) ...@@ -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) ...@@ -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);
......
...@@ -44,7 +44,8 @@ smartlist_create(void) ...@@ -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 ...@@ -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 ...@@ -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) ...@@ -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);
} }
......
...@@ -400,7 +400,8 @@ crypto_new_pk_env(void) ...@@ -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) ...@@ -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) ...@@ -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, ...@@ -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);
......
...@@ -426,6 +426,8 @@ _log_err(log_domain_mask_t domain, const char *format, ...) ...@@ -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);
......
...@@ -121,7 +121,7 @@ alloc_chunk(size_t sz, int freelist_ok) ...@@ -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) ...@@ -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) ...@@ -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;
} }
......
...@@ -423,7 +423,8 @@ tor_zlib_process(tor_zlib_state_t *state, ...@@ -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);
......
...@@ -986,7 +986,9 @@ void ...@@ -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.");
......
...@@ -147,10 +147,13 @@ get_freelist(size_t alloc) ...@@ -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) ...@@ -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) ...@@ -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) ...@@ -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) ...@@ -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) ...@@ -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);
......
...@@ -2744,7 +2744,8 @@ extend_info_from_router(routerinfo_t *r) ...@@ -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) ...@@ -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);
} }
......
...@@ -442,7 +442,9 @@ circuit_free(circuit_t *circ) ...@@ -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) ...@@ -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)
......
...@@ -859,6 +859,9 @@ get_version(void) ...@@ -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) ...@@ -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]));
......
...@@ -311,6 +311,9 @@ _connection_free(connection_t *conn) ...@@ -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) ...@@ -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) {
......
...@@ -688,7 +688,11 @@ addressmap_init(void) ...@@ -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) ...@@ -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);
......
...@@ -1075,7 +1075,8 @@ connection_init_or_handshake_state(or_connection_t *conn, int started_here) ...@@ -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);
} }
......
...@@ -1292,7 +1292,11 @@ clear_cached_dir(cached_dir_t *d) ...@@ -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);
} }
......
...@@ -1697,6 +1697,8 @@ get_detached_signatures_from_pending_consensuses(pending_consensus_t *pending, ...@@ -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,
......
...@@ -301,6 +301,8 @@ dns_get_expiry_ttl(uint32_t ttl) ...@@ -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;
......
...@@ -266,6 +266,8 @@ static void ...@@ -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) ...@@ -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) ...@@ -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)
......
...@@ -1276,7 +1276,8 @@ getinfo_helper_policies(control_connection_t *conn, ...@@ -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;