Commit 0c1b3070 authored by Nick Mathewson's avatar Nick Mathewson 🎨
Browse files

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

parent 79f72d0e
......@@ -165,9 +165,7 @@ tor_gzip_compress(char **out, size_t *out_len,
deflateEnd(stream);
tor_free(stream);
}
if (*out) {
tor_free(*out);
}
tor_free(*out);
return -1;
}
......
......@@ -1314,10 +1314,8 @@ 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);
tor_free(s1);
tor_free(s2);
}
/** Helper function: try to extract a link certificate and an identity
......
......@@ -953,8 +953,7 @@ const char *
escaped(const char *s)
{
static char *_escaped_val = NULL;
if (_escaped_val)
tor_free(_escaped_val);
tor_free(_escaped_val);
if (s)
_escaped_val = esc_for_log(s);
......
......@@ -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;
}
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,8 +2429,7 @@ 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);
extend_info_free(state->chosen_exit);
state->chosen_exit = extend_info_dup(exit);
++circ->build_state->desired_path_len;
......@@ -2746,8 +2744,7 @@ extend_info_free(extend_info_t *info)
{
if (!info)
return;
if (info->onion_key)
crypto_free_pk_env(info->onion_key);
crypto_free_pk_env(info->onion_key);
tor_free(info);
}
......
......@@ -451,18 +451,15 @@ 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);
crypto_free_pk_env(ocirc->intro_key);
rend_data_free(ocirc->rend_data);
} else {
or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
/* Remember cell statistics for this circuit before deallocating. */
......@@ -472,14 +469,10 @@ 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);
crypto_free_cipher_env(ocirc->p_crypto);
crypto_free_digest_env(ocirc->p_digest);
crypto_free_cipher_env(ocirc->n_crypto);
crypto_free_digest_env(ocirc->n_digest);
if (ocirc->rend_splice) {
or_circuit_t *other = ocirc->rend_splice;
......@@ -495,8 +488,7 @@ circuit_free(circuit_t *circ)
cell_queue_clear(&ocirc->p_conn_cells);
}
if (circ->n_hop)
extend_info_free(circ->n_hop);
extend_info_free(circ->n_hop);
tor_free(circ->n_conn_onionskin);
/* Remove from map. */
......@@ -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;
}
smartlist_free(circuits_pending_or_conns);
circuits_pending_or_conns = NULL;
HT_CLEAR(orconn_circid_map, &orconn_circid_circuit_map);
}
......@@ -563,18 +555,12 @@ 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);
crypto_free_cipher_env(victim->f_crypto);
crypto_free_cipher_env(victim->b_crypto);
crypto_free_digest_env(victim->f_digest);
crypto_free_digest_env(victim->b_digest);
crypto_dh_free(victim->dh_handshake_state);
extend_info_free(victim->extend_info);
memset(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
tor_free(victim);
......
......@@ -928,8 +928,8 @@ 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);
extend_info_free(info);
return circ;
}
......@@ -1222,8 +1222,7 @@ circuit_get_open_circ_or_launch(edge_connection_t *conn,
flags);
}
if (extend_info)
extend_info_free(extend_info);
extend_info_free(extend_info);
if (desired_circuit_purpose != CIRCUIT_PURPOSE_C_GENERAL) {
/* help predict this next time */
......
......@@ -827,8 +827,8 @@ 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);
config_free(&options_format, old_options);
return 0;
}
......@@ -862,8 +862,7 @@ or_options_free(or_options_t *options)
if (!options)
return;
if (options->_ExcludeExitNodesUnion)
routerset_free(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;
}
or_options_free(global_options);
global_options = NULL;
config_free(&state_format, global_state);
global_state = NULL;
config_free_lines(global_cmdline_options);
global_cmdline_options = NULL;
tor_free(torrc_fname);
tor_free(_version);
tor_free(global_dirfrontpagecontents);
......@@ -5089,8 +5085,7 @@ or_state_set(or_state_t *new_state)
{
char *err = NULL;
tor_assert(new_state);
if (global_state)
config_free(&state_format, global_state);
config_free(&state_format, global_state);
global_state = new_state;
if (entry_guards_parse_state(global_state, 1, &err)<0) {
log_warn(LD_GENERAL,"%s",err);
......
......@@ -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_tls_free(or_conn->tls);
or_conn->tls = NULL;
or_handshake_state_free(or_conn->handshake_state);
or_conn->handshake_state = NULL;
tor_free(or_conn->nickname);
}
if (CONN_IS_EDGE(conn)) {
......@@ -388,8 +384,8 @@ _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);
rend_data_free(edge_conn->rend_data);
}
if (conn->type == CONN_TYPE_CONTROL) {
control_connection_t *control_conn = TO_CONTROL_CONN(conn);
......@@ -402,16 +398,15 @@ _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);
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);
cached_dir_decref(dir_conn->cached_dir);
rend_data_free(dir_conn->rend_data);
}
if (conn->s >= 0) {
......
......@@ -795,14 +795,11 @@ 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;
}
strmap_free(addressmap, addressmap_ent_free);
addressmap = NULL;
strmap_free(virtaddress_reversemap, addressmap_virtaddress_ent_free);
virtaddress_reversemap = NULL;
}
/** Look at address, and rewrite it until it doesn't want any
......
......@@ -80,10 +80,9 @@ connection_or_clear_identity_map(void)
}
});
if (orconn_identity_map) {
digestmap_free(orconn_identity_map, NULL);
orconn_identity_map = NULL;
}
digestmap_free(orconn_identity_map, NULL);
orconn_identity_map = NULL;
}
/** Change conn->identity_digest to digest, and add conn into
......@@ -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;
}
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. */
......
......@@ -2143,8 +2143,7 @@ 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);
smartlist_free(routers);
return 0;
}
......
......@@ -2818,10 +2818,8 @@ 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);
smartlist_free(routers);
digestmap_free(omit_as_sybil, NULL);
return r;
}
......@@ -3497,8 +3495,7 @@ 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);
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;
}
digestmap_free(cached_v2_networkstatus, _free_cached_dir);
cached_v2_networkstatus = NULL;
strmap_free(cached_consensuses, _free_cached_dir);
cached_consensuses = NULL;
}
......@@ -2062,10 +2062,9 @@ 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;
}
networkstatus_vote_free(pc->consensus);
pc->consensus = NULL;
}
}
......@@ -2267,8 +2266,7 @@ dirvote_add_vote(const char *vote_body, const char **msg_out, int *status_out)
*status_out = 400;
discard:
if (vote)
networkstatus_vote_free(vote);
networkstatus_vote_free(vote);
if (end_of_vote && !strcmpstart(end_of_vote, "network-status-version ")) {
vote_body = end_of_vote;
......@@ -2453,8 +2451,7 @@ dirvote_compute_consensuses(void)
return 0;
err:
if (votes)
smartlist_free(votes);
smartlist_free(votes);
tor_free(consensus_body);
tor_free(signatures);
networkstatus_vote_free(consensus);
......@@ -2582,8 +2579,7 @@ 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);
ns_detached_signatures_free(sigs);
/* XXXX NM Check how return is used. We can now have an error *and*
signatures added. */
return r;
......
......@@ -366,8 +366,7 @@ dns_free_all(void)
_free_cached_resolve(item);
}
HT_CLEAR(cache_map, &cache_root);
if (cached_resolve_pqueue)
smartlist_free(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;
}
strmap_free(dns_wildcard_response_count, _tor_free);
dns_wildcard_response_count = NULL;
n_wildcard_requests = 0;
if (dns_wildcard_list) {
......
......@@ -1162,8 +1162,8 @@ 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);
strmap_free(country_idxplus1_by_lc_code, NULL);
if (geoip_entries) {
SMARTLIST_FOREACH(geoip_entries, geoip_entry_t *, ent, tor_free(ent));
smartlist_free(geoip_entries);
......
......@@ -1997,12 +1997,10 @@ 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);
smartlist_free(connection_array);
smartlist_free(closeable_connection_lst);
smartlist_free(active_linked_connection_lst);
tor_free(timeout_event);
if (!postfork) {
release_lockfile();
......
......@@ -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);
......
......@@ -361,8 +361,7 @@ networkstatus_vote_free(networkstatus_t *ns)
} SMARTLIST_FOREACH_END(voter);
smartlist_free(ns->voters);
}
if (ns->cert)
authority_cert_free(ns->cert);
authority_cert_free(ns->cert);
if (ns->routerstatus_list) {
if (ns->type == NS_TYPE_VOTE || ns->type == NS_TYPE_OPINION) {
......@@ -375,8 +374,8 @@ networkstatus_vote_free(networkstatus_t *ns)
smartlist_free(ns->routerstatus_list);
}
if (ns->desc_digest_map)
digestmap_free(ns->desc_digest_map, NULL);
digestmap_free(ns->desc_digest_map, NULL);
memset(ns, 11, sizeof(*ns));
tor_free(ns);
......@@ -1592,8 +1591,7 @@ 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);
networkstatus_vote_free(waiting->consensus);
tor_free(waiting->body);
waiting->consensus = c;
c = NULL; /* Prevent free. */
......@@ -1705,8 +1703,7 @@ networkstatus_set_current_consensus(const char *consensus,
result = 0;
done:
if (c)
networkstatus_vote_free(c);
networkstatus_vote_free(c);
tor_free(consensus_fname);
tor_free(unverified_fname);
return result;
......@@ -1838,11 +1835,9 @@ routerstatus_list_update_named_server_map(void)
if (!current_consensus)
return;
if (named_server_map)
strmap_free(named_server_map, _tor_free);
strmap_free(named_server_map, _tor_free);
named_server_map = strmap_new();
if (unnamed_server_map)
strmap_free(unnamed_server_map, NULL);
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;
}
digestmap_free(v2_download_status_map, _tor_free);
v2_download_status_map = NULL;
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);
}
strmap_free(named_server_map, _tor_free);
strmap_free(unnamed_server_map, NULL);
}
......@@ -876,8 +876,7 @@ rend_parse_service_authorization(or_options_t *options, int validate_only)
err:
res = -1;
done:
if (auth)
rend_service_authorization_free(auth);
rend_service_authorization_free(auth);
SMARTLIST_FOREACH(sl, char *, c, tor_free(c););
smartlist_free(sl);
if (!validate_only && res == 0) {
......
......@@ -405,8 +405,7 @@ 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);
rend_service_descriptor_free(test_parsed);
tor_free(test_intro_content);
return (res >= 0);
}
......@@ -428,10 +427,9 @@ rend_intro_point_free(rend_intro_point_t *intro)
{
if (!intro)
return;