[or-cvs] [tor/master] *_free functions now accept NULL

Nick Mathewson nickm at seul.org
Sat Dec 12 07:07:03 UTC 2009


Author: Sebastian Hahn <sebastian at torproject.org>
Date: Mon, 28 Sep 2009 16:37:01 +0200
Subject: *_free functions now accept NULL
Commit: 3807db001d71c51e53c1897ae067671f5b771f2f

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.
---
 src/common/aes.c         |    3 +-
 src/common/compat.c      |    8 +++++-
 src/common/container.c   |   10 +++++++-
 src/common/crypto.c      |   11 ++++++--
 src/common/log.c         |    2 +
 src/common/memarea.c     |    6 ++--
 src/common/torgzip.c     |    3 +-
 src/common/tortls.c      |    4 ++-
 src/or/buffers.c         |   19 ++++++++++------
 src/or/circuitbuild.c    |    6 +++-
 src/or/circuitlist.c     |    7 +++++-
 src/or/config.c          |    8 ++++++-
 src/or/connection.c      |    6 ++++-
 src/or/connection_edge.c |   12 ++++++++-
 src/or/connection_or.c   |    3 +-
 src/or/dirserv.c         |    6 ++++-
 src/or/dirvote.c         |    2 +
 src/or/dns.c             |    2 +
 src/or/networkstatus.c   |    6 +++++
 src/or/policies.c        |   26 +++++++++++----------
 src/or/relay.c           |    6 ++--
 src/or/rendcommon.c      |    6 +++++
 src/or/rendservice.c     |   11 ++++++---
 src/or/rephist.c         |    4 +++
 src/or/routerlist.c      |   12 +++++++++-
 src/or/routerparse.c     |   55 ++++++++++++++++++++++-----------------------
 26 files changed, 168 insertions(+), 76 deletions(-)

diff --git a/src/common/aes.c b/src/common/aes.c
index e076656..451c31f 100644
--- a/src/common/aes.c
+++ b/src/common/aes.c
@@ -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
diff --git a/src/common/compat.c b/src/common/compat.c
index dbd3197..87dedc5 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -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);
diff --git a/src/common/container.c b/src/common/container.c
index f3540f7..7690b4c 100644
--- a/src/common/container.c
+++ b/src/common/container.c
@@ -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);
 }
diff --git a/src/common/crypto.c b/src/common/crypto.c
index 4c880f6..4d17a8f 100644
--- a/src/common/crypto.c
+++ b/src/common/crypto.c
@@ -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);
diff --git a/src/common/log.c b/src/common/log.c
index 9912080..5b5b9e0 100644
--- a/src/common/log.c
+++ b/src/common/log.c
@@ -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);
diff --git a/src/common/memarea.c b/src/common/memarea.c
index e7f6720..661bd85 100644
--- a/src/common/memarea.c
+++ b/src/common/memarea.c
@@ -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;
   }
diff --git a/src/common/torgzip.c b/src/common/torgzip.c
index 762f2e7..13e0c7f 100644
--- a/src/common/torgzip.c
+++ b/src/common/torgzip.c
@@ -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);
diff --git a/src/common/tortls.c b/src/common/tortls.c
index 71d0bd6..0fde617 100644
--- a/src/common/tortls.c
+++ b/src/common/tortls.c
@@ -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.");
diff --git a/src/or/buffers.c b/src/or/buffers.c
index 1a1b207..7c40e47 100644
--- a/src/or/buffers.c
+++ b/src/or/buffers.c
@@ -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);
diff --git a/src/or/circuitbuild.c b/src/or/circuitbuild.c
index 91fa9d8..29f9d77 100644
--- a/src/or/circuitbuild.c
+++ b/src/or/circuitbuild.c
@@ -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);
 }
diff --git a/src/or/circuitlist.c b/src/or/circuitlist.c
index 02bf925..e0cb644 100644
--- a/src/or/circuitlist.c
+++ b/src/or/circuitlist.c
@@ -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)
diff --git a/src/or/config.c b/src/or/config.c
index 66f9d04..6ab87ab 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -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]));
diff --git a/src/or/connection.c b/src/or/connection.c
index 0600d97..ddcf08c 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -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) {
diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c
index 75a57fe..b0ba961 100644
--- a/src/or/connection_edge.c
+++ b/src/or/connection_edge.c
@@ -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);
diff --git a/src/or/connection_or.c b/src/or/connection_or.c
index bbd6439..2ed6add 100644
--- a/src/or/connection_or.c
+++ b/src/or/connection_or.c
@@ -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);
 }
diff --git a/src/or/dirserv.c b/src/or/dirserv.c
index 3700cd1..3338cd7 100644
--- a/src/or/dirserv.c
+++ b/src/or/dirserv.c
@@ -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);
 }
 
diff --git a/src/or/dirvote.c b/src/or/dirvote.c
index f745db6..b4f76b3 100644
--- a/src/or/dirvote.c
+++ b/src/or/dirvote.c
@@ -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,
diff --git a/src/or/dns.c b/src/or/dns.c
index ffd30c8..8951780 100644
--- a/src/or/dns.c
+++ b/src/or/dns.c
@@ -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;
diff --git a/src/or/networkstatus.c b/src/or/networkstatus.c
index e9e8663..c8bb033 100644
--- a/src/or/networkstatus.c
+++ b/src/or/networkstatus.c
@@ -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)
diff --git a/src/or/policies.c b/src/or/policies.c
index 023cd47..a852ce1 100644
--- a/src/or/policies.c
+++ b/src/or/policies.c
@@ -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);
   }
 }
 
diff --git a/src/or/relay.c b/src/or/relay.c
index 00e70d9..ac305ce 100644
--- a/src/or/relay.c
+++ b/src/or/relay.c
@@ -1563,7 +1563,7 @@ clean_cell_pool(void)
 
 /** Release storage held by <b>cell</b>. */
 static INLINE void
-packed_cell_free(packed_cell_t *cell)
+packed_cell_free_unchecked(packed_cell_t *cell)
 {
   --total_cells_allocated;
   mp_pool_release(cell);
@@ -1667,7 +1667,7 @@ cell_queue_clear(cell_queue_t *queue)
   cell = queue->head;
   while (cell) {
     next = cell->next;
-    packed_cell_free(cell);
+    packed_cell_free_unchecked(cell);
     cell = next;
   }
   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));
 
-    packed_cell_free(cell);
+    packed_cell_free_unchecked(cell);
     ++n_flushed;
     if (circ != conn->active_circuits) {
       /* If this happens, the current circuit just got made inactive by
diff --git a/src/or/rendcommon.c b/src/or/rendcommon.c
index 9055f98..a68ee0c 100644
--- a/src/or/rendcommon.c
+++ b/src/or/rendcommon.c
@@ -22,6 +22,8 @@ rend_cmp_service_ids(const char *one, const char *two)
 void
 rend_service_descriptor_free(rend_service_descriptor_t *desc)
 {
+  if (!desc)
+    return;
   if (desc->pk)
     crypto_free_pk_env(desc->pk);
   if (desc->intro_nodes) {
@@ -414,6 +416,8 @@ void
 rend_encoded_v2_service_descriptor_free(
   rend_encoded_v2_service_descriptor_t *desc)
 {
+  if (!desc)
+    return;
   tor_free(desc->desc_str);
   tor_free(desc);
 }
@@ -422,6 +426,8 @@ rend_encoded_v2_service_descriptor_free(
 void
 rend_intro_point_free(rend_intro_point_t *intro)
 {
+  if (!intro)
+    return;
   if (intro->extend_info)
     extend_info_free(intro->extend_info);
   if (intro->intro_key)
diff --git a/src/or/rendservice.c b/src/or/rendservice.c
index b6981d6..445fbff 100644
--- a/src/or/rendservice.c
+++ b/src/or/rendservice.c
@@ -87,7 +87,8 @@ num_rend_services(void)
 static void
 rend_authorized_client_free(rend_authorized_client_t *client)
 {
-  if (!client) return;
+  if (!client)
+    return;
   if (client->client_key)
     crypto_free_pk_env(client->client_key);
   tor_free(client->client_name);
@@ -106,7 +107,9 @@ rend_authorized_client_strmap_item_free(void *authorized_client)
 static void
 rend_service_free(rend_service_t *service)
 {
-  if (!service) return;
+  if (!service)
+    return;
+
   tor_free(service->directory);
   SMARTLIST_FOREACH(service->ports, void*, p, tor_free(p));
   smartlist_free(service->ports);
@@ -134,9 +137,9 @@ rend_service_free(rend_service_t *service)
 void
 rend_service_free_all(void)
 {
-  if (!rend_service_list) {
+  if (!rend_service_list)
     return;
-  }
+
   SMARTLIST_FOREACH(rend_service_list, rend_service_t*, ptr,
                     rend_service_free(ptr));
   smartlist_free(rend_service_list);
diff --git a/src/or/rephist.c b/src/or/rephist.c
index 1ff9cde..78ceb5f 100644
--- a/src/or/rephist.c
+++ b/src/or/rephist.c
@@ -2272,6 +2272,8 @@ static void
 hs_usage_general_period_related_observations_free(
                              hs_usage_general_period_related_observations_t *s)
 {
+  if (!s)
+    return;
   rephist_total_alloc-=sizeof(hs_usage_general_period_related_observations_t);
   tor_free(s);
 }
@@ -2281,6 +2283,8 @@ static void
 hs_usage_current_observation_period_free(
                                     hs_usage_current_observation_period_t *s)
 {
+  if (!s)
+    return;
   rephist_total_alloc -= sizeof(hs_usage_current_observation_period_t);
   tor_free(s);
 }
diff --git a/src/or/routerlist.c b/src/or/routerlist.c
index 18d656d..3484338 100644
--- a/src/or/routerlist.c
+++ b/src/or/routerlist.c
@@ -2378,6 +2378,9 @@ extrainfo_free(extrainfo_t *extrainfo)
 static void
 signed_descriptor_free(signed_descriptor_t *sd)
 {
+  if (!sd)
+    return;
+
   tor_free(sd->signed_descriptor_body);
 
   /* XXXX remove this once more bugs go away. */
@@ -2409,7 +2412,8 @@ _extrainfo_free(void *e)
 void
 routerlist_free(routerlist_t *rl)
 {
-  tor_assert(rl);
+  if (!rl)
+    return;
   rimap_free(rl->identity_map, NULL);
   sdmap_free(rl->desc_digest_map, NULL);
   sdmap_free(rl->desc_by_eid_map, NULL);
@@ -3779,6 +3783,9 @@ authority_cert_free(authority_cert_t *cert)
 static void
 trusted_dir_server_free(trusted_dir_server_t *ds)
 {
+  if (!ds)
+    return;
+
   tor_free(ds->nickname);
   tor_free(ds->description);
   tor_free(ds->address);
@@ -5305,6 +5312,9 @@ routerset_equal(const routerset_t *old, const routerset_t *new)
 void
 routerset_free(routerset_t *routerset)
 {
+  if (!routerset)
+    return;
+
   SMARTLIST_FOREACH(routerset->list, char *, cp, tor_free(cp));
   smartlist_free(routerset->list);
   SMARTLIST_FOREACH(routerset->policies, addr_policy_t *, p,
diff --git a/src/or/routerparse.c b/src/or/routerparse.c
index 1f89cff..864056f 100644
--- a/src/or/routerparse.c
+++ b/src/or/routerparse.c
@@ -151,7 +151,7 @@ typedef enum {
  * type.
  *
  * 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 {
   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,
                                   const char *start_str, const char *end_str,
                                   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 directory_token_t *_find_by_keyword(smartlist_t *s,
                                            directory_keyword keyword,
@@ -844,7 +844,7 @@ router_parse_directory(const char *str)
                             CST_CHECK_AUTHORITY, "directory")<0)
     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);
   memarea_clear(area);
 
@@ -882,7 +882,7 @@ router_parse_directory(const char *str)
  done:
   if (declared_key) crypto_free_pk_env(declared_key);
   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);
   }
   if (area) {
@@ -948,7 +948,7 @@ router_parse_runningrouters(const char *str)
   dump_desc(str_dup, "v1 running-routers");
   if (declared_key) crypto_free_pk_env(declared_key);
   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);
   }
   if (area) {
@@ -998,7 +998,7 @@ find_dir_signing_key(const char *str, const char *eos)
   }
 
  done:
-  if (tok) token_free(tok);
+  if (tok) token_clear(tok);
   if (area) {
     DUMP_AREA(area, "dir-signing-key token");
     memarea_drop_all(area);
@@ -1551,7 +1551,7 @@ router_parse_entry_from_string(const char *s, const char *end,
   router = NULL;
  done:
   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);
   }
   if (exit_policy_tokens) {
@@ -1677,7 +1677,7 @@ extrainfo_parse_entry_from_string(const char *s, const char *end,
   extrainfo = NULL;
  done:
   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);
   }
   if (area) {
@@ -1848,7 +1848,7 @@ authority_cert_parse_from_string(const char *s, const char **end_of_string)
   if (end_of_string) {
     *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);
   if (area) {
     DUMP_AREA(area, "authority cert");
@@ -1858,7 +1858,7 @@ authority_cert_parse_from_string(const char *s, const char **end_of_string)
  err:
   dump_desc(s_dup, "authority 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);
   if (area) {
     DUMP_AREA(area, "authority cert");
@@ -2129,7 +2129,7 @@ routerstatus_parse_entry_from_string(memarea_t *area,
     routerstatus_free(rs);
   rs = NULL;
  done:
-  SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
+  SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
   smartlist_clear(tokens);
   if (area) {
     DUMP_AREA(area, "routerstatus entry");
@@ -2280,7 +2280,7 @@ networkstatus_v2_parse_from_string(const char *s)
 
   ns->entries = smartlist_create();
   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);
   memarea_clear(area);
   while (!strcmpstart(s, "r ")) {
@@ -2320,9 +2320,9 @@ networkstatus_v2_parse_from_string(const char *s)
     networkstatus_v2_free(ns);
   ns = NULL;
  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_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);
   if (area) {
     DUMP_AREA(area, "v2 networkstatus");
@@ -2799,7 +2799,7 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
   ns = NULL;
  done:
   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);
   }
   if (voter) {
@@ -2814,11 +2814,11 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
     tor_free(voter);
   }
   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);
   }
   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);
   }
   if (area) {
@@ -3052,7 +3052,7 @@ networkstatus_parse_detached_signatures(const char *s, const char *eos)
   ns_detached_signatures_free(sigs);
   sigs = NULL;
  done:
-  SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_free(t));
+  SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
   smartlist_free(tokens);
   if (area) {
     DUMP_AREA(area, "detached signatures");
@@ -3108,7 +3108,7 @@ router_parse_addr_policy_item_from_string(const char *s, int assume_action)
  err:
   r = NULL;
  done:
-  token_free(tok);
+  token_clear(tok);
   if (area) {
     DUMP_AREA(area, "policy item");
     memarea_drop_all(area);
@@ -3231,9 +3231,8 @@ assert_addr_policy_ok(smartlist_t *lst)
 
 /** Free all resources allocated for <b>tok</b> */
 static void
-token_free(directory_token_t *tok)
+token_clear(directory_token_t *tok)
 {
-  tor_assert(tok);
   if (tok->key)
     crypto_free_pk_env(tok->key);
 }
@@ -3245,7 +3244,7 @@ token_free(directory_token_t *tok)
 
 #define RET_ERR(msg)                                               \
   STMT_BEGIN                                                       \
-    if (tok) token_free(tok);                                      \
+    if (tok) token_clear(tok);                                      \
     tok = ALLOC_ZERO(sizeof(directory_token_t));                   \
     tok->tp = _ERR;                                                \
     tok->error = STRDUP(msg);                                      \
@@ -3523,7 +3522,7 @@ tokenize_string(memarea_t *area,
     tok = get_next_token(area, s, end, table);
     if (tok->tp == _ERR) {
       log_warn(LD_DIR, "parse error: %s", tok->error);
-      token_free(tok);
+      token_clear(tok);
       return -1;
     }
     ++counts[tok->tp];
@@ -4270,7 +4269,7 @@ rend_parse_v2_service_descriptor(rend_service_descriptor_t **parsed_out,
   result = NULL;
  done:
   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);
   }
   if (area)
@@ -4428,7 +4427,7 @@ rend_parse_introduction_points(rend_service_descriptor_t *parsed,
       eos = eos+1;
     tor_assert(eos <= intro_points_encoded+intro_points_encoded_size);
     /* 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);
     memarea_clear(area);
     /* Tokenize string. */
@@ -4501,7 +4500,7 @@ rend_parse_introduction_points(rend_service_descriptor_t *parsed,
 
  done:
   /* 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);
   if (area)
     memarea_drop_all(area);
@@ -4540,7 +4539,7 @@ rend_parse_client_keys(strmap_t *parsed_clients, const char *ckstr)
     else
       eos = eos + 1;
     /* 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);
     memarea_clear(area);
     /* Tokenize string. */
@@ -4612,7 +4611,7 @@ rend_parse_client_keys(strmap_t *parsed_clients, const char *ckstr)
   result = -1;
  done:
   /* 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);
   if (area)
     memarea_drop_all(area);
-- 
1.5.6.5




More information about the tor-commits mailing list