Now that FOO_free(NULL) always works, remove checks before calling it.

This commit is contained in:
Nick Mathewson 2009-12-12 02:07:59 -05:00
parent 79f72d0ef6
commit 0c1b3070cf
24 changed files with 160 additions and 258 deletions

View file

@ -165,9 +165,7 @@ tor_gzip_compress(char **out, size_t *out_len,
deflateEnd(stream);
tor_free(stream);
}
if (*out) {
tor_free(*out);
}
return -1;
}

View file

@ -1314,9 +1314,7 @@ log_cert_lifetime(X509 *cert, const char *problem)
tls_log_errors(NULL, LOG_WARN, LD_NET, "getting certificate lifetime");
if (bio)
BIO_free(bio);
if (s1)
tor_free(s1);
if (s2)
tor_free(s2);
}

View file

@ -953,7 +953,6 @@ const char *
escaped(const char *s)
{
static char *_escaped_val = NULL;
if (_escaped_val)
tor_free(_escaped_val);
if (s)

View file

@ -364,7 +364,7 @@ circuit_build_times_update_state(circuit_build_times_t *cbt,
or_state_mark_dirty(get_or_state(), 0);
}
if (histogram) tor_free(histogram);
tor_free(histogram);
}
/**
@ -1819,10 +1819,9 @@ circuit_finish_handshake(origin_circuit_t *circ, uint8_t reply_type,
return -END_CIRC_REASON_TORPROTOCOL;
}
if (hop->dh_handshake_state) {
crypto_dh_free(hop->dh_handshake_state); /* don't need it anymore */
hop->dh_handshake_state = NULL;
}
memset(hop->fast_handshake_state, 0, sizeof(hop->fast_handshake_state));
if (circuit_init_cpath_crypto(hop, keys, 0)<0) {
@ -2430,7 +2429,6 @@ circuit_append_new_exit(origin_circuit_t *circ, extend_info_t *exit)
state = circ->build_state;
tor_assert(state);
if (state->chosen_exit)
extend_info_free(state->chosen_exit);
state->chosen_exit = extend_info_dup(exit);
@ -2746,7 +2744,6 @@ extend_info_free(extend_info_t *info)
{
if (!info)
return;
if (info->onion_key)
crypto_free_pk_env(info->onion_key);
tor_free(info);
}

View file

@ -451,17 +451,14 @@ circuit_free(circuit_t *circ)
memlen = sizeof(origin_circuit_t);
tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
if (ocirc->build_state) {
if (ocirc->build_state->chosen_exit)
extend_info_free(ocirc->build_state->chosen_exit);
if (ocirc->build_state->pending_final_cpath)
circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
}
tor_free(ocirc->build_state);
circuit_free_cpath(ocirc->cpath);
if (ocirc->intro_key)
crypto_free_pk_env(ocirc->intro_key);
if (ocirc->rend_data)
rend_data_free(ocirc->rend_data);
} else {
or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
@ -472,13 +469,9 @@ circuit_free(circuit_t *circ)
memlen = sizeof(or_circuit_t);
tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
if (ocirc->p_crypto)
crypto_free_cipher_env(ocirc->p_crypto);
if (ocirc->p_digest)
crypto_free_digest_env(ocirc->p_digest);
if (ocirc->n_crypto)
crypto_free_cipher_env(ocirc->n_crypto);
if (ocirc->n_digest)
crypto_free_digest_env(ocirc->n_digest);
if (ocirc->rend_splice) {
@ -495,7 +488,6 @@ circuit_free(circuit_t *circ)
cell_queue_clear(&ocirc->p_conn_cells);
}
if (circ->n_hop)
extend_info_free(circ->n_hop);
tor_free(circ->n_conn_onionskin);
@ -549,10 +541,10 @@ circuit_free_all(void)
circuit_free(global_circuitlist);
global_circuitlist = next;
}
if (circuits_pending_or_conns) {
smartlist_free(circuits_pending_or_conns);
circuits_pending_or_conns = NULL;
}
HT_CLEAR(orconn_circid_map, &orconn_circid_circuit_map);
}
@ -563,17 +555,11 @@ 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)
crypto_free_cipher_env(victim->b_crypto);
if (victim->f_digest)
crypto_free_digest_env(victim->f_digest);
if (victim->b_digest)
crypto_free_digest_env(victim->b_digest);
if (victim->dh_handshake_state)
crypto_dh_free(victim->dh_handshake_state);
if (victim->extend_info)
extend_info_free(victim->extend_info);
memset(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */

View file

@ -928,7 +928,7 @@ circuit_launch_by_router(uint8_t purpose,
if (exit)
info = extend_info_from_router(exit);
circ = circuit_launch_by_extend_info(purpose, info, flags);
if (info)
extend_info_free(info);
return circ;
}
@ -1222,7 +1222,6 @@ circuit_get_open_circ_or_launch(edge_connection_t *conn,
flags);
}
if (extend_info)
extend_info_free(extend_info);
if (desired_circuit_purpose != CIRCUIT_PURPOSE_C_GENERAL) {

View file

@ -827,7 +827,7 @@ set_options(or_options_t *new_val, char **msg)
"Acting on config options left us in a broken state. Dying.");
exit(1);
}
if (old_options)
config_free(&options_format, old_options);
return 0;
@ -862,7 +862,6 @@ or_options_free(or_options_t *options)
if (!options)
return;
if (options->_ExcludeExitNodesUnion)
routerset_free(options->_ExcludeExitNodesUnion);
config_free(&options_format, options);
}
@ -872,18 +871,15 @@ or_options_free(or_options_t *options)
void
config_free_all(void)
{
if (global_options) {
or_options_free(global_options);
global_options = NULL;
}
if (global_state) {
config_free(&state_format, global_state);
global_state = NULL;
}
if (global_cmdline_options) {
config_free_lines(global_cmdline_options);
global_cmdline_options = NULL;
}
tor_free(torrc_fname);
tor_free(_version);
tor_free(global_dirfrontpagecontents);
@ -5089,7 +5085,6 @@ or_state_set(or_state_t *new_state)
{
char *err = NULL;
tor_assert(new_state);
if (global_state)
config_free(&state_format, global_state);
global_state = new_state;
if (entry_guards_parse_state(global_state, 1, &err)<0) {

View file

@ -371,14 +371,10 @@ _connection_free(connection_t *conn)
if (connection_speaks_cells(conn)) {
or_connection_t *or_conn = TO_OR_CONN(conn);
if (or_conn->tls) {
tor_tls_free(or_conn->tls);
or_conn->tls = NULL;
}
if (or_conn->handshake_state) {
or_handshake_state_free(or_conn->handshake_state);
or_conn->handshake_state = NULL;
}
tor_free(or_conn->nickname);
}
if (CONN_IS_EDGE(conn)) {
@ -388,7 +384,7 @@ _connection_free(connection_t *conn)
memset(edge_conn->socks_request, 0xcc, sizeof(socks_request_t));
tor_free(edge_conn->socks_request);
}
if (edge_conn->rend_data)
rend_data_free(edge_conn->rend_data);
}
if (conn->type == CONN_TYPE_CONTROL) {
@ -402,15 +398,14 @@ _connection_free(connection_t *conn)
if (conn->type == CONN_TYPE_DIR) {
dir_connection_t *dir_conn = TO_DIR_CONN(conn);
tor_free(dir_conn->requested_resource);
if (dir_conn->zlib_state)
tor_zlib_free(dir_conn->zlib_state);
if (dir_conn->fingerprint_stack) {
SMARTLIST_FOREACH(dir_conn->fingerprint_stack, char *, cp, tor_free(cp));
smartlist_free(dir_conn->fingerprint_stack);
}
if (dir_conn->cached_dir)
cached_dir_decref(dir_conn->cached_dir);
if (dir_conn->rend_data)
rend_data_free(dir_conn->rend_data);
}

View file

@ -795,15 +795,12 @@ addressmap_clean(time_t now)
void
addressmap_free_all(void)
{
if (addressmap) {
strmap_free(addressmap, addressmap_ent_free);
addressmap = NULL;
}
if (virtaddress_reversemap) {
strmap_free(virtaddress_reversemap, addressmap_virtaddress_ent_free);
virtaddress_reversemap = NULL;
}
}
/** Look at address, and rewrite it until it doesn't want any
* more rewrites; but don't get into an infinite loop.

View file

@ -80,11 +80,10 @@ connection_or_clear_identity_map(void)
}
});
if (orconn_identity_map) {
digestmap_free(orconn_identity_map, NULL);
orconn_identity_map = NULL;
}
}
/** Change conn->identity_digest to digest, and add conn into
* orconn_digest_map. */
@ -1118,10 +1117,10 @@ connection_or_set_state_open(or_connection_t *conn)
}
}
}
if (conn->handshake_state) {
or_handshake_state_free(conn->handshake_state);
conn->handshake_state = NULL;
}
connection_start_reading(TO_CONN(conn));
circuit_n_conn_done(conn, 1); /* send the pending creates, if any. */

View file

@ -2143,7 +2143,6 @@ handle_control_extendcircuit(control_connection_t *conn, uint32_t len,
done:
SMARTLIST_FOREACH(router_nicknames, char *, n, tor_free(n));
smartlist_free(router_nicknames);
if (routers)
smartlist_free(routers);
return 0;
}

View file

@ -2818,9 +2818,7 @@ generate_v2_networkstatus_opinion(void)
tor_free(status);
tor_free(hostname);
tor_free(identity_pkey);
if (routers)
smartlist_free(routers);
if (omit_as_sybil)
digestmap_free(omit_as_sybil, NULL);
return r;
}
@ -3497,7 +3495,6 @@ connection_dirserv_add_networkstatus_bytes_to_outbuf(dir_connection_t *conn)
}
} else {
connection_dirserv_finish_spooling(conn);
if (conn->fingerprint_stack)
smartlist_free(conn->fingerprint_stack);
conn->fingerprint_stack = NULL;
return 0;
@ -3545,13 +3542,10 @@ dirserv_free_all(void)
cached_dir_decref(the_v2_networkstatus);
cached_dir_decref(cached_directory);
clear_cached_dir(&cached_runningrouters);
if (cached_v2_networkstatus) {
digestmap_free(cached_v2_networkstatus, _free_cached_dir);
cached_v2_networkstatus = NULL;
}
if (cached_consensuses) {
strmap_free(cached_consensuses, _free_cached_dir);
cached_consensuses = NULL;
}
}

View file

@ -2062,12 +2062,11 @@ dirvote_clear_pending_consensuses(void)
for (i = 0; i < N_CONSENSUS_FLAVORS; ++i) {
pending_consensus_t *pc = &pending_consensuses[i];
tor_free(pc->body);
if (pc->consensus) {
networkstatus_vote_free(pc->consensus);
pc->consensus = NULL;
}
}
}
/** Drop all currently pending votes, consensus, and detached signatures. */
static void
@ -2267,7 +2266,6 @@ dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
*status_out = 400;
discard:
if (vote)
networkstatus_vote_free(vote);
if (end_of_vote && !strcmpstart(end_of_vote, "network-status-version ")) {
@ -2453,7 +2451,6 @@ dirvote_compute_consensuses(void)
return 0;
err:
if (votes)
smartlist_free(votes);
tor_free(consensus_body);
tor_free(signatures);
@ -2582,7 +2579,6 @@ dirvote_add_signatures_to_all_pending_consensuses(
if (!*msg_out)
*msg_out = "Unrecognized error while adding detached signatures.";
done:
if (sigs)
ns_detached_signatures_free(sigs);
/* XXXX NM Check how return is used. We can now have an error *and*
signatures added. */

View file

@ -366,7 +366,6 @@ dns_free_all(void)
_free_cached_resolve(item);
}
HT_CLEAR(cache_map, &cache_root);
if (cached_resolve_pqueue)
smartlist_free(cached_resolve_pqueue);
cached_resolve_pqueue = NULL;
tor_free(resolv_conf_fname);
@ -1644,10 +1643,9 @@ dns_seems_to_be_broken(void)
void
dns_reset_correctness_checks(void)
{
if (dns_wildcard_response_count) {
strmap_free(dns_wildcard_response_count, _tor_free);
dns_wildcard_response_count = NULL;
}
n_wildcard_requests = 0;
if (dns_wildcard_list) {

View file

@ -1162,7 +1162,7 @@ clear_geoip_db(void)
SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, tor_free(c));
smartlist_free(geoip_countries);
}
if (country_idxplus1_by_lc_code)
strmap_free(country_idxplus1_by_lc_code, NULL);
if (geoip_entries) {
SMARTLIST_FOREACH(geoip_entries, geoip_entry_t *, ent, tor_free(ent));

View file

@ -1997,11 +1997,9 @@ tor_free_all(int postfork)
tor_tls_free_all();
}
/* stuff in main.c */
if (connection_array)
smartlist_free(connection_array);
if (closeable_connection_lst)
smartlist_free(closeable_connection_lst);
if (active_linked_connection_lst)
smartlist_free(active_linked_connection_lst);
tor_free(timeout_event);
if (!postfork) {

View file

@ -337,6 +337,8 @@ microdesc_cache_rebuild(microdesc_cache_t *cache)
void
microdesc_free(microdesc_t *md)
{
if (!md)
return;
/* Must be removed from hash table! */
if (md->onion_pkey)
crypto_free_pk_env(md->onion_pkey);

View file

@ -361,7 +361,6 @@ networkstatus_vote_free(networkstatus_t *ns)
} SMARTLIST_FOREACH_END(voter);
smartlist_free(ns->voters);
}
if (ns->cert)
authority_cert_free(ns->cert);
if (ns->routerstatus_list) {
@ -375,7 +374,7 @@ networkstatus_vote_free(networkstatus_t *ns)
smartlist_free(ns->routerstatus_list);
}
if (ns->desc_digest_map)
digestmap_free(ns->desc_digest_map, NULL);
memset(ns, 11, sizeof(*ns));
@ -1592,7 +1591,6 @@ networkstatus_set_current_consensus(const char *consensus,
if (!current_valid_after ||
c->valid_after > current_valid_after) {
waiting = &consensus_waiting_for_certs[flav];
if (waiting->consensus)
networkstatus_vote_free(waiting->consensus);
tor_free(waiting->body);
waiting->consensus = c;
@ -1705,7 +1703,6 @@ networkstatus_set_current_consensus(const char *consensus,
result = 0;
done:
if (c)
networkstatus_vote_free(c);
tor_free(consensus_fname);
tor_free(unverified_fname);
@ -1838,10 +1835,8 @@ routerstatus_list_update_named_server_map(void)
if (!current_consensus)
return;
if (named_server_map)
strmap_free(named_server_map, _tor_free);
named_server_map = strmap_new();
if (unnamed_server_map)
strmap_free(unnamed_server_map, NULL);
unnamed_server_map = strmap_new();
SMARTLIST_FOREACH(current_consensus->routerstatus_list, routerstatus_t *, rs,
@ -2159,14 +2154,12 @@ networkstatus_free_all(void)
smartlist_free(networkstatus_v2_list);
networkstatus_v2_list = NULL;
}
if (v2_download_status_map) {
digestmap_free(v2_download_status_map, _tor_free);
v2_download_status_map = NULL;
}
if (current_consensus) {
networkstatus_vote_free(current_consensus);
current_consensus = NULL;
}
for (i=0; i < N_CONSENSUS_FLAVORS; ++i) {
consensus_waiting_for_certs_t *waiting = &consensus_waiting_for_certs[i];
if (waiting->consensus) {
@ -2175,11 +2168,8 @@ networkstatus_free_all(void)
}
tor_free(waiting->body);
}
if (named_server_map) {
strmap_free(named_server_map, _tor_free);
}
if (unnamed_server_map) {
strmap_free(unnamed_server_map, NULL);
}
}

View file

@ -876,7 +876,6 @@ rend_parse_service_authorization(or_options_t *options, int validate_only)
err:
res = -1;
done:
if (auth)
rend_service_authorization_free(auth);
SMARTLIST_FOREACH(sl, char *, c, tor_free(c););
smartlist_free(sl);

View file

@ -405,7 +405,6 @@ rend_desc_v2_is_parsable(rend_encoded_v2_service_descriptor_t *desc)
&test_intro_size,
&test_encoded_size,
&test_next, desc->desc_str);
if (test_parsed)
rend_service_descriptor_free(test_parsed);
tor_free(test_intro_content);
return (res >= 0);
@ -428,9 +427,8 @@ 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)
crypto_free_pk_env(intro->intro_key);
tor_free(intro);
}
@ -797,9 +795,7 @@ _rend_cache_entry_free(void *p)
void
rend_cache_free_all(void)
{
if (rend_cache)
strmap_free(rend_cache, _rend_cache_entry_free);
if (rend_cache_v2_dir)
digestmap_free(rend_cache_v2_dir, _rend_cache_entry_free);
rend_cache = NULL;
rend_cache_v2_dir = NULL;
@ -1348,7 +1344,6 @@ rend_cache_store_v2_desc_as_client(const char *desc,
return 1;
err:
if (parsed)
rend_service_descriptor_free(parsed);
tor_free(intro_content);
return retval;

View file

@ -120,14 +120,13 @@ rend_service_free(rend_service_t *service)
rend_intro_point_free(intro););
smartlist_free(service->intro_nodes);
}
if (service->desc)
rend_service_descriptor_free(service->desc);
if (service->clients) {
SMARTLIST_FOREACH(service->clients, rend_authorized_client_t *, c,
rend_authorized_client_free(c););
smartlist_free(service->clients);
}
if (service->accepted_intros)
digestmap_free(service->accepted_intros, _tor_free);
tor_free(service);
}
@ -485,10 +484,10 @@ rend_service_update_descriptor(rend_service_t *service)
rend_service_descriptor_t *d;
origin_circuit_t *circ;
int i;
if (service->desc) {
rend_service_descriptor_free(service->desc);
service->desc = NULL;
}
d = service->desc = tor_malloc_zero(sizeof(rend_service_descriptor_t));
d->pk = crypto_pk_dup_key(service->private_key);
d->timestamp = time(NULL);

View file

@ -61,7 +61,6 @@ static void
set_onion_key(crypto_pk_env_t *k)
{
tor_mutex_acquire(key_lock);
if (onionkey)
crypto_free_pk_env(onionkey);
onionkey = k;
onionkey_set_at = time(NULL);
@ -111,7 +110,6 @@ get_onion_key_set_at(void)
void
set_identity_key(crypto_pk_env_t *k)
{
if (identitykey)
crypto_free_pk_env(identitykey);
identitykey = k;
crypto_pk_get_digest(identitykey, identitykey_digest);
@ -201,7 +199,6 @@ rotate_onion_key(void)
}
log_info(LD_GENERAL, "Rotating onion key");
tor_mutex_acquire(key_lock);
if (lastonionkey)
crypto_free_pk_env(lastonionkey);
lastonionkey = onionkey;
onionkey = prkey;
@ -331,10 +328,9 @@ load_authority_keyset(int legacy, crypto_pk_env_t **key_out,
goto done;
}
if (*key_out)
crypto_free_pk_env(*key_out);
if (*cert_out)
authority_cert_free(*cert_out);
*key_out = signing_key;
*cert_out = parsed;
r = 0;
@ -344,9 +340,7 @@ load_authority_keyset(int legacy, crypto_pk_env_t **key_out,
done:
tor_free(fname);
tor_free(cert);
if (signing_key)
crypto_free_pk_env(signing_key);
if (parsed)
authority_cert_free(parsed);
return r;
}
@ -1425,10 +1419,8 @@ router_rebuild_descriptor(int force)
tor_assert(! routerinfo_incompatible_with_extrainfo(ri, ei, NULL, NULL));
if (desc_routerinfo)
routerinfo_free(desc_routerinfo);
desc_routerinfo = ri;
if (desc_extrainfo)
extrainfo_free(desc_extrainfo);
desc_extrainfo = ei;
@ -2169,25 +2161,15 @@ router_purpose_from_string(const char *s)
void
router_free_all(void)
{
if (onionkey)
crypto_free_pk_env(onionkey);
if (lastonionkey)
crypto_free_pk_env(lastonionkey);
if (identitykey)
crypto_free_pk_env(identitykey);
if (key_lock)
tor_mutex_free(key_lock);
if (desc_routerinfo)
routerinfo_free(desc_routerinfo);
if (desc_extrainfo)
extrainfo_free(desc_extrainfo);
if (authority_signing_key)
crypto_free_pk_env(authority_signing_key);
if (authority_key_certificate)
authority_cert_free(authority_key_certificate);
if (legacy_signing_key)
crypto_free_pk_env(legacy_signing_key);
if (legacy_key_certificate)
authority_cert_free(legacy_key_certificate);
if (warned_nonexistent_family) {

View file

@ -757,7 +757,6 @@ router_rebuild_store(int flags, desc_store_t *store)
store->journal_len = 0;
store->bytes_dropped = 0;
done:
if (signed_descriptors)
smartlist_free(signed_descriptors);
tor_free(fname);
tor_free(fname_tmp);
@ -2861,7 +2860,6 @@ routerlist_reparse_old(routerlist_t *rl, signed_descriptor_t *sd)
void
routerlist_free_all(void)
{
if (routerlist)
routerlist_free(routerlist);
routerlist = NULL;
if (warned_nicknames) {
@ -3771,9 +3769,7 @@ authority_cert_free(authority_cert_t *cert)
return;
tor_free(cert->cache_info.signed_descriptor_body);
if (cert->signing_key)
crypto_free_pk_env(cert->signing_key);
if (cert->identity_key)
crypto_free_pk_env(cert->identity_key);
tor_free(cert);
@ -4829,8 +4825,8 @@ esc_router_info(routerinfo_t *router)
static char *info=NULL;
char *esc_contact, *esc_platform;
size_t len;
if (info)
tor_free(info);
if (!router)
return NULL; /* we're exiting; just free the memory we use */
@ -4965,9 +4961,8 @@ void
routerset_refresh_countries(routerset_t *target)
{
int cc;
if (target->countries) {
bitarray_free(target->countries);
}
if (!geoip_is_loaded()) {
target->countries = NULL;
target->n_countries = 0;
@ -5325,7 +5320,6 @@ routerset_free(routerset_t *routerset)
strmap_free(routerset->names, NULL);
digestmap_free(routerset->digests, NULL);
if (routerset->countries)
bitarray_free(routerset->countries);
tor_free(routerset);
}

View file

@ -1554,9 +1554,7 @@ router_parse_entry_from_string(const char *s, const char *end,
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_free(tokens);
}
if (exit_policy_tokens) {
smartlist_free(exit_policy_tokens);
}
if (area) {
DUMP_AREA(area, "routerinfo");
memarea_drop_all(area);
@ -1672,7 +1670,6 @@ extrainfo_parse_entry_from_string(const char *s, const char *end,
goto done;
err:
dump_desc(s_dup, "extra-info descriptor");
if (extrainfo)
extrainfo_free(extrainfo);
extrainfo = NULL;
done:
@ -2316,7 +2313,6 @@ networkstatus_v2_parse_from_string(const char *s)
goto done;
err:
dump_desc(s_dup, "v2 networkstatus");
if (ns)
networkstatus_v2_free(ns);
ns = NULL;
done:
@ -2794,7 +2790,6 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
goto done;
err:
dump_desc(s_dup, "v3 networkstatus");
if (ns)
networkstatus_vote_free(ns);
ns = NULL;
done:
@ -3859,7 +3854,6 @@ microdescs_parse_from_string(const char *s, const char *eos,
md = NULL;
next:
if (md)
microdesc_free(md);
memarea_clear(area);
@ -4264,7 +4258,6 @@ rend_parse_v2_service_descriptor(rend_service_descriptor_t **parsed_out,
}
goto done;
err:
if (result)
rend_service_descriptor_free(result);
result = NULL;
done: