diff options
Diffstat (limited to 'src/or')
96 files changed, 543 insertions, 284 deletions
diff --git a/src/or/addressmap.c b/src/or/addressmap.c index 9a2cc26b3b..96ce275578 100644 --- a/src/or/addressmap.c +++ b/src/or/addressmap.c @@ -90,34 +90,47 @@ addressmap_init(void) virtaddress_reversemap = strmap_new(); } +#define addressmap_ent_free(ent) \ + FREE_AND_NULL(addressmap_entry_t, addressmap_ent_free_, (ent)) + /** Free the memory associated with the addressmap entry <b>_ent</b>. */ static void -addressmap_ent_free(void *_ent) +addressmap_ent_free_(addressmap_entry_t *ent) { - addressmap_entry_t *ent; - if (!_ent) + if (!ent) return; - ent = _ent; tor_free(ent->new_address); tor_free(ent); } +static void +addressmap_ent_free_void(void *ent) +{ + addressmap_ent_free_(ent); +} + +#define addressmap_virtaddress_ent_free(ent) \ + FREE_AND_NULL(virtaddress_entry_t, addressmap_virtaddress_ent_free_, (ent)) + /** Free storage held by a virtaddress_entry_t* entry in <b>_ent</b>. */ static void -addressmap_virtaddress_ent_free(void *_ent) +addressmap_virtaddress_ent_free_(virtaddress_entry_t *ent) { - virtaddress_entry_t *ent; - if (!_ent) + if (!ent) return; - - ent = _ent; tor_free(ent->ipv4_address); tor_free(ent->ipv6_address); tor_free(ent->hostname_address); tor_free(ent); } +static void +addressmap_virtaddress_ent_free_void(void *ent) +{ + addressmap_virtaddress_ent_free_(ent); +} + /** Remove <b>address</b> (which must map to <b>ent</b>) from the * virtual address map. */ static void @@ -311,10 +324,10 @@ addressmap_clean(time_t now) void addressmap_free_all(void) { - strmap_free(addressmap, addressmap_ent_free); + strmap_free(addressmap, addressmap_ent_free_void); addressmap = NULL; - strmap_free(virtaddress_reversemap, addressmap_virtaddress_ent_free); + strmap_free(virtaddress_reversemap, addressmap_virtaddress_ent_free_void); virtaddress_reversemap = NULL; } diff --git a/src/or/bridges.c b/src/or/bridges.c index 320f5ee632..f6e3e419d3 100644 --- a/src/or/bridges.c +++ b/src/or/bridges.c @@ -53,7 +53,10 @@ struct bridge_info_t { smartlist_t *socks_args; }; -static void bridge_free(bridge_info_t *bridge); +#define bridge_free(bridge) \ + FREE_AND_NULL(bridge_info_t, bridge_free_, (bridge)) + +static void bridge_free_(bridge_info_t *bridge); static void rewrite_node_address_for_bridge(const bridge_info_t *bridge, node_t *node); @@ -101,7 +104,7 @@ clear_bridge_list(void) /** Free the bridge <b>bridge</b>. */ static void -bridge_free(bridge_info_t *bridge) +bridge_free_(bridge_info_t *bridge) { if (!bridge) return; diff --git a/src/or/channel.c b/src/or/channel.c index 5b9d860baf..7fa9768171 100644 --- a/src/or/channel.c +++ b/src/or/channel.c @@ -1,3 +1,4 @@ + /* * Copyright (c) 2012-2017, The Tor Project, Inc. */ /* See LICENSE for licensing information */ @@ -162,12 +163,12 @@ HT_GENERATE2(channel_idmap, channel_idmap_entry_s, node, channel_idmap_hash, /* Functions to maintain the digest map */ static void channel_remove_from_digest_map(channel_t *chan); -static void channel_force_free(channel_t *chan); -static void -channel_free_list(smartlist_t *channels, int mark_for_close); -static void -channel_listener_free_list(smartlist_t *channels, int mark_for_close); -static void channel_listener_force_free(channel_listener_t *chan_l); +static void channel_force_xfree(channel_t *chan); +static void channel_free_list(smartlist_t *channels, + int mark_for_close); +static void channel_listener_free_list(smartlist_t *channels, + int mark_for_close); +static void channel_listener_force_xfree(channel_listener_t *chan_l); /*********************************** * Channel state utility functions * @@ -881,7 +882,7 @@ channel_init_listener(channel_listener_t *chan_l) */ void -channel_free(channel_t *chan) +channel_free_(channel_t *chan) { if (!chan) return; @@ -934,7 +935,7 @@ channel_free(channel_t *chan) */ void -channel_listener_free(channel_listener_t *chan_l) +channel_listener_free_(channel_listener_t *chan_l) { if (!chan_l) return; @@ -962,7 +963,7 @@ channel_listener_free(channel_listener_t *chan_l) */ static void -channel_force_free(channel_t *chan) +channel_force_xfree(channel_t *chan) { tor_assert(chan); @@ -1007,7 +1008,7 @@ channel_force_free(channel_t *chan) */ static void -channel_listener_force_free(channel_listener_t *chan_l) +channel_listener_force_xfree(channel_listener_t *chan_l) { tor_assert(chan_l); @@ -1431,7 +1432,6 @@ channel_clear_remote_end(channel_t *chan) /** * Write to a channel the given packed cell. * - * Return 0 on success or -1 on error. * * Two possible errors can happen. Either the channel is not opened or the * lower layer (specialized channel) failed to write it. In both cases, it is @@ -2239,7 +2239,7 @@ channel_free_list(smartlist_t *channels, int mark_for_close) if (!CHANNEL_CONDEMNED(curr)) { channel_mark_for_close(curr); } - channel_force_free(curr); + channel_force_xfree(curr); } else channel_free(curr); } SMARTLIST_FOREACH_END(curr); } @@ -2268,7 +2268,7 @@ channel_listener_free_list(smartlist_t *listeners, int mark_for_close) curr->state == CHANNEL_LISTENER_STATE_ERROR)) { channel_listener_mark_for_close(curr); } - channel_listener_force_free(curr); + channel_listener_force_xfree(curr); } else channel_listener_free(curr); } SMARTLIST_FOREACH_END(curr); } diff --git a/src/or/channel.h b/src/or/channel.h index d88a77c9ae..0f685011a3 100644 --- a/src/or/channel.h +++ b/src/or/channel.h @@ -458,8 +458,11 @@ void channel_close_for_error(channel_t *chan); void channel_closed(channel_t *chan); /* Free a channel */ -void channel_free(channel_t *chan); -void channel_listener_free(channel_listener_t *chan_l); +void channel_free_(channel_t *chan); +#define channel_free(chan) FREE_AND_NULL(channel_t, channel_free_, (chan)) +void channel_listener_free_(channel_listener_t *chan_l); +#define channel_listener_free(chan_l) \ + FREE_AND_NULL(channel_listener_t, channel_listener_free_, (chan_l)) /* State/metadata setters */ @@ -634,6 +637,8 @@ int packed_cell_is_destroy(channel_t *chan, /* Declare the handle helpers */ HANDLE_DECL(channel, channel_s,) +#define channel_handle_free(h) \ + FREE_AND_NULL(channel_handle_t, channel_handle_free_, (h)) #endif /* !defined(TOR_CHANNEL_H) */ diff --git a/src/or/circuitbuild.c b/src/or/circuitbuild.c index 4e9d2457c4..8cbf5e3cbb 100644 --- a/src/or/circuitbuild.c +++ b/src/or/circuitbuild.c @@ -2732,7 +2732,7 @@ extend_info_from_node(const node_t *node, int for_direct_connect) /** Release storage held by an extend_info_t struct. */ void -extend_info_free(extend_info_t *info) +extend_info_free_(extend_info_t *info) { if (!info) return; diff --git a/src/or/circuitbuild.h b/src/or/circuitbuild.h index b8a651e055..9b5fcc71ed 100644 --- a/src/or/circuitbuild.h +++ b/src/or/circuitbuild.h @@ -58,7 +58,9 @@ extend_info_t *extend_info_new(const char *nickname, const tor_addr_t *addr, uint16_t port); extend_info_t *extend_info_from_node(const node_t *r, int for_direct_connect); extend_info_t *extend_info_dup(extend_info_t *info); -void extend_info_free(extend_info_t *info); +void extend_info_free_(extend_info_t *info); +#define extend_info_free(info) \ + FREE_AND_NULL(extend_info_t, extend_info_free_, (info)) int extend_info_addr_is_allowed(const tor_addr_t *addr); int extend_info_supports_tap(const extend_info_t* ei); int extend_info_supports_ntor(const extend_info_t* ei); diff --git a/src/or/circuitlist.c b/src/or/circuitlist.c index 6157bf68b7..9fac17a2bd 100644 --- a/src/or/circuitlist.c +++ b/src/or/circuitlist.c @@ -923,7 +923,7 @@ circuit_clear_testing_cell_stats(circuit_t *circ) /** Deallocate space associated with circ. */ STATIC void -circuit_free(circuit_t *circ) +circuit_free_(circuit_t *circ) { circid_t n_circ_id = 0; void *mem; @@ -1086,7 +1086,7 @@ circuit_free_all(void) while (or_circ->resolving_streams) { edge_connection_t *next_conn; next_conn = or_circ->resolving_streams->next_stream; - connection_free(TO_CONN(or_circ->resolving_streams)); + connection_free_(TO_CONN(or_circ->resolving_streams)); or_circ->resolving_streams = next_conn; } } diff --git a/src/or/circuitlist.h b/src/or/circuitlist.h index 5d0da15ca8..bf3393a941 100644 --- a/src/or/circuitlist.h +++ b/src/or/circuitlist.h @@ -81,7 +81,8 @@ MOCK_DECL(void, channel_note_destroy_not_pending, smartlist_t *circuit_find_circuits_to_upgrade_from_guard_wait(void); #ifdef CIRCUITLIST_PRIVATE -STATIC void circuit_free(circuit_t *circ); +STATIC void circuit_free_(circuit_t *circ); +#define circuit_free(circ) FREE_AND_NULL(circuit_t, circuit_free_, (circ)) STATIC size_t n_cells_in_circ_queues(const circuit_t *c); STATIC uint32_t circuit_max_queued_data_age(const circuit_t *c, uint32_t now); STATIC uint32_t circuit_max_queued_cell_age(const circuit_t *c, uint32_t now); diff --git a/src/or/circuitmux.c b/src/or/circuitmux.c index f3b8aecb1b..d8408f45fc 100644 --- a/src/or/circuitmux.c +++ b/src/or/circuitmux.c @@ -546,7 +546,7 @@ circuitmux_mark_destroyed_circids_usable(circuitmux_t *cmux, channel_t *chan) */ void -circuitmux_free(circuitmux_t *cmux) +circuitmux_free_(circuitmux_t *cmux) { if (!cmux) return; diff --git a/src/or/circuitmux.h b/src/or/circuitmux.h index 3e7496ea1c..255822946d 100644 --- a/src/or/circuitmux.h +++ b/src/or/circuitmux.h @@ -104,7 +104,9 @@ void circuitmux_assert_okay(circuitmux_t *cmux); circuitmux_t * circuitmux_alloc(void); void circuitmux_detach_all_circuits(circuitmux_t *cmux, smartlist_t *detached_out); -void circuitmux_free(circuitmux_t *cmux); +void circuitmux_free_(circuitmux_t *cmux); +#define circuitmux_free(cmux) \ + FREE_AND_NULL(circuitmux_t, circuitmux_free_, (cmux)) /* Policy control */ void circuitmux_clear_policy(circuitmux_t *cmux); diff --git a/src/or/config.c b/src/or/config.c index d589876785..bcfac1dae2 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -782,7 +782,7 @@ static or_options_t *global_default_options = NULL; /** Name of most recently read torrc file. */ static char *torrc_fname = NULL; /** Name of the most recently read torrc-defaults file.*/ -static char *torrc_defaults_fname; +static char *torrc_defaults_fname = NULL; /** Configuration options set by command line. */ static config_line_t *global_cmdline_options = NULL; /** Non-configuration options set by the command line */ @@ -926,7 +926,7 @@ get_short_version(void) /** Release additional memory allocated in options */ STATIC void -or_options_free(or_options_t *options) +or_options_free_(or_options_t *options) { if (!options) return; @@ -984,6 +984,8 @@ config_free_all(void) tor_free(the_short_tor_version); tor_free(the_tor_version); + + have_parsed_cmdline = 0; } /** Make <b>address</b> -- a piece of information related to our operation as @@ -5825,7 +5827,7 @@ validate_transport_socks_arguments(const smartlist_t *args) /** Deallocate a bridge_line_t structure. */ /* private */ void -bridge_line_free(bridge_line_t *bridge_line) +bridge_line_free_(bridge_line_t *bridge_line) { if (!bridge_line) return; @@ -6591,7 +6593,7 @@ port_cfg_new(size_t namelen) /** Free all storage held in <b>port</b> */ STATIC void -port_cfg_free(port_cfg_t *port) +port_cfg_free_(port_cfg_t *port) { tor_free(port); } diff --git a/src/or/config.h b/src/or/config.h index 1e4239b6d7..7c7ef1825a 100644 --- a/src/or/config.h +++ b/src/or/config.h @@ -198,7 +198,9 @@ typedef struct bridge_line_t { transport proxy. */ } bridge_line_t; -void bridge_line_free(bridge_line_t *bridge_line); +void bridge_line_free_(bridge_line_t *bridge_line); +#define bridge_line_free(line) \ + FREE_AND_NULL(bridge_line_t, bridge_line_free_, (line)) bridge_line_t *parse_bridge_line(const char *line); smartlist_t *get_options_from_transport_options_line(const char *line, const char *transport); @@ -221,8 +223,12 @@ extern struct config_format_t options_format; #endif STATIC port_cfg_t *port_cfg_new(size_t namelen); -STATIC void port_cfg_free(port_cfg_t *port); -STATIC void or_options_free(or_options_t *options); +#define port_cfg_free(port) \ + FREE_AND_NULL(port_cfg_t, port_cfg_free_, (port)) +STATIC void port_cfg_free_(port_cfg_t *port); +#define or_options_free(opt) \ + FREE_AND_NULL(or_options_t, or_options_free_, (opt)) +STATIC void or_options_free_(or_options_t *options); STATIC int options_validate_single_onion(or_options_t *options, char **msg); STATIC int options_validate(or_options_t *old_options, diff --git a/src/or/confparse.c b/src/or/confparse.c index abae7e33dc..64ed9ee6bb 100644 --- a/src/or/confparse.c +++ b/src/or/confparse.c @@ -863,7 +863,7 @@ config_reset(const config_format_t *fmt, void *options, /** Release storage held by <b>options</b>. */ void -config_free(const config_format_t *fmt, void *options) +config_free_(const config_format_t *fmt, void *options) { int i; diff --git a/src/or/confparse.h b/src/or/confparse.h index 022886e811..fc4a7b2d06 100644 --- a/src/or/confparse.h +++ b/src/or/confparse.h @@ -177,7 +177,12 @@ typedef struct config_format_t { #define CAL_WARN_DEPRECATIONS (1u<<2) void *config_new(const config_format_t *fmt); -void config_free(const config_format_t *fmt, void *options); +void config_free_(const config_format_t *fmt, void *options); +#define config_free(fmt, options) do { \ + config_free_((fmt), (options)); \ + (options) = NULL; \ + } while (0) + config_line_t *config_get_assigned_option(const config_format_t *fmt, const void *options, const char *key, int escape_val); diff --git a/src/or/connection.c b/src/or/connection.c index 92ccd3d5c8..3c37c59b40 100644 --- a/src/or/connection.c +++ b/src/or/connection.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2001 Matej Pfajfar. + /* Copyright (c) 2001 Matej Pfajfar. * Copyright (c) 2001-2004, Roger Dingledine. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. * Copyright (c) 2007-2017, The Tor Project, Inc. */ @@ -499,7 +499,7 @@ conn_listener_type_supports_af_unix(int type) * if <b>conn</b> is an OR or OP connection. */ STATIC void -connection_free_(connection_t *conn) +connection_free_minimal(connection_t *conn) { void *mem; size_t memlen; @@ -675,7 +675,7 @@ connection_free_(connection_t *conn) /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it. */ MOCK_IMPL(void, -connection_free,(connection_t *conn)) +connection_free_,(connection_t *conn)) { if (!conn) return; @@ -704,7 +704,7 @@ connection_free,(connection_t *conn)) } #endif /* 1 */ connection_unregister_events(conn); - connection_free_(conn); + connection_free_minimal(conn); } /** @@ -5239,8 +5239,8 @@ proxy_type_to_string(int proxy_type) return NULL; /*Unreached*/ } -/** Call connection_free_() on every connection in our array, and release all - * storage held by connection.c. +/** Call connection_free_minimal() on every connection in our array, and + * release all storage held by connection.c. * * Don't do the checks in connection_free(), because they will * fail. @@ -5264,7 +5264,8 @@ connection_free_all(void) /* Clear out our list of broken connections */ clear_broken_connection_map(0); - SMARTLIST_FOREACH(conns, connection_t *, conn, connection_free_(conn)); + SMARTLIST_FOREACH(conns, connection_t *, conn, + connection_free_minimal(conn)); if (outgoing_addrs) { SMARTLIST_FOREACH(outgoing_addrs, tor_addr_t *, addr, tor_free(addr)); diff --git a/src/or/connection.h b/src/or/connection.h index 1d41a3c4f5..6bc5a7cfd0 100644 --- a/src/or/connection.h +++ b/src/or/connection.h @@ -29,7 +29,9 @@ connection_t *connection_new(int type, int socket_family); int connection_init_accepted_conn(connection_t *conn, const listener_connection_t *listener); void connection_link_connections(connection_t *conn_a, connection_t *conn_b); -MOCK_DECL(void,connection_free,(connection_t *conn)); +MOCK_DECL(void,connection_free_,(connection_t *conn)); +#define connection_free(conn) \ + FREE_AND_NULL(connection_t, connection_free_, (conn)) void connection_free_all(void); void connection_about_to_close_connection(connection_t *conn); void connection_close_immediate(connection_t *conn); @@ -267,7 +269,7 @@ connection_is_moribund(connection_t *conn) void connection_check_oos(int n_socks, int failed); #ifdef CONNECTION_PRIVATE -STATIC void connection_free_(connection_t *conn); +STATIC void connection_free_minimal(connection_t *conn); /* Used only by connection.c and test*.c */ uint32_t bucket_millis_empty(int tokens_before, uint32_t last_empty_time, diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c index 113568bb71..0c4352ea13 100644 --- a/src/or/connection_edge.c +++ b/src/or/connection_edge.c @@ -3331,7 +3331,7 @@ handle_hs_exit_conn(circuit_t *circ, edge_connection_t *conn) relay_send_end_cell_from_edge(conn->stream_id, circ, END_STREAM_REASON_DONE, origin_circ->cpath->prev); - connection_free(TO_CONN(conn)); + connection_free_(TO_CONN(conn)); /* Drop the circuit here since it might be someone deliberately * scanning the hidden service ports. Note that this mitigates port @@ -3523,7 +3523,7 @@ connection_exit_begin_conn(cell_t *cell, circuit_t *circ) if (we_are_hibernating()) { relay_send_end_cell_from_edge(rh.stream_id, circ, END_STREAM_REASON_HIBERNATING, NULL); - connection_free(TO_CONN(n_stream)); + connection_free_(TO_CONN(n_stream)); return 0; } @@ -3601,7 +3601,7 @@ connection_exit_begin_resolve(cell_t *cell, or_circuit_t *circ) return 0; case 1: /* The result was cached; a resolved cell was sent. */ if (!dummy_conn->base_.marked_for_close) - connection_free(TO_CONN(dummy_conn)); + connection_free_(TO_CONN(dummy_conn)); return 0; case 0: /* resolve added to pending list */ assert_circuit_ok(TO_CIRCUIT(circ)); @@ -3774,8 +3774,8 @@ connection_exit_connect_dir(edge_connection_t *exitconn) if (connection_add(TO_CONN(exitconn))<0) { connection_edge_end(exitconn, END_STREAM_REASON_RESOURCELIMIT); - connection_free(TO_CONN(exitconn)); - connection_free(TO_CONN(dirconn)); + connection_free_(TO_CONN(exitconn)); + connection_free_(TO_CONN(dirconn)); return 0; } @@ -3787,7 +3787,7 @@ connection_exit_connect_dir(edge_connection_t *exitconn) connection_edge_end(exitconn, END_STREAM_REASON_RESOURCELIMIT); connection_close_immediate(TO_CONN(exitconn)); connection_mark_for_close(TO_CONN(exitconn)); - connection_free(TO_CONN(dirconn)); + connection_free_(TO_CONN(dirconn)); return 0; } diff --git a/src/or/connection_or.c b/src/or/connection_or.c index c680c5b218..a877761491 100644 --- a/src/or/connection_or.c +++ b/src/or/connection_or.c @@ -505,7 +505,7 @@ var_cell_copy(const var_cell_t *src) /** Release all space held by <b>cell</b>. */ void -var_cell_free(var_cell_t *cell) +var_cell_free_(var_cell_t *cell) { tor_free(cell); } @@ -1263,7 +1263,7 @@ connection_or_connect, (const tor_addr_t *_addr, uint16_t port, fmt_addrport(&TO_CONN(conn)->addr, TO_CONN(conn)->port)); } - connection_free(TO_CONN(conn)); + connection_free_(TO_CONN(conn)); return NULL; } @@ -1276,7 +1276,7 @@ connection_or_connect, (const tor_addr_t *_addr, uint16_t port, connection_or_connect_failed(conn, errno_to_orconn_end_reason(socket_error), tor_socket_strerror(socket_error)); - connection_free(TO_CONN(conn)); + connection_free_(TO_CONN(conn)); return NULL; case 0: connection_watch_events(TO_CONN(conn), READ_EVENT | WRITE_EVENT); @@ -1870,7 +1870,7 @@ connection_init_or_handshake_state(or_connection_t *conn, int started_here) /** Free all storage held by <b>state</b>. */ void -or_handshake_state_free(or_handshake_state_t *state) +or_handshake_state_free_(or_handshake_state_t *state) { if (!state) return; diff --git a/src/or/connection_or.h b/src/or/connection_or.h index 644df5c2c9..7c1dced631 100644 --- a/src/or/connection_or.h +++ b/src/or/connection_or.h @@ -68,7 +68,9 @@ int connection_or_client_learned_peer_id(or_connection_t *conn, const ed25519_public_key_t *ed_peer_id); time_t connection_or_client_used(or_connection_t *conn); MOCK_DECL(int, connection_or_get_num_circuits, (or_connection_t *conn)); -void or_handshake_state_free(or_handshake_state_t *state); +void or_handshake_state_free_(or_handshake_state_t *state); +#define or_handshake_state_free(state) \ + FREE_AND_NULL(or_handshake_state_t, or_handshake_state_free_, (state)) void or_handshake_state_record_cell(or_connection_t *conn, or_handshake_state_t *state, const cell_t *cell, @@ -105,7 +107,8 @@ int var_cell_pack_header(const var_cell_t *cell, char *hdr_out, int wide_circ_ids); var_cell_t *var_cell_new(uint16_t payload_len); var_cell_t *var_cell_copy(const var_cell_t *src); -void var_cell_free(var_cell_t *cell); +void var_cell_free_(var_cell_t *cell); +#define var_cell_free(cell) FREE_AND_NULL(var_cell_t, var_cell_free_, (cell)) /* DOCDOC */ #define MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS 4 diff --git a/src/or/conscache.c b/src/or/conscache.c index 9a90ff4dd2..e25ac5f40b 100644 --- a/src/or/conscache.c +++ b/src/or/conscache.c @@ -170,7 +170,7 @@ consensus_cache_clear(consensus_cache_t *cache) * Drop all storage held by <b>cache</b>. */ void -consensus_cache_free(consensus_cache_t *cache) +consensus_cache_free_(consensus_cache_t *cache) { if (! cache) return; diff --git a/src/or/conscache.h b/src/or/conscache.h index 3c89dedf43..08a5c5a37b 100644 --- a/src/or/conscache.h +++ b/src/or/conscache.h @@ -10,9 +10,14 @@ typedef struct consensus_cache_entry_t consensus_cache_entry_t; typedef struct consensus_cache_t consensus_cache_t; HANDLE_DECL(consensus_cache_entry, consensus_cache_entry_t, ) +#define consensus_cache_entry_handle_free(h) \ + FREE_AND_NULL(consensus_cache_entry_handle_t, \ + consensus_cache_entry_handle_free_, (h)) consensus_cache_t *consensus_cache_open(const char *subdir, int max_entries); -void consensus_cache_free(consensus_cache_t *cache); +void consensus_cache_free_(consensus_cache_t *cache); +#define consensus_cache_free(cache) \ + FREE_AND_NULL(consensus_cache_t, consensus_cache_free_, (cache)) struct sandbox_cfg_elem; int consensus_cache_may_overallocate(consensus_cache_t *cache); int consensus_cache_register_with_sandbox(consensus_cache_t *cache, diff --git a/src/or/consdiffmgr.c b/src/or/consdiffmgr.c index e539b61484..38d901a3ae 100644 --- a/src/or/consdiffmgr.c +++ b/src/or/consdiffmgr.c @@ -207,9 +207,12 @@ HT_PROTOTYPE(cdm_diff_ht, cdm_diff_t, node, cdm_diff_hash, cdm_diff_eq) HT_GENERATE2(cdm_diff_ht, cdm_diff_t, node, cdm_diff_hash, cdm_diff_eq, 0.6, tor_reallocarray, tor_free_) +#define cdm_diff_free(diff) \ + FREE_AND_NULL(cdm_diff_t, cdm_diff_free_, (diff)) + /** Release all storage held in <b>diff</b>. */ static void -cdm_diff_free(cdm_diff_t *diff) +cdm_diff_free_(cdm_diff_t *diff) { if (!diff) return; @@ -1506,11 +1509,15 @@ consensus_diff_worker_threadfn(void *state_, void *work_) return WQ_RPL_REPLY; } +#define consensus_diff_worker_job_free(job) \ + FREE_AND_NULL(consensus_diff_worker_job_t, \ + consensus_diff_worker_job_free_, (job)) + /** * Helper: release all storage held in <b>job</b>. */ static void -consensus_diff_worker_job_free(consensus_diff_worker_job_t *job) +consensus_diff_worker_job_free_(consensus_diff_worker_job_t *job) { if (!job) return; @@ -1658,11 +1665,15 @@ typedef struct consensus_compress_worker_job_t { compressed_result_t out[ARRAY_LENGTH(compress_consensus_with)]; } consensus_compress_worker_job_t; +#define consensus_compress_worker_job_free(job) \ + FREE_AND_NULL(consensus_compress_worker_job_t, \ + consensus_compress_worker_job_free_, (job)) + /** * Free all resources held in <b>job</b> */ static void -consensus_compress_worker_job_free(consensus_compress_worker_job_t *job) +consensus_compress_worker_job_free_(consensus_compress_worker_job_t *job) { if (!job) return; diff --git a/src/or/control.c b/src/or/control.c index cb9d69c6cd..cce5c7953b 100644 --- a/src/or/control.c +++ b/src/or/control.c @@ -785,9 +785,12 @@ queue_control_event_string,(uint16_t event, char *msg)) } } +#define queued_event_free(ev) \ + FREE_AND_NULL(queued_event_t, queued_event_free_, (ev)) + /** Release all storage held by <b>ev</b>. */ static void -queued_event_free(queued_event_t *ev) +queued_event_free_(queued_event_t *ev) { if (ev == NULL) return; diff --git a/src/or/cpuworker.c b/src/or/cpuworker.c index d9371b3446..9bec04d790 100644 --- a/src/or/cpuworker.c +++ b/src/or/cpuworker.c @@ -48,14 +48,25 @@ worker_state_new(void *arg) ws->onion_keys = server_onion_keys_new(); return ws; } + +#define worker_state_free(ws) \ + FREE_AND_NULL(worker_state_t, worker_state_free_, (ws)) + static void -worker_state_free(void *arg) +worker_state_free_(worker_state_t *ws) { - worker_state_t *ws = arg; + if (!ws) + return; server_onion_keys_free(ws->onion_keys); tor_free(ws); } +static void +worker_state_free_void(void *arg) +{ + worker_state_free_(arg); +} + static replyqueue_t *replyqueue = NULL; static threadpool_t *threadpool = NULL; static struct event *reply_event = NULL; @@ -102,7 +113,7 @@ cpu_init(void) threadpool = threadpool_new(n_threads, replyqueue, worker_state_new, - worker_state_free, + worker_state_free_void, NULL); } /* Total voodoo. Can we make this more sensible? */ @@ -198,7 +209,7 @@ cpuworkers_rotate_keyinfo(void) if (threadpool_queue_update(threadpool, worker_state_new, update_state_threadfn, - worker_state_free, + worker_state_free_void, NULL)) { log_warn(LD_OR, "Failed to queue key update for worker threads."); } diff --git a/src/or/dircollate.c b/src/or/dircollate.c index d34ebe8af5..ce4534ff6c 100644 --- a/src/or/dircollate.c +++ b/src/or/dircollate.c @@ -41,9 +41,12 @@ typedef struct ddmap_entry_s { vote_routerstatus_t *vrs_lst[FLEXIBLE_ARRAY_MEMBER]; } ddmap_entry_t; +#define ddmap_entry_free(e) \ + FREE_AND_NULL(ddmap_entry_t, ddmap_entry_free_, (e)) + /** Release all storage held by e. */ static void -ddmap_entry_free(ddmap_entry_t *e) +ddmap_entry_free_(ddmap_entry_t *e) { tor_free(e); } @@ -158,7 +161,7 @@ dircollator_new(int n_votes, int n_authorities) /** Release all storage held by <b>dc</b>. */ void -dircollator_free(dircollator_t *dc) +dircollator_free_(dircollator_t *dc) { if (!dc) return; diff --git a/src/or/dircollate.h b/src/or/dircollate.h index 7932e18998..0584b2fe06 100644 --- a/src/or/dircollate.h +++ b/src/or/dircollate.h @@ -18,7 +18,9 @@ typedef struct dircollator_s dircollator_t; dircollator_t *dircollator_new(int n_votes, int n_authorities); -void dircollator_free(dircollator_t *obj); +void dircollator_free_(dircollator_t *obj); +#define dircollator_free(c) \ + FREE_AND_NULL(dircollator_t, dircollator_free_, (c)) void dircollator_add_vote(dircollator_t *dc, networkstatus_t *v); void dircollator_collate(dircollator_t *dc, int consensus_method); diff --git a/src/or/directory.c b/src/or/directory.c index 1a7c016c3e..dcdcff396f 100644 --- a/src/or/directory.c +++ b/src/or/directory.c @@ -1102,7 +1102,7 @@ directory_request_new(uint8_t dir_purpose) * Release all resources held by <b>req</b>. */ void -directory_request_free(directory_request_t *req) +directory_request_free_(directory_request_t *req) { if (req == NULL) return; diff --git a/src/or/directory.h b/src/or/directory.h index 3aef600716..edf75ffe13 100644 --- a/src/or/directory.h +++ b/src/or/directory.h @@ -51,7 +51,9 @@ int directory_must_use_begindir(const or_options_t *options); */ typedef struct directory_request_t directory_request_t; directory_request_t *directory_request_new(uint8_t dir_purpose); -void directory_request_free(directory_request_t *req); +void directory_request_free_(directory_request_t *req); +#define directory_request_free(req) \ + FREE_AND_NULL(directory_request_t, directory_request_free_, (req)) void directory_request_set_or_addr_port(directory_request_t *req, const tor_addr_port_t *p); void directory_request_set_dir_addr_port(directory_request_t *req, diff --git a/src/or/dirserv.c b/src/or/dirserv.c index c8ec788332..d3bae241f9 100644 --- a/src/or/dirserv.c +++ b/src/or/dirserv.c @@ -3515,7 +3515,7 @@ spooled_resource_new_from_cache_entry(consensus_cache_entry_t *entry) /** Release all storage held by <b>spooled</b>. */ void -spooled_resource_free(spooled_resource_t *spooled) +spooled_resource_free_(spooled_resource_t *spooled) { if (spooled == NULL) return; diff --git a/src/or/dirserv.h b/src/or/dirserv.h index 9dc239f213..0fd7b79150 100644 --- a/src/or/dirserv.h +++ b/src/or/dirserv.h @@ -196,7 +196,9 @@ spooled_resource_t *spooled_resource_new(dir_spool_source_t source, size_t digestlen); spooled_resource_t *spooled_resource_new_from_cache_entry( struct consensus_cache_entry_t *entry); -void spooled_resource_free(spooled_resource_t *spooled); +void spooled_resource_free_(spooled_resource_t *spooled); +#define spooled_resource_free(sp) \ + FREE_AND_NULL(spooled_resource_t, spooled_resource_free_, (sp)) void dirserv_spool_remove_missing_and_guess_size(dir_connection_t *conn, time_t cutoff, int compression, diff --git a/src/or/dirvote.c b/src/or/dirvote.c index 24a6ed42be..a75d9e55a5 100644 --- a/src/or/dirvote.c +++ b/src/or/dirvote.c @@ -2691,7 +2691,7 @@ get_detached_signatures_from_pending_consensuses(pending_consensus_t *pending, /** Release all storage held in <b>s</b>. */ void -ns_detached_signatures_free(ns_detached_signatures_t *s) +ns_detached_signatures_free_(ns_detached_signatures_t *s) { if (!s) return; @@ -2849,10 +2849,13 @@ get_voting_schedule(const or_options_t *options, time_t now, int severity) return new_voting_schedule; } +#define voting_schedule_free(s) \ + FREE_AND_NULL(voting_schedule_t, voting_schedule_free_, (s)) + /** Frees a voting_schedule_t. This should be used instead of the generic * tor_free. */ static void -voting_schedule_free(voting_schedule_t *voting_schedule_to_free) +voting_schedule_free_(voting_schedule_t *voting_schedule_to_free) { if (!voting_schedule_to_free) return; diff --git a/src/or/dirvote.h b/src/or/dirvote.h index c9cb527788..deeb27bfe1 100644 --- a/src/or/dirvote.h +++ b/src/or/dirvote.h @@ -148,7 +148,9 @@ int networkstatus_add_detached_signatures(networkstatus_t *target, int severity, const char **msg_out); char *networkstatus_get_detached_signatures(smartlist_t *consensuses); -void ns_detached_signatures_free(ns_detached_signatures_t *s); +void ns_detached_signatures_free_(ns_detached_signatures_t *s); +#define ns_detached_signatures_free(s) \ + FREE_AND_NULL(ns_detached_signatures_t, ns_detached_signatures_free_, (s)) /* cert manipulation */ authority_cert_t *authority_cert_dup(authority_cert_t *cert); diff --git a/src/or/dns.c b/src/or/dns.c index f140051e81..1f00fe5b4d 100644 --- a/src/or/dns.c +++ b/src/or/dns.c @@ -457,7 +457,7 @@ purge_expired_resolves(time_t now) if (!pendconn->base_.marked_for_close) { connection_edge_end(pendconn, END_STREAM_REASON_TIMEOUT); circuit_detach_stream(circuit_get_by_edge_conn(pendconn), pendconn); - connection_free(TO_CONN(pendconn)); + connection_free_(TO_CONN(pendconn)); } tor_free(pend); } @@ -670,7 +670,7 @@ dns_resolve(edge_connection_t *exitconn) /* If we made the connection pending, then we freed it already in * dns_cancel_pending_resolve(). If we marked it for close, it'll * get freed from the main loop. Otherwise, can free it now. */ - connection_free(TO_CONN(exitconn)); + connection_free_(TO_CONN(exitconn)); } break; default: @@ -1101,7 +1101,7 @@ dns_cancel_pending_resolve,(const char *address)) if (circ) circuit_detach_stream(circ, pendconn); if (!pendconn->base_.marked_for_close) - connection_free(TO_CONN(pendconn)); + connection_free_(TO_CONN(pendconn)); resolve->pending_connections = pend->next; tor_free(pend); } @@ -1230,7 +1230,7 @@ inform_pending_connections(cached_resolve_t *resolve) /* This detach must happen after we send the resolved cell. */ circuit_detach_stream(circuit_get_by_edge_conn(pendconn), pendconn); } - connection_free(TO_CONN(pendconn)); + connection_free_(TO_CONN(pendconn)); } else { circuit_t *circ; if (pendconn->base_.purpose == EXIT_PURPOSE_CONNECT) { @@ -1259,7 +1259,7 @@ inform_pending_connections(cached_resolve_t *resolve) circ = circuit_get_by_edge_conn(pendconn); tor_assert(circ); circuit_detach_stream(circ, pendconn); - connection_free(TO_CONN(pendconn)); + connection_free_(TO_CONN(pendconn)); } } resolve->pending_connections = pend->next; diff --git a/src/or/dnsserv.c b/src/or/dnsserv.c index d254717a54..9385561d99 100644 --- a/src/or/dnsserv.c +++ b/src/or/dnsserv.c @@ -172,7 +172,7 @@ evdns_server_callback(struct evdns_server_request *req, void *data_) if (connection_add(ENTRY_TO_CONN(entry_conn)) < 0) { log_warn(LD_APP, "Couldn't register dummy connection for DNS request"); evdns_server_request_respond(req, DNS_ERR_SERVERFAILED); - connection_free(ENTRY_TO_CONN(entry_conn)); + connection_free_(ENTRY_TO_CONN(entry_conn)); return; } @@ -249,7 +249,7 @@ dnsserv_launch_request(const char *name, int reverse, if (connection_add(TO_CONN(conn))<0) { log_warn(LD_APP, "Couldn't register dummy connection for RESOLVE request"); - connection_free(TO_CONN(conn)); + connection_free_(TO_CONN(conn)); return -1; } diff --git a/src/or/entrynodes.c b/src/or/entrynodes.c index f9438361f0..cbdb36aa43 100644 --- a/src/or/entrynodes.c +++ b/src/or/entrynodes.c @@ -2196,7 +2196,7 @@ entry_guard_has_higher_priority(entry_guard_t *a, entry_guard_t *b) /** Release all storage held in <b>restriction</b> */ STATIC void -entry_guard_restriction_free(entry_guard_restriction_t *rst) +entry_guard_restriction_free_(entry_guard_restriction_t *rst) { tor_free(rst); } @@ -2205,7 +2205,7 @@ entry_guard_restriction_free(entry_guard_restriction_t *rst) * Release all storage held in <b>state</b>. */ void -circuit_guard_state_free(circuit_guard_state_t *state) +circuit_guard_state_free_(circuit_guard_state_t *state) { if (!state) return; @@ -3108,7 +3108,7 @@ get_guard_state_for_bridge_desc_fetch(const char *digest) /** Release all storage held by <b>e</b>. */ STATIC void -entry_guard_free(entry_guard_t *e) +entry_guard_free_(entry_guard_t *e) { if (!e) return; @@ -3601,7 +3601,7 @@ entry_guards_get_err_str_if_dir_info_missing(int using_mds, /** Free one guard selection context */ STATIC void -guard_selection_free(guard_selection_t *gs) +guard_selection_free_(guard_selection_t *gs) { if (!gs) return; diff --git a/src/or/entrynodes.h b/src/or/entrynodes.h index 9d1293d02a..21ba706c5b 100644 --- a/src/or/entrynodes.h +++ b/src/or/entrynodes.h @@ -356,7 +356,10 @@ typedef enum { GUARD_USAGE_DIRGUARD = 1 } guard_usage_t; -void circuit_guard_state_free(circuit_guard_state_t *state); +#define circuit_guard_state_free(val) \ + FREE_AND_NULL(circuit_guard_state_t, circuit_guard_state_free_, (val)) + +void circuit_guard_state_free_(circuit_guard_state_t *state); int entry_guard_pick_for_circuit(guard_selection_t *gs, guard_usage_t usage, entry_guard_restriction_t *rst, @@ -476,6 +479,9 @@ STATIC double get_meaningful_restriction_threshold(void); STATIC double get_extreme_restriction_threshold(void); HANDLE_DECL(entry_guard, entry_guard_t, STATIC) +#define entry_guard_handle_free(h) \ + FREE_AND_NULL(entry_guard_handle_t, entry_guard_handle_free_, (h)) + STATIC guard_selection_type_t guard_selection_infer_type( guard_selection_type_t type_in, const char *name); @@ -483,7 +489,9 @@ STATIC guard_selection_t *guard_selection_new(const char *name, guard_selection_type_t type); STATIC guard_selection_t *get_guard_selection_by_name( const char *name, guard_selection_type_t type, int create_if_absent); -STATIC void guard_selection_free(guard_selection_t *gs); +STATIC void guard_selection_free_(guard_selection_t *gs); +#define guard_selection_free(gs) \ + FREE_AND_NULL(guard_selection_t, guard_selection_free_, (gs)) MOCK_DECL(STATIC int, entry_guard_is_listed, (guard_selection_t *gs, const entry_guard_t *guard)); STATIC const char *choose_guard_selection(const or_options_t *options, @@ -504,7 +512,9 @@ STATIC entry_guard_t *entry_guard_add_to_sample(guard_selection_t *gs, STATIC entry_guard_t *entry_guards_expand_sample(guard_selection_t *gs); STATIC char *entry_guard_encode_for_state(entry_guard_t *guard); STATIC entry_guard_t *entry_guard_parse_from_state(const char *s); -STATIC void entry_guard_free(entry_guard_t *e); +#define entry_guard_free(e) \ + FREE_AND_NULL(entry_guard_t, entry_guard_free_, (e)) +STATIC void entry_guard_free_(entry_guard_t *e); STATIC void entry_guards_update_filtered_sets(guard_selection_t *gs); STATIC int entry_guards_all_primary_guards_are_down(guard_selection_t *gs); /** @@ -567,7 +577,10 @@ STATIC entry_guard_restriction_t *guard_create_exit_restriction( STATIC entry_guard_restriction_t *guard_create_dirserver_md_restriction(void); -STATIC void entry_guard_restriction_free(entry_guard_restriction_t *rst); +STATIC void entry_guard_restriction_free_(entry_guard_restriction_t *rst); +#define entry_guard_restriction_free(rst) \ + FREE_AND_NULL(entry_guard_restriction_t, \ + entry_guard_restriction_free_, (rst)) #endif /* defined(ENTRYNODES_PRIVATE) */ diff --git a/src/or/ext_orport.c b/src/or/ext_orport.c index 28377a3f36..16a250fa58 100644 --- a/src/or/ext_orport.c +++ b/src/or/ext_orport.c @@ -40,7 +40,7 @@ ext_or_cmd_new(uint16_t len) /** Deallocate the Extended ORPort message in <b>cmd</b>. */ void -ext_or_cmd_free(ext_or_cmd_t *cmd) +ext_or_cmd_free_(ext_or_cmd_t *cmd) { tor_free(cmd); } diff --git a/src/or/ext_orport.h b/src/or/ext_orport.h index af2b97e77c..09acbc407e 100644 --- a/src/or/ext_orport.h +++ b/src/or/ext_orport.h @@ -10,7 +10,11 @@ int connection_ext_or_start_auth(or_connection_t *or_conn); ext_or_cmd_t *ext_or_cmd_new(uint16_t len); -void ext_or_cmd_free(ext_or_cmd_t *cmd); + +#define ext_or_cmd_free(cmd) \ + FREE_AND_NULL(ext_or_cmd_t, ext_or_cmd_free_, (cmd)) + +void ext_or_cmd_free_(ext_or_cmd_t *cmd); void connection_or_set_ext_or_identifier(or_connection_t *conn); void connection_or_remove_from_ext_or_id_map(or_connection_t *conn); void connection_or_clear_ext_or_id_map(void); diff --git a/src/or/fp_pair.c b/src/or/fp_pair.c index f730106d06..c938e76678 100644 --- a/src/or/fp_pair.c +++ b/src/or/fp_pair.c @@ -196,7 +196,7 @@ fp_pair_map_remove(fp_pair_map_t *map, const fp_pair_t *key) */ void -fp_pair_map_free(fp_pair_map_t *map, void (*free_val)(void*)) +fp_pair_map_free_(fp_pair_map_t *map, void (*free_val)(void*)) { fp_pair_map_entry_t **ent, **next, *this; diff --git a/src/or/fp_pair.h b/src/or/fp_pair.h index f7c060b459..4498a16101 100644 --- a/src/or/fp_pair.h +++ b/src/or/fp_pair.h @@ -26,7 +26,12 @@ void * fp_pair_map_get(const fp_pair_map_t *map, const fp_pair_t *key); void * fp_pair_map_get_by_digests(const fp_pair_map_t *map, const char *first, const char *second); void * fp_pair_map_remove(fp_pair_map_t *map, const fp_pair_t *key); -void fp_pair_map_free(fp_pair_map_t *map, void (*free_val)(void*)); +void fp_pair_map_free_(fp_pair_map_t *map, void (*free_val)(void*)); +#define fp_pair_map_free(map, free_val) do { \ + fp_pair_map_free_((map), (free_val)); \ + (map) = NULL; \ + } while (0) + int fp_pair_map_isempty(const fp_pair_map_t *map); int fp_pair_map_size(const fp_pair_map_t *map); fp_pair_map_iter_t * fp_pair_map_iter_init(fp_pair_map_t *map); diff --git a/src/or/geoip.c b/src/or/geoip.c index c976b8d276..d7411e6aaa 100644 --- a/src/or/geoip.c +++ b/src/or/geoip.c @@ -527,9 +527,12 @@ HT_PROTOTYPE(clientmap, clientmap_entry_t, node, clientmap_entry_hash, HT_GENERATE2(clientmap, clientmap_entry_t, node, clientmap_entry_hash, clientmap_entries_eq, 0.6, tor_reallocarray_, tor_free_) +#define clientmap_entry_free(ent) \ + FREE_AND_NULL(clientmap_entry_t, clientmap_entry_free_, ent) + /** Free all storage held by <b>ent</b>. */ static void -clientmap_entry_free(clientmap_entry_t *ent) +clientmap_entry_free_(clientmap_entry_t *ent) { if (!ent) return; diff --git a/src/or/hs_cache.c b/src/or/hs_cache.c index b864a0f717..1a8fdbd03b 100644 --- a/src/or/hs_cache.c +++ b/src/or/hs_cache.c @@ -52,9 +52,12 @@ lookup_v3_desc_as_dir(const uint8_t *key) return digest256map_get(hs_cache_v3_dir, key); } +#define cache_dir_desc_free(val) \ + FREE_AND_NULL(hs_cache_dir_descriptor_t, cache_dir_desc_free_, (val)) + /* Free a directory descriptor object. */ static void -cache_dir_desc_free(hs_cache_dir_descriptor_t *desc) +cache_dir_desc_free_(hs_cache_dir_descriptor_t *desc) { if (desc == NULL) { return; @@ -67,10 +70,9 @@ cache_dir_desc_free(hs_cache_dir_descriptor_t *desc) /* Helper function: Use by the free all function using the digest256map * interface to cache entries. */ static void -cache_dir_desc_free_(void *ptr) +cache_dir_desc_free_void(void *ptr) { - hs_cache_dir_descriptor_t *desc = ptr; - cache_dir_desc_free(desc); + cache_dir_desc_free_(ptr); } /* Create a new directory cache descriptor object from a encoded descriptor. @@ -417,9 +419,12 @@ cache_client_desc_new(const char *desc_str, return client_desc; } +#define cache_client_desc_free(val) \ + FREE_AND_NULL(hs_cache_client_descriptor_t, cache_client_desc_free_, (val)) + /** Free memory allocated by <b>desc</b>. */ static void -cache_client_desc_free(hs_cache_client_descriptor_t *desc) +cache_client_desc_free_(hs_cache_client_descriptor_t *desc) { if (desc == NULL) { return; @@ -433,7 +438,7 @@ cache_client_desc_free(hs_cache_client_descriptor_t *desc) /** Helper function: Use by the free all function to clear the client cache */ static void -cache_client_desc_free_(void *ptr) +cache_client_desc_free_void(void *ptr) { hs_cache_client_descriptor_t *desc = ptr; cache_client_desc_free(desc); @@ -448,18 +453,21 @@ cache_intro_state_new(void) return state; } +#define cache_intro_state_free(val) \ + FREE_AND_NULL(hs_cache_intro_state_t, cache_intro_state_free_, (val)) + /* Free an hs_cache_intro_state_t object. */ static void -cache_intro_state_free(hs_cache_intro_state_t *state) +cache_intro_state_free_(hs_cache_intro_state_t *state) { tor_free(state); } /* Helper function: use by the free all function. */ static void -cache_intro_state_free_(void *state) +cache_intro_state_free_void(void *state) { - cache_intro_state_free(state); + cache_intro_state_free_(state); } /* Return a newly allocated and initialized hs_cache_client_intro_state_t @@ -472,22 +480,26 @@ cache_client_intro_state_new(void) return cache; } +#define cache_client_intro_state_free(val) \ + FREE_AND_NULL(hs_cache_client_intro_state_t, \ + cache_client_intro_state_free_, (val)) + /* Free a cache client intro state object. */ static void -cache_client_intro_state_free(hs_cache_client_intro_state_t *cache) +cache_client_intro_state_free_(hs_cache_client_intro_state_t *cache) { if (cache == NULL) { return; } - digest256map_free(cache->intro_points, cache_intro_state_free_); + digest256map_free(cache->intro_points, cache_intro_state_free_void); tor_free(cache); } /* Helper function: use by the free all function. */ static void -cache_client_intro_state_free_(void *entry) +cache_client_intro_state_free_void(void *entry) { - cache_client_intro_state_free(entry); + cache_client_intro_state_free_(entry); } /* For the given service identity key service_pk and an introduction @@ -951,14 +963,14 @@ hs_cache_init(void) void hs_cache_free_all(void) { - digest256map_free(hs_cache_v3_dir, cache_dir_desc_free_); + digest256map_free(hs_cache_v3_dir, cache_dir_desc_free_void); hs_cache_v3_dir = NULL; - digest256map_free(hs_cache_v3_client, cache_client_desc_free_); + digest256map_free(hs_cache_v3_client, cache_client_desc_free_void); hs_cache_v3_client = NULL; digest256map_free(hs_cache_client_intro_state, - cache_client_intro_state_free_); + cache_client_intro_state_free_void); hs_cache_client_intro_state = NULL; } diff --git a/src/or/hs_circuitmap.c b/src/or/hs_circuitmap.c index 97d6053e9b..e3d364d7cd 100644 --- a/src/or/hs_circuitmap.c +++ b/src/or/hs_circuitmap.c @@ -106,9 +106,12 @@ hs_token_new(hs_token_type_t type, size_t token_len, return hs_token; } +#define hs_token_free(val) \ + FREE_AND_NULL(hs_token_t, hs_token_free_, (val)) + /** Free memory allocated by this <b>hs_token</b>. */ static void -hs_token_free(hs_token_t *hs_token) +hs_token_free_(hs_token_t *hs_token) { if (!hs_token) { return; diff --git a/src/or/hs_common.c b/src/or/hs_common.c index a0f2af29cd..fcc524ae1c 100644 --- a/src/or/hs_common.c +++ b/src/or/hs_common.c @@ -329,7 +329,7 @@ rend_data_alloc(uint32_t version) /** Free all storage associated with <b>data</b> */ void -rend_data_free(rend_data_t *data) +rend_data_free_(rend_data_t *data) { if (!data) { return; diff --git a/src/or/hs_common.h b/src/or/hs_common.h index 58fcd1b93c..40fa1bc466 100644 --- a/src/or/hs_common.h +++ b/src/or/hs_common.h @@ -192,7 +192,9 @@ void hs_build_blinded_keypair(const ed25519_keypair_t *kp, ed25519_keypair_t *kp_out); int hs_service_requires_uptime_circ(const smartlist_t *ports); -void rend_data_free(rend_data_t *data); +void rend_data_free_(rend_data_t *data); +#define rend_data_free(data) \ + FREE_AND_NULL(rend_data_t, rend_data_free_, (data)) rend_data_t *rend_data_dup(const rend_data_t *data); rend_data_t *rend_data_client_create(const char *onion_address, const char *desc_id, diff --git a/src/or/hs_descriptor.c b/src/or/hs_descriptor.c index 1708866944..789bc1d046 100644 --- a/src/or/hs_descriptor.c +++ b/src/or/hs_descriptor.c @@ -2367,7 +2367,7 @@ hs_desc_encode_descriptor,(const hs_descriptor_t *desc, /* Free the descriptor plaintext data object. */ void -hs_desc_plaintext_data_free(hs_desc_plaintext_data_t *desc) +hs_desc_plaintext_data_free_(hs_desc_plaintext_data_t *desc) { desc_plaintext_data_free_contents(desc); tor_free(desc); @@ -2375,7 +2375,7 @@ hs_desc_plaintext_data_free(hs_desc_plaintext_data_t *desc) /* Free the descriptor encrypted data object. */ void -hs_desc_encrypted_data_free(hs_desc_encrypted_data_t *desc) +hs_desc_encrypted_data_free_(hs_desc_encrypted_data_t *desc) { desc_encrypted_data_free_contents(desc); tor_free(desc); @@ -2383,7 +2383,7 @@ hs_desc_encrypted_data_free(hs_desc_encrypted_data_t *desc) /* Free the given descriptor object. */ void -hs_descriptor_free(hs_descriptor_t *desc) +hs_descriptor_free_(hs_descriptor_t *desc) { if (!desc) { return; @@ -2448,7 +2448,7 @@ hs_desc_intro_point_new(void) /* Free a descriptor intro point object. */ void -hs_desc_intro_point_free(hs_desc_intro_point_t *ip) +hs_desc_intro_point_free_(hs_desc_intro_point_t *ip) { if (ip == NULL) { return; @@ -2467,7 +2467,7 @@ hs_desc_intro_point_free(hs_desc_intro_point_t *ip) /* Free the given descriptor link specifier. */ void -hs_desc_link_specifier_free(hs_desc_link_specifier_t *ls) +hs_desc_link_specifier_free_(hs_desc_link_specifier_t *ls) { if (ls == NULL) { return; diff --git a/src/or/hs_descriptor.h b/src/or/hs_descriptor.h index 52bec8e244..09979410e1 100644 --- a/src/or/hs_descriptor.h +++ b/src/or/hs_descriptor.h @@ -208,11 +208,20 @@ hs_desc_is_supported_version(uint32_t version) /* Public API. */ -void hs_descriptor_free(hs_descriptor_t *desc); -void hs_desc_plaintext_data_free(hs_desc_plaintext_data_t *desc); -void hs_desc_encrypted_data_free(hs_desc_encrypted_data_t *desc); +void hs_descriptor_free_(hs_descriptor_t *desc); +#define hs_descriptor_free(desc) \ + FREE_AND_NULL(hs_descriptor_t, hs_descriptor_free_, (desc)) +void hs_desc_plaintext_data_free_(hs_desc_plaintext_data_t *desc); +#define hs_desc_plaintext_data_free(desc) \ + FREE_AND_NULL(hs_desc_plaintext_data_t, hs_desc_plaintext_data_free_, (desc)) +void hs_desc_encrypted_data_free_(hs_desc_encrypted_data_t *desc); +#define hs_desc_encrypted_data_free(desc) \ + FREE_AND_NULL(hs_desc_encrypted_data_t, hs_desc_encrypted_data_free_, (desc)) + +void hs_desc_link_specifier_free_(hs_desc_link_specifier_t *ls); +#define hs_desc_link_specifier_free(ls) \ + FREE_AND_NULL(hs_desc_link_specifier_t, hs_desc_link_specifier_free_, (ls)) -void hs_desc_link_specifier_free(hs_desc_link_specifier_t *ls); hs_desc_link_specifier_t *hs_desc_link_specifier_new( const extend_info_t *info, uint8_t type); void hs_descriptor_clear_intro_points(hs_descriptor_t *desc); @@ -234,7 +243,9 @@ size_t hs_desc_obj_size(const hs_descriptor_t *data); size_t hs_desc_plaintext_obj_size(const hs_desc_plaintext_data_t *data); hs_desc_intro_point_t *hs_desc_intro_point_new(void); -void hs_desc_intro_point_free(hs_desc_intro_point_t *ip); +void hs_desc_intro_point_free_(hs_desc_intro_point_t *ip); +#define hs_desc_intro_point_free(ip) \ + FREE_AND_NULL(hs_desc_intro_point_t, hs_desc_intro_point_free_, (ip)) link_specifier_t *hs_desc_lspec_to_trunnel( const hs_desc_link_specifier_t *spec); diff --git a/src/or/hs_ident.c b/src/or/hs_ident.c index b0e4e36a9b..0bce2f625b 100644 --- a/src/or/hs_ident.c +++ b/src/or/hs_ident.c @@ -25,7 +25,7 @@ hs_ident_circuit_new(const ed25519_public_key_t *identity_pk, /* Free the given circuit identifier. */ void -hs_ident_circuit_free(hs_ident_circuit_t *ident) +hs_ident_circuit_free_(hs_ident_circuit_t *ident) { if (ident == NULL) { return; @@ -56,7 +56,7 @@ hs_ident_dir_conn_dup(const hs_ident_dir_conn_t *src) /* Free the given directory connection identifier. */ void -hs_ident_dir_conn_free(hs_ident_dir_conn_t *ident) +hs_ident_dir_conn_free_(hs_ident_dir_conn_t *ident) { if (ident == NULL) { return; @@ -93,7 +93,7 @@ hs_ident_edge_conn_new(const ed25519_public_key_t *identity_pk) /* Free the given edge connection identifier. */ void -hs_ident_edge_conn_free(hs_ident_edge_conn_t *ident) +hs_ident_edge_conn_free_(hs_ident_edge_conn_t *ident) { if (ident == NULL) { return; diff --git a/src/or/hs_ident.h b/src/or/hs_ident.h index 03150d25ea..91ec389aa4 100644 --- a/src/or/hs_ident.h +++ b/src/or/hs_ident.h @@ -119,12 +119,16 @@ typedef struct hs_ident_edge_conn_t { hs_ident_circuit_t *hs_ident_circuit_new( const ed25519_public_key_t *identity_pk, hs_ident_circuit_type_t circuit_type); -void hs_ident_circuit_free(hs_ident_circuit_t *ident); +void hs_ident_circuit_free_(hs_ident_circuit_t *ident); +#define hs_ident_circuit_free(id) \ + FREE_AND_NULL(hs_ident_circuit_t, hs_ident_circuit_free_, (id)) hs_ident_circuit_t *hs_ident_circuit_dup(const hs_ident_circuit_t *src); /* Directory connection identifier API. */ hs_ident_dir_conn_t *hs_ident_dir_conn_dup(const hs_ident_dir_conn_t *src); -void hs_ident_dir_conn_free(hs_ident_dir_conn_t *ident); +void hs_ident_dir_conn_free_(hs_ident_dir_conn_t *ident); +#define hs_ident_dir_conn_free(id) \ + FREE_AND_NULL(hs_ident_dir_conn_t, hs_ident_dir_conn_free_, (id)) void hs_ident_dir_conn_init(const ed25519_public_key_t *identity_pk, const ed25519_public_key_t *blinded_pk, hs_ident_dir_conn_t *ident); @@ -132,7 +136,9 @@ void hs_ident_dir_conn_init(const ed25519_public_key_t *identity_pk, /* Edge connection identifier API. */ hs_ident_edge_conn_t *hs_ident_edge_conn_new( const ed25519_public_key_t *identity_pk); -void hs_ident_edge_conn_free(hs_ident_edge_conn_t *ident); +void hs_ident_edge_conn_free_(hs_ident_edge_conn_t *ident); +#define hs_ident_edge_conn_free(id) \ + FREE_AND_NULL(hs_ident_edge_conn_t, hs_ident_edge_conn_free_, (id)) /* Validators */ int hs_ident_intro_circ_is_valid(const hs_ident_circuit_t *ident); diff --git a/src/or/hs_service.c b/src/or/hs_service.c index f65b59ae1c..4190f1a038 100644 --- a/src/or/hs_service.c +++ b/src/or/hs_service.c @@ -353,7 +353,7 @@ service_free_all(void) /* Free a given service intro point object. */ STATIC void -service_intro_point_free(hs_service_intro_point_t *ip) +service_intro_point_free_(hs_service_intro_point_t *ip) { if (!ip) { return; @@ -369,9 +369,9 @@ service_intro_point_free(hs_service_intro_point_t *ip) /* Helper: free an hs_service_intro_point_t object. This function is used by * digest256map_free() which requires a void * pointer. */ static void -service_intro_point_free_(void *obj) +service_intro_point_free_void(void *obj) { - service_intro_point_free(obj); + service_intro_point_free_(obj); } /* Return a newly allocated service intro point and fully initialized from the @@ -1028,7 +1028,7 @@ load_service_keys(hs_service_t *service) /* Free a given service descriptor object and all key material is wiped. */ STATIC void -service_descriptor_free(hs_service_descriptor_t *desc) +service_descriptor_free_(hs_service_descriptor_t *desc) { if (!desc) { return; @@ -1037,7 +1037,7 @@ service_descriptor_free(hs_service_descriptor_t *desc) memwipe(&desc->signing_kp, 0, sizeof(desc->signing_kp)); memwipe(&desc->blinded_kp, 0, sizeof(desc->blinded_kp)); /* Cleanup all intro points. */ - digest256map_free(desc->intro_points.map, service_intro_point_free_); + digest256map_free(desc->intro_points.map, service_intro_point_free_void); digestmap_free(desc->intro_points.failed_id, tor_free_); if (desc->previous_hsdirs) { SMARTLIST_FOREACH(desc->previous_hsdirs, char *, s, tor_free(s)); @@ -3441,7 +3441,7 @@ hs_service_new(const or_options_t *options) * also takes care of wiping service keys from memory. It is safe to pass a * NULL pointer. */ void -hs_service_free(hs_service_t *service) +hs_service_free_(hs_service_t *service) { if (service == NULL) { return; diff --git a/src/or/hs_service.h b/src/or/hs_service.h index 678f24b0a2..d163eeef28 100644 --- a/src/or/hs_service.h +++ b/src/or/hs_service.h @@ -249,7 +249,8 @@ void hs_service_free_all(void); /* Service new/free functions. */ hs_service_t *hs_service_new(const or_options_t *options); -void hs_service_free(hs_service_t *service); +void hs_service_free_(hs_service_t *service); +#define hs_service_free(s) FREE_AND_NULL(hs_service_t, hs_service_free_, (s)) unsigned int hs_service_get_num_services(void); void hs_service_stage_services(const smartlist_t *service_list); @@ -289,12 +290,15 @@ void hs_service_upload_desc_to_dir(const char *encoded_desc, #ifdef HS_SERVICE_PRIVATE #ifdef TOR_UNIT_TESTS - /* Useful getters for unit tests. */ STATIC unsigned int get_hs_service_map_size(void); STATIC int get_hs_service_staging_list_size(void); STATIC hs_service_ht *get_hs_service_map(void); STATIC hs_service_t *get_first_service(void); +STATIC hs_service_intro_point_t *service_intro_point_find_by_ident( + const hs_service_t *service, + const hs_ident_circuit_t *ident); +#endif /* Service accessors. */ STATIC hs_service_t *find_service(hs_service_ht *map, @@ -305,7 +309,10 @@ STATIC int register_service(hs_service_ht *map, hs_service_t *service); STATIC hs_service_intro_point_t *service_intro_point_new( const extend_info_t *ei, unsigned int is_legacy); -STATIC void service_intro_point_free(hs_service_intro_point_t *ip); +STATIC void service_intro_point_free_(hs_service_intro_point_t *ip); +#define service_intro_point_free(ip) \ + FREE_AND_NULL(hs_service_intro_point_t, \ + service_intro_point_free_, (ip)) STATIC void service_intro_point_add(digest256map_t *map, hs_service_intro_point_t *ip); STATIC void service_intro_point_remove(const hs_service_t *service, @@ -313,9 +320,6 @@ STATIC void service_intro_point_remove(const hs_service_t *service, STATIC hs_service_intro_point_t *service_intro_point_find( const hs_service_t *service, const ed25519_public_key_t *auth_key); -STATIC hs_service_intro_point_t *service_intro_point_find_by_ident( - const hs_service_t *service, - const hs_ident_circuit_t *ident); /* Service descriptor functions. */ STATIC hs_service_descriptor_t *service_descriptor_new(void); STATIC hs_service_descriptor_t *service_desc_find_by_intro( @@ -341,7 +345,10 @@ STATIC void run_upload_descriptor_event(time_t now); STATIC char * encode_desc_rev_counter_for_state(const hs_service_descriptor_t *desc); -STATIC void service_descriptor_free(hs_service_descriptor_t *desc); +STATIC void service_descriptor_free_(hs_service_descriptor_t *desc); +#define service_descriptor_free(d) \ + FREE_AND_NULL(hs_service_descriptor_t, \ + service_descriptor_free_, (d)) STATIC uint64_t check_state_line_for_service_rev_counter(const char *state_line, @@ -361,8 +368,6 @@ STATIC void service_desc_schedule_upload(hs_service_descriptor_t *desc, STATIC int service_desc_hsdirs_changed(const hs_service_t *service, const hs_service_descriptor_t *desc); -#endif /* defined(TOR_UNIT_TESTS) */ - #endif /* defined(HS_SERVICE_PRIVATE) */ #endif /* !defined(TOR_HS_SERVICE_H) */ diff --git a/src/or/microdesc.h b/src/or/microdesc.h index 1be12156a4..83a90bd8ff 100644 --- a/src/or/microdesc.h +++ b/src/or/microdesc.h @@ -38,8 +38,10 @@ smartlist_t *microdesc_list_missing_digest256(networkstatus_t *ns, digest256map_t *skip); void microdesc_free_(microdesc_t *md, const char *fname, int line); -#define microdesc_free(md) \ - microdesc_free_((md), __FILE__, __LINE__) +#define microdesc_free(md) do { \ + microdesc_free_((md), __FILE__, __LINE__); \ + (md) = NULL; \ + } while (0) void microdesc_free_all(void); void update_microdesc_downloads(time_t now); diff --git a/src/or/networkstatus.c b/src/or/networkstatus.c index 14629cc5ff..2843d06262 100644 --- a/src/or/networkstatus.c +++ b/src/or/networkstatus.c @@ -255,7 +255,7 @@ router_reload_consensus_networkstatus(void) /** Free all storage held by the vote_routerstatus object <b>rs</b>. */ void -vote_routerstatus_free(vote_routerstatus_t *rs) +vote_routerstatus_free_(vote_routerstatus_t *rs) { vote_microdesc_hash_t *h, *next; if (!rs) @@ -273,7 +273,7 @@ vote_routerstatus_free(vote_routerstatus_t *rs) /** Free all storage held by the routerstatus object <b>rs</b>. */ void -routerstatus_free(routerstatus_t *rs) +routerstatus_free_(routerstatus_t *rs) { if (!rs) return; @@ -283,7 +283,7 @@ routerstatus_free(routerstatus_t *rs) /** Free all storage held in <b>sig</b> */ void -document_signature_free(document_signature_t *sig) +document_signature_free_(document_signature_t *sig) { tor_free(sig->signature); tor_free(sig); @@ -301,7 +301,7 @@ document_signature_dup(const document_signature_t *sig) /** Free all storage held in <b>ns</b>. */ void -networkstatus_vote_free(networkstatus_t *ns) +networkstatus_vote_free_(networkstatus_t *ns) { if (!ns) return; diff --git a/src/or/networkstatus.h b/src/or/networkstatus.h index 39a0f753d8..791e32adc4 100644 --- a/src/or/networkstatus.h +++ b/src/or/networkstatus.h @@ -18,8 +18,12 @@ void networkstatus_reset_warnings(void); void networkstatus_reset_download_failures(void); char *networkstatus_read_cached_consensus(const char *flavorname); int router_reload_consensus_networkstatus(void); -void routerstatus_free(routerstatus_t *rs); -void networkstatus_vote_free(networkstatus_t *ns); +void routerstatus_free_(routerstatus_t *rs); +#define routerstatus_free(rs) \ + FREE_AND_NULL(routerstatus_t, routerstatus_free_, (rs)) +void networkstatus_vote_free_(networkstatus_t *ns); +#define networkstatus_vote_free(ns) \ + FREE_AND_NULL(networkstatus_t, networkstatus_vote_free_, (ns)) networkstatus_voter_info_t *networkstatus_get_voter_by_id( networkstatus_t *vote, const char *identity); @@ -124,12 +128,16 @@ int32_t networkstatus_get_bw_weight(networkstatus_t *ns, const char *weight, int32_t default_val); const char *networkstatus_get_flavor_name(consensus_flavor_t flav); int networkstatus_parse_flavor_name(const char *flavname); -void document_signature_free(document_signature_t *sig); +void document_signature_free_(document_signature_t *sig); +#define document_signature_free(sig) \ + FREE_AND_NULL(document_signature_t, document_signature_free_, (sig)) document_signature_t *document_signature_dup(const document_signature_t *sig); void networkstatus_free_all(void); int networkstatus_get_weight_scale_param(networkstatus_t *ns); -void vote_routerstatus_free(vote_routerstatus_t *rs); +void vote_routerstatus_free_(vote_routerstatus_t *rs); +#define vote_routerstatus_free(rs) \ + FREE_AND_NULL(vote_routerstatus_t, vote_routerstatus_free_, (rs)) #ifdef NETWORKSTATUS_PRIVATE #ifdef TOR_UNIT_TESTS diff --git a/src/or/nodelist.c b/src/or/nodelist.c index 1ab385cd35..e6eaefb216 100644 --- a/src/or/nodelist.c +++ b/src/or/nodelist.c @@ -65,7 +65,9 @@ #include <string.h> static void nodelist_drop_node(node_t *node, int remove_from_ht); -static void node_free(node_t *node); +#define node_free(val) \ + FREE_AND_NULL(node_t, node_free_, (val)) +static void node_free_(node_t *node); /** count_usable_descriptors counts descriptors with these flag(s) */ @@ -656,7 +658,7 @@ nodelist_find_nodes_with_microdesc(const microdesc_t *md) /** Release storage held by <b>node</b> */ static void -node_free(node_t *node) +node_free_(node_t *node) { if (!node) return; diff --git a/src/or/onion.c b/src/or/onion.c index 7e1e89df1b..bd80c2f503 100644 --- a/src/or/onion.c +++ b/src/or/onion.c @@ -423,7 +423,7 @@ server_onion_keys_new(void) /** Release all storage held in <b>keys</b>. */ void -server_onion_keys_free(server_onion_keys_t *keys) +server_onion_keys_free_(server_onion_keys_t *keys) { if (! keys) return; diff --git a/src/or/onion.h b/src/or/onion.h index 95544dfac1..3b738debeb 100644 --- a/src/or/onion.h +++ b/src/or/onion.h @@ -31,7 +31,9 @@ typedef struct server_onion_keys_t { #define MAX_ONIONSKIN_REPLY_LEN 255 server_onion_keys_t *server_onion_keys_new(void); -void server_onion_keys_free(server_onion_keys_t *keys); +void server_onion_keys_free_(server_onion_keys_t *keys); +#define server_onion_keys_free(keys) \ + FREE_AND_NULL(server_onion_keys_t, server_onion_keys_free_, (keys)) void onion_handshake_state_release(onion_handshake_state_t *state); diff --git a/src/or/onion_fast.c b/src/or/onion_fast.c index 146943a273..56c954829e 100644 --- a/src/or/onion_fast.c +++ b/src/or/onion_fast.c @@ -32,7 +32,7 @@ /** Release all state held in <b>victim</b>. */ void -fast_handshake_state_free(fast_handshake_state_t *victim) +fast_handshake_state_free_(fast_handshake_state_t *victim) { if (! victim) return; diff --git a/src/or/onion_fast.h b/src/or/onion_fast.h index 3a5aefea3f..c56712e2c2 100644 --- a/src/or/onion_fast.h +++ b/src/or/onion_fast.h @@ -19,7 +19,9 @@ typedef struct fast_handshake_state_t { uint8_t state[DIGEST_LEN]; } fast_handshake_state_t; -void fast_handshake_state_free(fast_handshake_state_t *victim); +void fast_handshake_state_free_(fast_handshake_state_t *victim); +#define fast_handshake_state_free(st) \ + FREE_AND_NULL(fast_handshake_state_t, fast_handshake_state_free_, (st)) int fast_onionskin_create(fast_handshake_state_t **handshake_state_out, uint8_t *handshake_out); diff --git a/src/or/onion_ntor.c b/src/or/onion_ntor.c index 902260b54b..b167cb61fb 100644 --- a/src/or/onion_ntor.c +++ b/src/or/onion_ntor.c @@ -28,7 +28,7 @@ /** Free storage held in an ntor handshake state. */ void -ntor_handshake_state_free(ntor_handshake_state_t *state) +ntor_handshake_state_free_(ntor_handshake_state_t *state) { if (!state) return; diff --git a/src/or/onion_ntor.h b/src/or/onion_ntor.h index 02dea2dfc1..f7c962b7d0 100644 --- a/src/or/onion_ntor.h +++ b/src/or/onion_ntor.h @@ -17,7 +17,9 @@ typedef struct ntor_handshake_state_t ntor_handshake_state_t; /** Length of an ntor reply, as sent from server to client. */ #define NTOR_REPLY_LEN 64 -void ntor_handshake_state_free(ntor_handshake_state_t *state); +void ntor_handshake_state_free_(ntor_handshake_state_t *state); +#define ntor_handshake_state_free(state) \ + FREE_AND_NULL(ntor_handshake_state_t, ntor_handshake_state_free_, (state)) int onion_skin_ntor_create(const uint8_t *router_id, const curve25519_public_key_t *router_key, diff --git a/src/or/policies.c b/src/or/policies.c index 1f80130710..fc033c5593 100644 --- a/src/or/policies.c +++ b/src/or/policies.c @@ -1792,14 +1792,14 @@ policies_parse_exit_policy_reject_private( /* Reject public IPv4 addresses on any interface */ public_addresses = get_interface_address6_list(LOG_INFO, AF_INET, 0); addr_policy_append_reject_addr_list_filter(dest, public_addresses, 1, 0); - free_interface_address6_list(public_addresses); + interface_address6_list_free(public_addresses); /* Don't look for IPv6 addresses if we're configured as IPv4-only */ if (ipv6_exit) { /* Reject public IPv6 addresses on any interface */ public_addresses = get_interface_address6_list(LOG_INFO, AF_INET6, 0); addr_policy_append_reject_addr_list_filter(dest, public_addresses, 0, 1); - free_interface_address6_list(public_addresses); + interface_address6_list_free(public_addresses); } } @@ -2794,7 +2794,7 @@ write_short_policy(const short_policy_t *policy) /** Release all storage held in <b>policy</b>. */ void -short_policy_free(short_policy_t *policy) +short_policy_free_(short_policy_t *policy) { tor_free(policy); } @@ -3044,7 +3044,7 @@ getinfo_helper_policies(control_connection_t *conn, /** Release all storage held by <b>p</b>. */ void -addr_policy_list_free(smartlist_t *lst) +addr_policy_list_free_(smartlist_t *lst) { if (!lst) return; @@ -3054,7 +3054,7 @@ addr_policy_list_free(smartlist_t *lst) /** Release all storage held by <b>p</b>. */ void -addr_policy_free(addr_policy_t *p) +addr_policy_free_(addr_policy_t *p) { if (!p) return; diff --git a/src/or/policies.h b/src/or/policies.h index cd97ee7f59..35220a812f 100644 --- a/src/or/policies.h +++ b/src/or/policies.h @@ -115,15 +115,21 @@ int getinfo_helper_policies(control_connection_t *conn, int policy_write_item(char *buf, size_t buflen, const addr_policy_t *item, int format_for_desc); -void addr_policy_list_free(smartlist_t *p); -void addr_policy_free(addr_policy_t *p); +void addr_policy_list_free_(smartlist_t *p); +#define addr_policy_list_free(lst) \ + FREE_AND_NULL(smartlist_t, addr_policy_list_free_, (lst)) +void addr_policy_free_(addr_policy_t *p); +#define addr_policy_free(p) \ + FREE_AND_NULL(addr_policy_t, addr_policy_free_, (p)) void policies_free_all(void); char *policy_summarize(smartlist_t *policy, sa_family_t family); short_policy_t *parse_short_policy(const char *summary); char *write_short_policy(const short_policy_t *policy); -void short_policy_free(short_policy_t *policy); +void short_policy_free_(short_policy_t *policy); +#define short_policy_free(p) \ + FREE_AND_NULL(short_policy_t, short_policy_free_, (p)) int short_policy_is_reject_star(const short_policy_t *policy); addr_policy_result_t compare_tor_addr_to_short_policy( const tor_addr_t *addr, uint16_t port, diff --git a/src/or/proto_socks.c b/src/or/proto_socks.c index 7649fcc4be..91633d02af 100644 --- a/src/or/proto_socks.c +++ b/src/or/proto_socks.c @@ -66,7 +66,7 @@ socks_request_new(void) /** Free all storage held in the socks_request_t <b>req</b>. */ void -socks_request_free(socks_request_t *req) +socks_request_free_(socks_request_t *req) { if (!req) return; diff --git a/src/or/proto_socks.h b/src/or/proto_socks.h index a714151414..02e0aca7e9 100644 --- a/src/or/proto_socks.h +++ b/src/or/proto_socks.h @@ -11,7 +11,9 @@ struct socks_request_t; struct buf_t; struct socks_request_t *socks_request_new(void); -void socks_request_free(struct socks_request_t *req); +void socks_request_free_(struct socks_request_t *req); +#define socks_request_free(req) \ + FREE_AND_NULL(socks_request_t, socks_request_free_, (req)) int fetch_from_buf_socks(struct buf_t *buf, socks_request_t *req, int log_sockstype, int safe_socks); int fetch_from_buf_socks_client(buf_t *buf, int state, char **reason); diff --git a/src/or/protover.c b/src/or/protover.c index 748c0c2f50..1a5f4ac91f 100644 --- a/src/or/protover.c +++ b/src/or/protover.c @@ -96,7 +96,7 @@ str_to_protocol_type(const char *s, protocol_type_t *pr_out) * Release all space held by a single proto_entry_t structure */ STATIC void -proto_entry_free(proto_entry_t *entry) +proto_entry_free_(proto_entry_t *entry) { if (!entry) return; diff --git a/src/or/protover.h b/src/or/protover.h index 83a728e626..8bbc2fc716 100644 --- a/src/or/protover.h +++ b/src/or/protover.h @@ -82,7 +82,9 @@ STATIC smartlist_t *parse_protocol_list(const char *s); STATIC char *encode_protocol_list(const smartlist_t *sl); STATIC const char *protocol_type_to_str(protocol_type_t pr); STATIC int str_to_protocol_type(const char *s, protocol_type_t *pr_out); -STATIC void proto_entry_free(proto_entry_t *entry); +STATIC void proto_entry_free_(proto_entry_t *entry); +#define proto_entry_free(entry) \ + FREE_AND_NULL(proto_entry_t, proto_entry_free_, (entry)) #endif /* !defined(HAVE_RUST) && defined(TOR_UNIT_TESTS) */ diff --git a/src/or/relay.c b/src/or/relay.c index 0ad064417d..f6528c6ea5 100644 --- a/src/or/relay.c +++ b/src/or/relay.c @@ -1158,7 +1158,7 @@ connected_cell_parse(const relay_header_t *rh, const cell_t *cell, /** Drop all storage held by <b>addr</b>. */ STATIC void -address_ttl_free(address_ttl_t *addr) +address_ttl_free_(address_ttl_t *addr) { if (!addr) return; @@ -2425,7 +2425,7 @@ packed_cell_new(void) /** Return a packed cell used outside by channel_t lower layer */ void -packed_cell_free(packed_cell_t *cell) +packed_cell_free_(packed_cell_t *cell) { if (!cell) return; diff --git a/src/or/relay.h b/src/or/relay.h index 2412dcb23c..95b8beb3b5 100644 --- a/src/or/relay.h +++ b/src/or/relay.h @@ -51,7 +51,9 @@ size_t packed_cell_mem_cost(void); int have_been_under_memory_pressure(void); /* For channeltls.c */ -void packed_cell_free(packed_cell_t *cell); +void packed_cell_free_(packed_cell_t *cell); +#define packed_cell_free(cell) \ + FREE_AND_NULL(packed_cell_t, packed_cell_free_, (cell)) void cell_queue_init(cell_queue_t *queue); void cell_queue_clear(cell_queue_t *queue); @@ -94,7 +96,9 @@ typedef struct address_ttl_s { char *hostname; int ttl; } address_ttl_t; -STATIC void address_ttl_free(address_ttl_t *addr); +STATIC void address_ttl_free_(address_ttl_t *addr); +#define address_ttl_free(addr) \ + FREE_AND_NULL(address_ttl_t, address_ttl_free_, (addr)) STATIC int resolved_cell_parse(const cell_t *cell, const relay_header_t *rh, smartlist_t *addresses_out, int *errcode_out); STATIC int connection_edge_process_resolved_cell(edge_connection_t *conn, diff --git a/src/or/rendcache.c b/src/or/rendcache.c index b98b2bccfa..6f56df6718 100644 --- a/src/or/rendcache.c +++ b/src/or/rendcache.c @@ -120,7 +120,7 @@ rend_cache_increment_allocation(size_t n) /** Helper: free a rend cache failure intro object. */ STATIC void -rend_cache_failure_intro_entry_free(rend_cache_failure_intro_t *entry) +rend_cache_failure_intro_entry_free_(rend_cache_failure_intro_t *entry) { if (entry == NULL) { return; @@ -129,9 +129,9 @@ rend_cache_failure_intro_entry_free(rend_cache_failure_intro_t *entry) } static void -rend_cache_failure_intro_entry_free_(void *entry) +rend_cache_failure_intro_entry_free_void(void *entry) { - rend_cache_failure_intro_entry_free(entry); + rend_cache_failure_intro_entry_free_(entry); } /** Allocate a rend cache failure intro object and return it. <b>failure</b> @@ -147,7 +147,7 @@ rend_cache_failure_intro_entry_new(rend_intro_point_failure_t failure) /** Helper: free a rend cache failure object. */ STATIC void -rend_cache_failure_entry_free(rend_cache_failure_t *entry) +rend_cache_failure_entry_free_(rend_cache_failure_t *entry) { if (entry == NULL) { return; @@ -155,7 +155,7 @@ rend_cache_failure_entry_free(rend_cache_failure_t *entry) /* Free and remove every intro failure object. */ digestmap_free(entry->intro_failures, - rend_cache_failure_intro_entry_free_); + rend_cache_failure_intro_entry_free_void); tor_free(entry); } @@ -163,9 +163,9 @@ rend_cache_failure_entry_free(rend_cache_failure_t *entry) /** Helper: deallocate a rend_cache_failure_t. (Used with strmap_free(), * which requires a function pointer whose argument is void*). */ STATIC void -rend_cache_failure_entry_free_(void *entry) +rend_cache_failure_entry_free_void(void *entry) { - rend_cache_failure_entry_free(entry); + rend_cache_failure_entry_free_(entry); } /** Allocate a rend cache failure object and return it. This function can @@ -201,7 +201,7 @@ rend_cache_failure_remove(rend_service_descriptor_t *desc) /** Helper: free storage held by a single service descriptor cache entry. */ STATIC void -rend_cache_entry_free(rend_cache_entry_t *e) +rend_cache_entry_free_(rend_cache_entry_t *e) { if (!e) return; @@ -217,19 +217,19 @@ rend_cache_entry_free(rend_cache_entry_t *e) /** Helper: deallocate a rend_cache_entry_t. (Used with strmap_free(), which * requires a function pointer whose argument is void*). */ static void -rend_cache_entry_free_(void *p) +rend_cache_entry_free_void(void *p) { - rend_cache_entry_free(p); + rend_cache_entry_free_(p); } /** Free all storage held by the service descriptor cache. */ void rend_cache_free_all(void) { - strmap_free(rend_cache, rend_cache_entry_free_); - digestmap_free(rend_cache_v2_dir, rend_cache_entry_free_); - strmap_free(rend_cache_local_service, rend_cache_entry_free_); - strmap_free(rend_cache_failure, rend_cache_failure_entry_free_); + strmap_free(rend_cache, rend_cache_entry_free_void); + digestmap_free(rend_cache_v2_dir, rend_cache_entry_free_void); + strmap_free(rend_cache_local_service, rend_cache_entry_free_void); + strmap_free(rend_cache_failure, rend_cache_failure_entry_free_void); rend_cache = NULL; rend_cache_v2_dir = NULL; rend_cache_local_service = NULL; @@ -304,7 +304,7 @@ rend_cache_purge(void) { if (rend_cache) { log_info(LD_REND, "Purging HS v2 descriptor cache"); - strmap_free(rend_cache, rend_cache_entry_free_); + strmap_free(rend_cache, rend_cache_entry_free_void); } rend_cache = strmap_new(); } @@ -316,7 +316,7 @@ rend_cache_failure_purge(void) { if (rend_cache_failure) { log_info(LD_REND, "Purging HS v2 failure cache"); - strmap_free(rend_cache_failure, rend_cache_failure_entry_free_); + strmap_free(rend_cache_failure, rend_cache_failure_entry_free_void); } rend_cache_failure = strmap_new(); } diff --git a/src/or/rendcache.h b/src/or/rendcache.h index 5b13eadfa1..66e9785645 100644 --- a/src/or/rendcache.h +++ b/src/or/rendcache.h @@ -90,10 +90,18 @@ void rend_cache_increment_allocation(size_t n); #ifdef RENDCACHE_PRIVATE STATIC size_t rend_cache_entry_allocation(const rend_cache_entry_t *e); -STATIC void rend_cache_entry_free(rend_cache_entry_t *e); -STATIC void rend_cache_failure_intro_entry_free(rend_cache_failure_intro_t - *entry); -STATIC void rend_cache_failure_entry_free(rend_cache_failure_t *entry); +STATIC void rend_cache_entry_free_(rend_cache_entry_t *e); +#define rend_cache_entry_free(e) \ + FREE_AND_NULL(rend_cache_entry_t, rend_cache_entry_free_, (e)) +STATIC void rend_cache_failure_intro_entry_free_(rend_cache_failure_intro_t + *entry); +#define rend_cache_failure_intro_entry_free(e) \ + FREE_AND_NULL(rend_cache_failure_intro_t, \ + rend_cache_failure_intro_entry_free_, (e)) +STATIC void rend_cache_failure_entry_free_(rend_cache_failure_t *entry); +#define rend_cache_failure_entry_free(e) \ + FREE_AND_NULL(rend_cache_failure_t, \ + rend_cache_failure_entry_free_, (e)) STATIC int cache_failure_intro_lookup(const uint8_t *identity, const char *service_id, rend_cache_failure_intro_t @@ -108,7 +116,7 @@ STATIC void cache_failure_intro_add(const uint8_t *identity, STATIC void validate_intro_point_failure(const rend_service_descriptor_t *desc, const char *service_id); -STATIC void rend_cache_failure_entry_free_(void *entry); +STATIC void rend_cache_failure_entry_free_void(void *entry); #ifdef TOR_UNIT_TESTS extern strmap_t *rend_cache; diff --git a/src/or/rendclient.c b/src/or/rendclient.c index 8291e5abfb..07fa33306d 100644 --- a/src/or/rendclient.c +++ b/src/or/rendclient.c @@ -1104,18 +1104,22 @@ rend_client_lookup_service_authorization(const char *onion_address) return strmap_get(auth_hid_servs, onion_address); } +#define rend_service_authorization_free(val) \ + FREE_AND_NULL(rend_service_authorization_t, \ + rend_service_authorization_free_, (val)) + /** Helper: Free storage held by rend_service_authorization_t. */ static void -rend_service_authorization_free(rend_service_authorization_t *auth) +rend_service_authorization_free_(rend_service_authorization_t *auth) { tor_free(auth); } /** Helper for strmap_free. */ static void -rend_service_authorization_strmap_item_free(void *service_auth) +rend_service_authorization_free_void(void *service_auth) { - rend_service_authorization_free(service_auth); + rend_service_authorization_free_(service_auth); } /** Release all the storage held in auth_hid_servs. @@ -1126,7 +1130,7 @@ rend_service_authorization_free_all(void) if (!auth_hid_servs) { return; } - strmap_free(auth_hid_servs, rend_service_authorization_strmap_item_free); + strmap_free(auth_hid_servs, rend_service_authorization_free_void); auth_hid_servs = NULL; } @@ -1201,7 +1205,7 @@ rend_parse_service_authorization(const or_options_t *options, rend_service_authorization_free_all(); auth_hid_servs = parsed; } else { - strmap_free(parsed, rend_service_authorization_strmap_item_free); + strmap_free(parsed, rend_service_authorization_free_void); } return res; } diff --git a/src/or/rendcommon.c b/src/or/rendcommon.c index 458a90058f..230da4be5c 100644 --- a/src/or/rendcommon.c +++ b/src/or/rendcommon.c @@ -37,7 +37,7 @@ rend_cmp_service_ids(const char *one, const char *two) /** Free the storage held by the service descriptor <b>desc</b>. */ void -rend_service_descriptor_free(rend_service_descriptor_t *desc) +rend_service_descriptor_free_(rend_service_descriptor_t *desc) { if (!desc) return; @@ -419,7 +419,7 @@ rend_desc_v2_is_parsable(rend_encoded_v2_service_descriptor_t *desc) /** Free the storage held by an encoded v2 service descriptor. */ void -rend_encoded_v2_service_descriptor_free( +rend_encoded_v2_service_descriptor_free_( rend_encoded_v2_service_descriptor_t *desc) { if (!desc) @@ -430,7 +430,7 @@ rend_encoded_v2_service_descriptor_free( /** Free the storage held by an introduction point info. */ void -rend_intro_point_free(rend_intro_point_t *intro) +rend_intro_point_free_(rend_intro_point_t *intro) { if (!intro) return; diff --git a/src/or/rendcommon.h b/src/or/rendcommon.h index c35d7272fd..1ed0f62609 100644 --- a/src/or/rendcommon.h +++ b/src/or/rendcommon.h @@ -24,11 +24,19 @@ void rend_process_relay_cell(circuit_t *circ, const crypt_path_t *layer_hint, int command, size_t length, const uint8_t *payload); -void rend_service_descriptor_free(rend_service_descriptor_t *desc); +void rend_service_descriptor_free_(rend_service_descriptor_t *desc); +#define rend_service_descriptor_free(desc) \ + FREE_AND_NULL(rend_service_descriptor_t, rend_service_descriptor_free_, \ + (desc)) int rend_get_service_id(crypto_pk_t *pk, char *out); -void rend_encoded_v2_service_descriptor_free( +void rend_encoded_v2_service_descriptor_free_( rend_encoded_v2_service_descriptor_t *desc); -void rend_intro_point_free(rend_intro_point_t *intro); +#define rend_encoded_v2_service_descriptor_free(desc) \ + FREE_AND_NULL(rend_encoded_v2_service_descriptor_t, \ + rend_encoded_v2_service_descriptor_free_, (desc)) +void rend_intro_point_free_(rend_intro_point_t *intro); +#define rend_intro_point_free(intro) \ + FREE_AND_NULL(rend_intro_point_t, rend_intro_point_free_, (intro)) int rend_valid_v2_service_id(const char *query); int rend_valid_descriptor_id(const char *query); diff --git a/src/or/rendservice.c b/src/or/rendservice.c index 15a0017d66..06166d7cf3 100644 --- a/src/or/rendservice.c +++ b/src/or/rendservice.c @@ -157,7 +157,7 @@ rend_num_services(void) /** Helper: free storage held by a single service authorized client entry. */ void -rend_authorized_client_free(rend_authorized_client_t *client) +rend_authorized_client_free_(rend_authorized_client_t *client) { if (!client) return; @@ -172,15 +172,15 @@ rend_authorized_client_free(rend_authorized_client_t *client) /** Helper for strmap_free. */ static void -rend_authorized_client_strmap_item_free(void *authorized_client) +rend_authorized_client_free_void(void *authorized_client) { - rend_authorized_client_free(authorized_client); + rend_authorized_client_free_(authorized_client); } /** Release the storage held by <b>service</b>. */ STATIC void -rend_service_free(rend_service_t *service) +rend_service_free_(rend_service_t *service) { if (!service) return; @@ -470,7 +470,7 @@ rend_service_parse_port_config(const char *string, const char *sep, /** Release all storage held in a rend_service_port_config_t. */ void -rend_service_port_config_free(rend_service_port_config_t *p) +rend_service_port_config_free_(rend_service_port_config_t *p) { tor_free(p); } @@ -1675,7 +1675,7 @@ rend_service_load_auth_keys(rend_service_t *s, const char *hfname) memwipe(client_keys_str, 0, strlen(client_keys_str)); tor_free(client_keys_str); } - strmap_free(parsed_clients, rend_authorized_client_strmap_item_free); + strmap_free(parsed_clients, rend_authorized_client_free_void); if (cfname) { memwipe(cfname, 0, strlen(cfname)); @@ -2223,7 +2223,7 @@ find_rp_for_intro(const rend_intro_cell_t *intro, * rend_service_parse_intro(). */ void -rend_service_free_intro(rend_intro_cell_t *request) +rend_service_free_intro_(rend_intro_cell_t *request) { if (!request) { return; @@ -3728,7 +3728,7 @@ upload_service_descriptor(rend_service_t *service) } /* Free memory for descriptors. */ for (i = 0; i < smartlist_len(descs); i++) - rend_encoded_v2_service_descriptor_free(smartlist_get(descs, i)); + rend_encoded_v2_service_descriptor_free_(smartlist_get(descs, i)); smartlist_clear(descs); /* Update next upload time. */ if (seconds_valid - REND_TIME_PERIOD_OVERLAPPING_V2_DESCS @@ -3759,7 +3759,7 @@ upload_service_descriptor(rend_service_t *service) } /* Free memory for descriptors. */ for (i = 0; i < smartlist_len(descs); i++) - rend_encoded_v2_service_descriptor_free(smartlist_get(descs, i)); + rend_encoded_v2_service_descriptor_free_(smartlist_get(descs, i)); smartlist_clear(descs); } } diff --git a/src/or/rendservice.h b/src/or/rendservice.h index 15badce6ab..88da7b8665 100644 --- a/src/or/rendservice.h +++ b/src/or/rendservice.h @@ -117,7 +117,9 @@ typedef struct rend_service_t { int max_streams_close_circuit; } rend_service_t; -STATIC void rend_service_free(rend_service_t *service); +STATIC void rend_service_free_(rend_service_t *service); +#define rend_service_free(s) \ + FREE_AND_NULL(rend_service_t, rend_service_free_, (s)) STATIC char *rend_service_sos_poison_path(const rend_service_t *service); STATIC int rend_service_verify_single_onion_poison( const rend_service_t *s, @@ -160,7 +162,11 @@ int rend_service_receive_introduction(origin_circuit_t *circuit, int rend_service_decrypt_intro(rend_intro_cell_t *request, crypto_pk_t *key, char **err_msg_out); -void rend_service_free_intro(rend_intro_cell_t *request); +void rend_service_free_intro_(rend_intro_cell_t *request); +#define rend_service_free_intro(req) do { \ + rend_service_free_intro_(req); \ + (req) = NULL; \ + } while (0) rend_intro_cell_t * rend_service_begin_parse_intro(const uint8_t *request, size_t request_len, uint8_t type, @@ -183,9 +189,15 @@ void rend_service_init(void); rend_service_port_config_t *rend_service_parse_port_config(const char *string, const char *sep, char **err_msg_out); -void rend_service_port_config_free(rend_service_port_config_t *p); - -void rend_authorized_client_free(rend_authorized_client_t *client); +void rend_service_port_config_free_(rend_service_port_config_t *p); +#define rend_service_port_config_free(p) \ + FREE_AND_NULL(rend_service_port_config_t, rend_service_port_config_free_, \ + (p)) + +void rend_authorized_client_free_(rend_authorized_client_t *client); +#define rend_authorized_client_free(client) \ + FREE_AND_NULL(rend_authorized_client_t, rend_authorized_client_free_, \ + (client)) hs_service_add_ephemeral_status_t rend_service_add_ephemeral(crypto_pk_t *pk, smartlist_t *ports, diff --git a/src/or/rephist.c b/src/or/rephist.c index 87b5a93713..de4635ee59 100644 --- a/src/or/rephist.c +++ b/src/or/rephist.c @@ -1358,9 +1358,12 @@ bw_array_new(void) return b; } +#define bw_array_free(val) \ + FREE_AND_NULL(bw_array_t, bw_array_free_, (val)) + /** Free storage held by bandwidth array <b>b</b>. */ static void -bw_array_free(bw_array_t *b) +bw_array_free_(bw_array_t *b) { if (!b) { return; @@ -1883,7 +1886,7 @@ predicted_ports_init(void) * be used. */ static void -predicted_ports_free(void) +predicted_ports_free_all(void) { rephist_total_alloc -= smartlist_len(predicted_ports_list)*sizeof(predicted_port_t); @@ -2828,7 +2831,7 @@ HT_GENERATE2(bidimap, bidi_map_entry_t, node, bidi_map_ent_hash, /* DOCDOC bidi_map_free */ static void -bidi_map_free(void) +bidi_map_free_all(void) { bidi_map_entry_t **ptr, **next, *ent; for (ptr = HT_START(bidimap, &bidi_map); ptr; ptr = next) { @@ -2848,7 +2851,7 @@ rep_hist_reset_conn_stats(time_t now) mostly_read = 0; mostly_written = 0; both_read_and_written = 0; - bidi_map_free(); + bidi_map_free_all(); } /** Stop collecting connection stats in a way that we can re-start doing @@ -3037,9 +3040,12 @@ hs_stats_new(void) return new_hs_stats; } +#define hs_stats_free(val) \ + FREE_AND_NULL(hs_stats_t, hs_stats_free_, (val)) + /** Free an hs_stats_t structure. */ static void -hs_stats_free(hs_stats_t *victim_hs_stats) +hs_stats_free_(hs_stats_t *victim_hs_stats) { if (!victim_hs_stats) { return; @@ -3451,8 +3457,8 @@ rep_hist_free_all(void) tor_free(exit_bytes_read); tor_free(exit_bytes_written); tor_free(exit_streams); - predicted_ports_free(); - bidi_map_free(); + predicted_ports_free_all(); + bidi_map_free_all(); if (circuits_for_buffer_stats) { SMARTLIST_FOREACH(circuits_for_buffer_stats, circ_buffer_stats_t *, s, diff --git a/src/or/replaycache.c b/src/or/replaycache.c index 3d42deb90a..4a56bfd7d4 100644 --- a/src/or/replaycache.c +++ b/src/or/replaycache.c @@ -28,7 +28,7 @@ */ void -replaycache_free(replaycache_t *r) +replaycache_free_(replaycache_t *r) { if (!r) { log_info(LD_BUG, "replaycache_free() called on NULL"); diff --git a/src/or/replaycache.h b/src/or/replaycache.h index 1cae3497ae..81a8d907fd 100644 --- a/src/or/replaycache.h +++ b/src/or/replaycache.h @@ -33,7 +33,9 @@ struct replaycache_s { /* replaycache_t free/new */ -void replaycache_free(replaycache_t *r); +void replaycache_free_(replaycache_t *r); +#define replaycache_free(r) \ + FREE_AND_NULL(replaycache_t, replaycache_free_, (r)) replaycache_t * replaycache_new(time_t horizon, time_t interval); #ifdef REPLAYCACHE_PRIVATE diff --git a/src/or/router.c b/src/or/router.c index cd1848ee7b..9c053cad46 100644 --- a/src/or/router.c +++ b/src/or/router.c @@ -233,7 +233,7 @@ ntor_key_map_free_helper(void *arg) } /** Release all storage from a keymap returned by construct_ntor_key_map. */ void -ntor_key_map_free(di_digest256_map_t *map) +ntor_key_map_free_(di_digest256_map_t *map) { if (!map) return; @@ -3685,6 +3685,7 @@ router_free_all(void) crypto_pk_free(lastonionkey); crypto_pk_free(server_identitykey); crypto_pk_free(client_identitykey); + tor_mutex_free(key_lock); routerinfo_free(desc_routerinfo); extrainfo_free(desc_extrainfo); diff --git a/src/or/router.h b/src/or/router.h index 3351400911..696e983662 100644 --- a/src/or/router.h +++ b/src/or/router.h @@ -36,7 +36,9 @@ int get_onion_key_lifetime(void); int get_onion_key_grace_period(void); di_digest256_map_t *construct_ntor_key_map(void); -void ntor_key_map_free(di_digest256_map_t *map); +void ntor_key_map_free_(di_digest256_map_t *map); +#define ntor_key_map_free(map) \ + FREE_AND_NULL(di_digest256_map_t, ntor_key_map_free_, (map)) int router_initialize_tls_context(void); int init_keys(void); diff --git a/src/or/routerlist.c b/src/or/routerlist.c index 93c75ef583..d8c8a81738 100644 --- a/src/or/routerlist.c +++ b/src/or/routerlist.c @@ -143,6 +143,10 @@ DECLARE_TYPED_DIGESTMAP_FNS(dsmap_, digest_ds_map_t, download_status_t) #define DSMAP_FOREACH(map, keyvar, valvar) \ DIGESTMAP_FOREACH(dsmap_to_digestmap(map), keyvar, download_status_t *, \ valvar) +#define eimap_free(map, fn) MAP_FREE_AND_NULL(eimap, (map), (fn)) +#define rimap_free(map, fn) MAP_FREE_AND_NULL(rimap, (map), (fn)) +#define dsmap_free(map, fn) MAP_FREE_AND_NULL(dsmap, (map), (fn)) +#define sdmap_free(map, fn) MAP_FREE_AND_NULL(sdmap, (map), (fn)) /* Forward declaration for cert_list_t */ typedef struct cert_list_t cert_list_t; @@ -159,7 +163,6 @@ static const routerstatus_t *router_pick_dirserver_generic( smartlist_t *sourcelist, dirinfo_type_t type, int flags); static void mark_all_dirservers_up(smartlist_t *server_list); -static void dir_server_free(dir_server_t *ds); static int signed_desc_digest_is_recognized(signed_descriptor_t *desc); static const char *signed_descriptor_get_body_impl( const signed_descriptor_t *desc, @@ -443,9 +446,12 @@ download_status_for_authority_id_and_sk,(const char *id_digest, return dl; } +#define cert_list_free(val) \ + FREE_AND_NULL(cert_list_t, cert_list_free_, (val)) + /** Release all space held by a cert_list_t */ static void -cert_list_free(cert_list_t *cl) +cert_list_free_(cert_list_t *cl) { if (!cl) return; @@ -459,9 +465,9 @@ cert_list_free(cert_list_t *cl) /** Wrapper for cert_list_free so we can pass it to digestmap_free */ static void -cert_list_free_(void *cl) +cert_list_free_void(void *cl) { - cert_list_free(cl); + cert_list_free_(cl); } /** Reload the cached v3 key certificates from the cached-certs file in @@ -3167,7 +3173,7 @@ router_get_routerlist(void) /** Free all storage held by <b>router</b>. */ void -routerinfo_free(routerinfo_t *router) +routerinfo_free_(routerinfo_t *router) { if (!router) return; @@ -3197,7 +3203,7 @@ routerinfo_free(routerinfo_t *router) /** Release all storage held by <b>extrainfo</b> */ void -extrainfo_free(extrainfo_t *extrainfo) +extrainfo_free_(extrainfo_t *extrainfo) { if (!extrainfo) return; @@ -3209,9 +3215,12 @@ extrainfo_free(extrainfo_t *extrainfo) tor_free(extrainfo); } +#define signed_descriptor_free(val) \ + FREE_AND_NULL(signed_descriptor_t, signed_descriptor_free_, (val)) + /** Release storage held by <b>sd</b>. */ static void -signed_descriptor_free(signed_descriptor_t *sd) +signed_descriptor_free_(signed_descriptor_t *sd) { if (!sd) return; @@ -3265,21 +3274,21 @@ signed_descriptor_from_routerinfo(routerinfo_t *ri) /** Helper: free the storage held by the extrainfo_t in <b>e</b>. */ static void -extrainfo_free_(void *e) +extrainfo_free_void(void *e) { - extrainfo_free(e); + extrainfo_free_(e); } /** Free all storage held by a routerlist <b>rl</b>. */ void -routerlist_free(routerlist_t *rl) +routerlist_free_(routerlist_t *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); - eimap_free(rl->extra_info_map, extrainfo_free_); + eimap_free(rl->extra_info_map, extrainfo_free_void); SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r, routerinfo_free(r)); SMARTLIST_FOREACH(rl->old_routers, signed_descriptor_t *, sd, @@ -3771,7 +3780,7 @@ routerlist_free_all(void) smartlist_free(fallback_dir_servers); trusted_dir_servers = fallback_dir_servers = NULL; if (trusted_dir_certs) { - digestmap_free(trusted_dir_certs, cert_list_free_); + digestmap_free(trusted_dir_certs, cert_list_free_void); trusted_dir_certs = NULL; } } @@ -4739,7 +4748,7 @@ dir_server_add(dir_server_t *ent) /** Free storage held in <b>cert</b>. */ void -authority_cert_free(authority_cert_t *cert) +authority_cert_free_(authority_cert_t *cert) { if (!cert) return; @@ -4751,9 +4760,12 @@ authority_cert_free(authority_cert_t *cert) tor_free(cert); } +#define dir_server_free(val) \ + FREE_AND_NULL(dir_server_t, dir_server_free_, (val)) + /** Free storage held in <b>ds</b>. */ static void -dir_server_free(dir_server_t *ds) +dir_server_free_(dir_server_t *ds) { if (!ds) return; diff --git a/src/or/routerlist.h b/src/or/routerlist.h index 8384c7eb8c..83f4d1002f 100644 --- a/src/or/routerlist.h +++ b/src/or/routerlist.h @@ -96,9 +96,13 @@ MOCK_DECL(signed_descriptor_t *,extrainfo_get_by_descriptor_digest, const char *signed_descriptor_get_body(const signed_descriptor_t *desc); const char *signed_descriptor_get_annotations(const signed_descriptor_t *desc); routerlist_t *router_get_routerlist(void); -void routerinfo_free(routerinfo_t *router); -void extrainfo_free(extrainfo_t *extrainfo); -void routerlist_free(routerlist_t *rl); +void routerinfo_free_(routerinfo_t *router); +#define routerinfo_free(router) \ + FREE_AND_NULL(routerinfo_t, routerinfo_free_, (router)) +void extrainfo_free_(extrainfo_t *extrainfo); +#define extrainfo_free(ei) FREE_AND_NULL(extrainfo_t, extrainfo_free_, (ei)) +void routerlist_free_(routerlist_t *rl); +#define routerlist_free(rl) FREE_AND_NULL(routerlist_t, routerlist_free_, (rl)) void dump_routerlist_mem_usage(int severity); void routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old, time_t now); @@ -191,7 +195,9 @@ dir_server_t *fallback_dir_server_new(const tor_addr_t *addr, const char *id_digest, double weight); void dir_server_add(dir_server_t *ent); -void authority_cert_free(authority_cert_t *cert); +void authority_cert_free_(authority_cert_t *cert); +#define authority_cert_free(cert) \ + FREE_AND_NULL(authority_cert_t, authority_cert_free_, (cert)) void clear_dir_servers(void); void update_consensus_router_descriptor_downloads(time_t now, int is_vote, networkstatus_t *consensus); diff --git a/src/or/routerset.c b/src/or/routerset.c index 54e26ef943..a2599b316c 100644 --- a/src/or/routerset.c +++ b/src/or/routerset.c @@ -437,7 +437,7 @@ routerset_equal(const routerset_t *old, const routerset_t *new) /** Free all storage held in <b>routerset</b>. */ void -routerset_free(routerset_t *routerset) +routerset_free_(routerset_t *routerset) { if (!routerset) return; diff --git a/src/or/routerset.h b/src/or/routerset.h index d8819ef3fd..53e8c66c5e 100644 --- a/src/or/routerset.h +++ b/src/or/routerset.h @@ -40,7 +40,8 @@ void routerset_subtract_nodes(smartlist_t *out, char *routerset_to_string(const routerset_t *routerset); int routerset_equal(const routerset_t *old, const routerset_t *new); -void routerset_free(routerset_t *routerset); +void routerset_free_(routerset_t *routerset); +#define routerset_free(rs) FREE_AND_NULL(routerset_t, routerset_free_, (rs)) int routerset_len(const routerset_t *set); #ifdef ROUTERSET_PRIVATE diff --git a/src/or/scheduler_kist.c b/src/or/scheduler_kist.c index 3d8f553ac2..e02926e478 100644 --- a/src/or/scheduler_kist.c +++ b/src/or/scheduler_kist.c @@ -461,7 +461,7 @@ kist_free_all(void) /* Function of the scheduler interface: on_channel_free() */ static void -kist_on_channel_free(const channel_t *chan) +kist_on_channel_free_fn(const channel_t *chan) { free_socket_info_by_chan(&socket_table, chan); } @@ -731,7 +731,7 @@ kist_scheduler_run(void) static scheduler_t kist_scheduler = { .type = SCHEDULER_KIST, .free_all = kist_free_all, - .on_channel_free = kist_on_channel_free, + .on_channel_free = kist_on_channel_free_fn, .init = kist_scheduler_init, .on_new_consensus = kist_scheduler_on_new_consensus, .schedule = kist_scheduler_schedule, diff --git a/src/or/shared_random.c b/src/or/shared_random.c index b3f62a8fd8..7723ad4611 100644 --- a/src/or/shared_random.c +++ b/src/or/shared_random.c @@ -384,7 +384,7 @@ commit_encode(const sr_commit_t *commit, char *dst, size_t len) static void sr_cleanup(void) { - sr_state_free(); + sr_state_free_all(); } /* Using <b>commit</b>, return a newly allocated string containing the commit @@ -897,7 +897,7 @@ sr_srv_encode(char *dst, size_t dst_len, const sr_srv_t *srv) /* Free a commit object. */ void -sr_commit_free(sr_commit_t *commit) +sr_commit_free_(sr_commit_t *commit) { if (commit == NULL) { return; diff --git a/src/or/shared_random.h b/src/or/shared_random.h index c0992489cb..675a8d8b06 100644 --- a/src/or/shared_random.h +++ b/src/or/shared_random.h @@ -113,7 +113,8 @@ sr_srv_t *sr_parse_srv(const smartlist_t *args); char *sr_get_string_for_vote(void); char *sr_get_string_for_consensus(const smartlist_t *votes, int32_t num_srv_agreements); -void sr_commit_free(sr_commit_t *commit); +void sr_commit_free_(sr_commit_t *commit); +#define sr_commit_free(sr) FREE_AND_NULL(sr_commit_t, sr_commit_free_, (sr)) void sr_srv_encode(char *dst, size_t dst_len, const sr_srv_t *srv); /* Private methods (only used by shared_random_state.c): */ diff --git a/src/or/shared_random_state.c b/src/or/shared_random_state.c index ae904cfda3..d6109b2de7 100644 --- a/src/or/shared_random_state.c +++ b/src/or/shared_random_state.c @@ -271,12 +271,15 @@ commit_add_to_state(sr_commit_t *commit, sr_state_t *state) static void commit_free_(void *p) { - sr_commit_free(p); + sr_commit_free_(p); } +#define state_free(val) \ + FREE_AND_NULL(sr_state_t, state_free_, (val)) + /* Free a state that was allocated with state_new(). */ static void -state_free(sr_state_t *state) +state_free_(sr_state_t *state) { if (state == NULL) { return; @@ -318,9 +321,12 @@ state_set(sr_state_t *state) sr_state = state; } +#define disk_state_free(val) \ + FREE_AND_NULL(sr_disk_state_t, disk_state_free_, (val)) + /* Free an allocated disk state. */ static void -disk_state_free(sr_disk_state_t *state) +disk_state_free_(sr_disk_state_t *state) { if (state == NULL) { return; @@ -1286,7 +1292,7 @@ sr_state_srv_is_fresh(void) /* Cleanup and free our disk and memory state. */ void -sr_state_free(void) +sr_state_free_all(void) { state_free(sr_state); disk_state_free(sr_disk_state); diff --git a/src/or/shared_random_state.h b/src/or/shared_random_state.h index 866725c435..fdbbf4919a 100644 --- a/src/or/shared_random_state.h +++ b/src/or/shared_random_state.h @@ -119,7 +119,7 @@ void sr_state_unset_fresh_srv(void); int sr_state_init(int save_to_disk, int read_from_disk); int sr_state_is_initialized(void); void sr_state_save(void); -void sr_state_free(void); +void sr_state_free_all(void); time_t sr_state_get_start_time_of_current_protocol_run(time_t now); unsigned int sr_state_get_phase_duration(void); diff --git a/src/or/statefile.c b/src/or/statefile.c index 97bd9cac36..cc114f0a2b 100644 --- a/src/or/statefile.c +++ b/src/or/statefile.c @@ -681,7 +681,7 @@ save_transport_to_state(const char *transport, } STATIC void -or_state_free(or_state_t *state) +or_state_free_(or_state_t *state) { if (!state) return; diff --git a/src/or/statefile.h b/src/or/statefile.h index 574afb3622..b4cc4d1dc6 100644 --- a/src/or/statefile.h +++ b/src/or/statefile.h @@ -20,7 +20,8 @@ void or_state_free_all(void); #ifdef STATEFILE_PRIVATE STATIC config_line_t *get_transport_in_state_by_name(const char *transport); -STATIC void or_state_free(or_state_t *state); +STATIC void or_state_free_(or_state_t *state); +#define or_state_free(st) FREE_AND_NULL(or_state_t, or_state_free_, (st)) STATIC or_state_t *or_state_new(void); #endif diff --git a/src/or/torcert.c b/src/or/torcert.c index befb39d6e8..bd677d1f4a 100644 --- a/src/or/torcert.c +++ b/src/or/torcert.c @@ -137,7 +137,7 @@ tor_cert_create(const ed25519_keypair_t *signing_key, /** Release all storage held for <b>cert</b>. */ void -tor_cert_free(tor_cert_t *cert) +tor_cert_free_(tor_cert_t *cert) { if (! cert) return; @@ -453,7 +453,7 @@ or_handshake_certs_new(void) /** Release all storage held in <b>certs</b> */ void -or_handshake_certs_free(or_handshake_certs_t *certs) +or_handshake_certs_free_(or_handshake_certs_t *certs) { if (!certs) return; diff --git a/src/or/torcert.h b/src/or/torcert.h index c77ae2089d..0a8a252049 100644 --- a/src/or/torcert.h +++ b/src/or/torcert.h @@ -57,7 +57,8 @@ tor_cert_t *tor_cert_create(const ed25519_keypair_t *signing_key, tor_cert_t *tor_cert_parse(const uint8_t *cert, size_t certlen); -void tor_cert_free(tor_cert_t *cert); +void tor_cert_free_(tor_cert_t *cert); +#define tor_cert_free(cert) FREE_AND_NULL(tor_cert_t, tor_cert_free_, (cert)) int tor_cert_get_checkable_sig(ed25519_checkable_t *checkable_out, const tor_cert_t *out, @@ -83,7 +84,9 @@ rsa_ed25519_crosscert_check, (const uint8_t *crosscert, const time_t reject_if_expired_before)); or_handshake_certs_t *or_handshake_certs_new(void); -void or_handshake_certs_free(or_handshake_certs_t *certs); +void or_handshake_certs_free_(or_handshake_certs_t *certs); +#define or_handshake_certs_free(certs) \ + FREE_AND_NULL(or_handshake_certs_t, or_handshake_certs_free_, (certs)) int or_handshake_certs_rsa_ok(int severity, or_handshake_certs_t *certs, tor_tls_t *tls, diff --git a/src/or/transports.c b/src/or/transports.c index 68d1354844..04b24b0f81 100644 --- a/src/or/transports.c +++ b/src/or/transports.c @@ -154,7 +154,7 @@ transport_new(const tor_addr_t *addr, uint16_t port, /** Free the pluggable transport struct <b>transport</b>. */ void -transport_free(transport_t *transport) +transport_free_(transport_t *transport) { if (!transport) return; diff --git a/src/or/transports.h b/src/or/transports.h index e368e447c3..1b2786472c 100644 --- a/src/or/transports.h +++ b/src/or/transports.h @@ -35,7 +35,8 @@ void sweep_transport_list(void); MOCK_DECL(int, transport_add_from_config, (const tor_addr_t *addr, uint16_t port, const char *name, int socks_ver)); -void transport_free(transport_t *transport); +void transport_free_(transport_t *transport); +#define transport_free(tr) FREE_AND_NULL(transport_t, transport_free_, (tr)) transport_t *transport_get_by_name(const char *name); |