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
......@@ -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
......
......@@ -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)
......
......@@ -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);
......
......@@ -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);
}
......
......@@ -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,
......
......@@ -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);
......
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