diff --git a/changes/bug24337 b/changes/bug24337 new file mode 100644 index 0000000000..82b430425d --- /dev/null +++ b/changes/bug24337 @@ -0,0 +1,8 @@ + o Minor features (defensive programming): + - Most of the functions in Tor that free objects have been replaced + with macros that free the objects and set the corresponding pointers + to NULL. This change should help prevent a large class of dangling + pointer bugs. Closes ticket 24337. + + - Where possible, the tor_free() macro now only evaluates its input once. + Part of ticket 24337. diff --git a/configure.ac b/configure.ac index 0b3e1dda26..82e656d3c5 100644 --- a/configure.ac +++ b/configure.ac @@ -1991,7 +1991,6 @@ if test "x$enable_gcc_warnings_advisory" != "xno"; then -Winvalid-source-encoding -Winvalid-token-paste -Wknr-promoted-parameter - -Wlanguage-extension-token -Wlarge-by-value-copy -Wliteral-conversion -Wliteral-range diff --git a/doc/HACKING/CodingStandards.md b/doc/HACKING/CodingStandards.md index dd21d6fd2c..4b1bf4718c 100644 --- a/doc/HACKING/CodingStandards.md +++ b/doc/HACKING/CodingStandards.md @@ -346,6 +346,46 @@ macro, as in: if (BUG(ptr == NULL)) return -1; +Allocator conventions +--------------------- + +By convention, any tor type with a name like `abc_t` should be allocated +by a function named `abc_new()`. This function should never return +NULL. + +Also, a type named `abc_t` should be freed by a function named `abc_free_()`. +Don't call this `abc_free_()` function directly -- instead, wrap it in a +macro called `abc_free()`, using the `FREE_AND_NULL` macro: + + void abc_free_(abc_t *obj); + #define abc_free(obj) FREE_AND_NULL(abc_t, abc_free_, (abc)) + +This macro will free the underlying `abc_t` object, and will also set +the object pointer to NULL. + +You should define all `abc_free_()` functions to accept NULL inputs: + + void + abc_free_(abc_t *obj) + { + if (!obj) + return; + tor_free(obj->name); + thing_free(obj->thing); + tor_free(obj); + } + +If you need a free function that takes a `void *` argument (for example, +to use it as a function callback), define it with a name like +`abc_free_void()`: + + static void + abc_free_void_(void *obj) + { + abc_free_(obj); + } + + Doxygen comment conventions --------------------------- diff --git a/src/common/address.c b/src/common/address.c index dbe129be59..0c0ba782ae 100644 --- a/src/common/address.c +++ b/src/common/address.c @@ -1759,14 +1759,14 @@ get_interface_address6,(int severity, sa_family_t family, tor_addr_t *addr)) break; } SMARTLIST_FOREACH_END(a); - free_interface_address6_list(addrs); + interface_address6_list_free(addrs); return rv; } /** Free a smartlist of IP addresses returned by get_interface_address6_list. */ void -free_interface_address6_list(smartlist_t *addrs) +interface_address6_list_free_(smartlist_t *addrs) { if (addrs != NULL) { SMARTLIST_FOREACH(addrs, tor_addr_t *, a, tor_free(a)); @@ -1781,7 +1781,7 @@ free_interface_address6_list(smartlist_t *addrs) * An empty smartlist means that there are no addresses of the selected type * matching these criteria. * Returns NULL on failure. - * Use free_interface_address6_list to free the returned list. + * Use interface_address6_list_free to free the returned list. */ MOCK_IMPL(smartlist_t *, get_interface_address6_list,(int severity, diff --git a/src/common/address.h b/src/common/address.h index c7f6935754..7607c76bae 100644 --- a/src/common/address.h +++ b/src/common/address.h @@ -206,7 +206,9 @@ const char * fmt_addr32(uint32_t addr); MOCK_DECL(int,get_interface_address6,(int severity, sa_family_t family, tor_addr_t *addr)); -void free_interface_address6_list(smartlist_t * addrs); +void interface_address6_list_free_(smartlist_t * addrs);// XXXX +#define interface_address6_list_free(addrs) \ + FREE_AND_NULL(smartlist_t, interface_address6_list_free_, (addrs)) MOCK_DECL(smartlist_t *,get_interface_address6_list,(int severity, sa_family_t family, int include_internal)); @@ -321,13 +323,8 @@ int addr_mask_get_bits(uint32_t mask); int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len); char *tor_dup_ip(uint32_t addr) ATTR_MALLOC; MOCK_DECL(int,get_interface_address,(int severity, uint32_t *addr)); -/** Free a smartlist of IP addresses returned by get_interface_address_list. - */ -static inline void -free_interface_address_list(smartlist_t *addrs) -{ - free_interface_address6_list(addrs); -} +#define interface_address_list_free(lst)\ + interface_address6_list_free(lst) /** Return a smartlist of the IPv4 addresses of all interfaces on the server. * Excludes loopback and multicast addresses. Only includes internal addresses * if include_internal is true. (Note that a relay behind NAT may use an diff --git a/src/common/aes.c b/src/common/aes.c index df4368fdba..9b5b0197ea 100644 --- a/src/common/aes.c +++ b/src/common/aes.c @@ -110,7 +110,7 @@ aes_new_cipher(const uint8_t *key, const uint8_t *iv, int key_bits) return (aes_cnt_cipher_t *) cipher; } void -aes_cipher_free(aes_cnt_cipher_t *cipher_) +aes_cipher_free_(aes_cnt_cipher_t *cipher_) { if (!cipher_) return; @@ -324,7 +324,7 @@ aes_set_key(aes_cnt_cipher_t *cipher, const uint8_t *key, int key_bits) /** Release storage held by cipher */ void -aes_cipher_free(aes_cnt_cipher_t *cipher) +aes_cipher_free_(aes_cnt_cipher_t *cipher) { if (!cipher) return; diff --git a/src/common/aes.h b/src/common/aes.h index 1e400a56e0..0b17cd55a4 100644 --- a/src/common/aes.h +++ b/src/common/aes.h @@ -17,7 +17,9 @@ typedef struct aes_cnt_cipher aes_cnt_cipher_t; aes_cnt_cipher_t* aes_new_cipher(const uint8_t *key, const uint8_t *iv, int key_bits); -void aes_cipher_free(aes_cnt_cipher_t *cipher); +void aes_cipher_free_(aes_cnt_cipher_t *cipher); +#define aes_cipher_free(cipher) \ + FREE_AND_NULL(aes_cnt_cipher_t, aes_cipher_free_, (cipher)) void aes_crypt_inplace(aes_cnt_cipher_t *cipher, char *data, size_t len); int evaluate_evp_for_aes(int force_value); diff --git a/src/common/buffers.c b/src/common/buffers.c index e5d3664638..a01add9bef 100644 --- a/src/common/buffers.c +++ b/src/common/buffers.c @@ -409,7 +409,7 @@ buf_slack(const buf_t *buf) /** Release storage held by buf. */ void -buf_free(buf_t *buf) +buf_free_(buf_t *buf) { if (!buf) return; diff --git a/src/common/buffers.h b/src/common/buffers.h index 8e938eec38..22a5f7bfa3 100644 --- a/src/common/buffers.h +++ b/src/common/buffers.h @@ -24,7 +24,8 @@ struct tor_compress_state_t; buf_t *buf_new(void); buf_t *buf_new_with_capacity(size_t size); size_t buf_get_default_chunk_size(const buf_t *buf); -void buf_free(buf_t *buf); +void buf_free_(buf_t *buf); +#define buf_free(b) FREE_AND_NULL(buf_t, buf_free_, (b)) void buf_clear(buf_t *buf); buf_t *buf_copy(const buf_t *buf); diff --git a/src/common/compat.c b/src/common/compat.c index 38693b21fe..b4bd6eba06 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -1905,9 +1905,12 @@ tor_passwd_dup(const struct passwd *pw) return new_pw; } +#define tor_passwd_free(pw) \ + FREE_AND_NULL(struct passwd, tor_passwd_free_, (pw)) + /** Helper: free one of our cached 'struct passwd' values. */ static void -tor_passwd_free(struct passwd *pw) +tor_passwd_free_(struct passwd *pw) { if (!pw) return; diff --git a/src/common/compat_libevent.c b/src/common/compat_libevent.c index 1c3a1b9f37..10489bf296 100644 --- a/src/common/compat_libevent.c +++ b/src/common/compat_libevent.c @@ -69,7 +69,7 @@ suppress_libevent_log_msg(const char *msg) /* Wrapper for event_free() that tolerates tor_event_free(NULL) */ void -tor_event_free(struct event *ev) +tor_event_free_(struct event *ev) { if (ev == NULL) return; @@ -213,7 +213,7 @@ periodic_timer_new(struct event_base *base, /** Stop and free a periodic timer */ void -periodic_timer_free(periodic_timer_t *timer) +periodic_timer_free_(periodic_timer_t *timer) { if (!timer) return; diff --git a/src/common/compat_libevent.h b/src/common/compat_libevent.h index 3d8672fc63..0cdb73fbb9 100644 --- a/src/common/compat_libevent.h +++ b/src/common/compat_libevent.h @@ -19,7 +19,9 @@ void suppress_libevent_log_msg(const char *msg); evdns_add_server_port_with_base(tor_libevent_get_base(), \ (sock),(tcp),(cb),(data)); -void tor_event_free(struct event *ev); +void tor_event_free_(struct event *ev); +#define tor_event_free(ev) \ + FREE_AND_NULL(struct event, tor_event_free_, (ev)) typedef struct periodic_timer_t periodic_timer_t; @@ -27,7 +29,9 @@ periodic_timer_t *periodic_timer_new(struct event_base *base, const struct timeval *tv, void (*cb)(periodic_timer_t *timer, void *data), void *data); -void periodic_timer_free(periodic_timer_t *); +void periodic_timer_free_(periodic_timer_t *); +#define periodic_timer_free(t) \ + FREE_AND_NULL(periodic_timer_t, periodic_timer_free_, (t)) #define tor_event_base_loopexit event_base_loopexit #define tor_event_base_loopbreak event_base_loopbreak diff --git a/src/common/compat_threads.c b/src/common/compat_threads.c index 6adcaab956..3171c4b2f2 100644 --- a/src/common/compat_threads.c +++ b/src/common/compat_threads.c @@ -48,7 +48,7 @@ tor_mutex_new_nonrecursive(void) } /** Release all storage and system resources held by m. */ void -tor_mutex_free(tor_mutex_t *m) +tor_mutex_free_(tor_mutex_t *m) { if (!m) return; @@ -68,7 +68,7 @@ tor_cond_new(void) /** Free all storage held in c. */ void -tor_cond_free(tor_cond_t *c) +tor_cond_free_(tor_cond_t *c) { if (!c) return; diff --git a/src/common/compat_threads.h b/src/common/compat_threads.h index ce86b15e9d..c93e601ec5 100644 --- a/src/common/compat_threads.h +++ b/src/common/compat_threads.h @@ -54,7 +54,8 @@ void tor_mutex_init(tor_mutex_t *m); void tor_mutex_init_nonrecursive(tor_mutex_t *m); void tor_mutex_acquire(tor_mutex_t *m); void tor_mutex_release(tor_mutex_t *m); -void tor_mutex_free(tor_mutex_t *m); +void tor_mutex_free_(tor_mutex_t *m); +#define tor_mutex_free(m) FREE_AND_NULL(tor_mutex_t, tor_mutex_free_, (m)) void tor_mutex_uninit(tor_mutex_t *m); unsigned long tor_get_thread_id(void); void tor_threads_init(void); @@ -81,7 +82,8 @@ typedef struct tor_cond_t { } tor_cond_t; tor_cond_t *tor_cond_new(void); -void tor_cond_free(tor_cond_t *cond); +void tor_cond_free_(tor_cond_t *cond); +#define tor_cond_free(c) FREE_AND_NULL(tor_cond_t, tor_cond_free_, (c)) int tor_cond_init(tor_cond_t *cond); void tor_cond_uninit(tor_cond_t *cond); int tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex, diff --git a/src/common/compress.c b/src/common/compress.c index bc12a58ad6..47c93cf6a9 100644 --- a/src/common/compress.c +++ b/src/common/compress.c @@ -598,7 +598,7 @@ tor_compress_process(tor_compress_state_t *state, /** Deallocate state. */ void -tor_compress_free(tor_compress_state_t *state) +tor_compress_free_(tor_compress_state_t *state) { if (state == NULL) return; diff --git a/src/common/compress.h b/src/common/compress.h index 23a9817479..952102bf97 100644 --- a/src/common/compress.h +++ b/src/common/compress.h @@ -80,7 +80,9 @@ tor_compress_output_t tor_compress_process(tor_compress_state_t *state, char **out, size_t *out_len, const char **in, size_t *in_len, int finish); -void tor_compress_free(tor_compress_state_t *state); +void tor_compress_free_(tor_compress_state_t *state); +#define tor_compress_free(st) \ + FREE_AND_NULL(tor_compress_state_t, tor_compress_free_, (st)) size_t tor_compress_state_size(const tor_compress_state_t *state); diff --git a/src/common/compress_lzma.c b/src/common/compress_lzma.c index 6426ede4fd..051c59ba2d 100644 --- a/src/common/compress_lzma.c +++ b/src/common/compress_lzma.c @@ -323,7 +323,7 @@ tor_lzma_compress_process(tor_lzma_compress_state_t *state, /** Deallocate state. */ void -tor_lzma_compress_free(tor_lzma_compress_state_t *state) +tor_lzma_compress_free_(tor_lzma_compress_state_t *state) { if (state == NULL) return; diff --git a/src/common/compress_lzma.h b/src/common/compress_lzma.h index 7639d98a70..38a447c1f3 100644 --- a/src/common/compress_lzma.h +++ b/src/common/compress_lzma.h @@ -31,7 +31,10 @@ tor_lzma_compress_process(tor_lzma_compress_state_t *state, const char **in, size_t *in_len, int finish); -void tor_lzma_compress_free(tor_lzma_compress_state_t *state); +void tor_lzma_compress_free_(tor_lzma_compress_state_t *state); +#define tor_lzma_compress_free(st) \ + FREE_AND_NULL(tor_lzma_compress_state_t, \ + tor_lzma_compress_free_, (st)) size_t tor_lzma_compress_state_size(const tor_lzma_compress_state_t *state); diff --git a/src/common/compress_zlib.c b/src/common/compress_zlib.c index 284542e885..23d71d27be 100644 --- a/src/common/compress_zlib.c +++ b/src/common/compress_zlib.c @@ -265,7 +265,7 @@ tor_zlib_compress_process(tor_zlib_compress_state_t *state, /** Deallocate state. */ void -tor_zlib_compress_free(tor_zlib_compress_state_t *state) +tor_zlib_compress_free_(tor_zlib_compress_state_t *state) { if (state == NULL) return; diff --git a/src/common/compress_zlib.h b/src/common/compress_zlib.h index 8ace467bf0..e3c1a2b339 100644 --- a/src/common/compress_zlib.h +++ b/src/common/compress_zlib.h @@ -31,7 +31,10 @@ tor_zlib_compress_process(tor_zlib_compress_state_t *state, const char **in, size_t *in_len, int finish); -void tor_zlib_compress_free(tor_zlib_compress_state_t *state); +void tor_zlib_compress_free_(tor_zlib_compress_state_t *state); +#define tor_zlib_compress_free(st) \ + FREE_AND_NULL(tor_zlib_compress_state_t, \ + tor_zlib_compress_free_, (st)) size_t tor_zlib_compress_state_size(const tor_zlib_compress_state_t *state); diff --git a/src/common/compress_zstd.c b/src/common/compress_zstd.c index c1cdaf17ad..0db87d61b7 100644 --- a/src/common/compress_zstd.c +++ b/src/common/compress_zstd.c @@ -399,7 +399,7 @@ tor_zstd_compress_process(tor_zstd_compress_state_t *state, /** Deallocate state. */ void -tor_zstd_compress_free(tor_zstd_compress_state_t *state) +tor_zstd_compress_free_(tor_zstd_compress_state_t *state) { if (state == NULL) return; diff --git a/src/common/compress_zstd.h b/src/common/compress_zstd.h index 02466010ff..9bca24ded7 100644 --- a/src/common/compress_zstd.h +++ b/src/common/compress_zstd.h @@ -31,7 +31,10 @@ tor_zstd_compress_process(tor_zstd_compress_state_t *state, const char **in, size_t *in_len, int finish); -void tor_zstd_compress_free(tor_zstd_compress_state_t *state); +void tor_zstd_compress_free_(tor_zstd_compress_state_t *state); +#define tor_zstd_compress_free(st) \ + FREE_AND_NULL(tor_zstd_compress_state_t, \ + tor_zstd_compress_free_, (st)) size_t tor_zstd_compress_state_size(const tor_zstd_compress_state_t *state); diff --git a/src/common/confline.c b/src/common/confline.c index 781ad2a12b..bf613ab742 100644 --- a/src/common/confline.c +++ b/src/common/confline.c @@ -330,7 +330,7 @@ config_process_include(const char *path, int recursion_level, int extended, * Free all the configuration lines on the linked list front. */ void -config_free_lines(config_line_t *front) +config_free_lines_(config_line_t *front) { config_line_t *tmp; diff --git a/src/common/confline.h b/src/common/confline.h index feeb9f249d..772a9bbbdc 100644 --- a/src/common/confline.h +++ b/src/common/confline.h @@ -48,7 +48,12 @@ int config_get_lines(const char *string, config_line_t **result, int extended); int config_get_lines_include(const char *string, config_line_t **result, int extended, int *has_include, smartlist_t *opened_lst); -void config_free_lines(config_line_t *front); +void config_free_lines_(config_line_t *front); +#define config_free_lines(front) \ + do { \ + config_free_lines_(front); \ + (front) = NULL; \ + } while (0) const char *parse_config_line_from_str_verbose(const char *line, char **key_out, char **value_out, const char **err_out); diff --git a/src/common/container.c b/src/common/container.c index 8645cb4826..54b0b2028f 100644 --- a/src/common/container.c +++ b/src/common/container.c @@ -42,7 +42,7 @@ smartlist_new,(void)) * list's elements. */ MOCK_IMPL(void, -smartlist_free,(smartlist_t *sl)) +smartlist_free_,(smartlist_t *sl)) { if (!sl) return; @@ -1163,19 +1163,26 @@ HT_GENERATE2(digest256map_impl, digest256map_entry_t, node, digest256map_entry_hash, digest256map_entries_eq, 0.6, tor_reallocarray_, tor_free_) +#define strmap_entry_free(ent) \ + FREE_AND_NULL(strmap_entry_t, strmap_entry_free_, (ent)) +#define digestmap_entry_free(ent) \ + FREE_AND_NULL(digestmap_entry_t, digestmap_entry_free_, (ent)) +#define digest256map_entry_free(ent) \ + FREE_AND_NULL(digest256map_entry_t, digest256map_entry_free_, (ent)) + static inline void -strmap_entry_free(strmap_entry_t *ent) +strmap_entry_free_(strmap_entry_t *ent) { tor_free(ent->key); tor_free(ent); } static inline void -digestmap_entry_free(digestmap_entry_t *ent) +digestmap_entry_free_(digestmap_entry_t *ent) { tor_free(ent); } static inline void -digest256map_entry_free(digest256map_entry_t *ent) +digest256map_entry_free_(digest256map_entry_t *ent) { tor_free(ent); } @@ -1335,7 +1342,7 @@ digest256map_assign_key(digest256map_entry_t *ent, const uint8_t *key) * those entries. If free_val is provided, invoked it every value in \ * map. */ \ MOCK_IMPL(void, \ - prefix##_free, (maptype *map, void (*free_val)(void*))) \ + prefix##_free_, (maptype *map, void (*free_val)(void*))) \ { \ prefix##_entry_t **ent, **next, *this; \ if (!map) \ @@ -1525,7 +1532,7 @@ digestset_new(int max_elements) /** Free all storage held in set. */ void -digestset_free(digestset_t *set) +digestset_free_(digestset_t *set) { if (!set) return; diff --git a/src/common/container.h b/src/common/container.h index f6affd3bc6..5d2dce5416 100644 --- a/src/common/container.h +++ b/src/common/container.h @@ -28,7 +28,9 @@ typedef struct smartlist_t { } smartlist_t; MOCK_DECL(smartlist_t *, smartlist_new, (void)); -MOCK_DECL(void, smartlist_free, (smartlist_t *sl)); +MOCK_DECL(void, smartlist_free_, (smartlist_t *sl)); +#define smartlist_free(sl) FREE_AND_NULL(smartlist_t, smartlist_free_, (sl)) + void smartlist_clear(smartlist_t *sl); void smartlist_add(smartlist_t *sl, void *element); void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2); @@ -350,7 +352,7 @@ char *smartlist_join_strings2(smartlist_t *sl, const char *join, void* prefix##set(maptype *map, keytype key, void *val); \ void* prefix##get(const maptype *map, keytype key); \ void* prefix##remove(maptype *map, keytype key); \ - MOCK_DECL(void, prefix##free, (maptype *map, void (*free_val)(void*))); \ + MOCK_DECL(void, prefix##free_, (maptype *map, void (*free_val)(void*))); \ int prefix##isempty(const maptype *map); \ int prefix##size(const maptype *map); \ prefix##iter_t *prefix##iter_init(maptype *map); \ @@ -368,6 +370,16 @@ DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_); * table. */ DECLARE_MAP_FNS(digest256map_t, const uint8_t *, digest256map_); +#define MAP_FREE_AND_NULL(maptype, map, fn) \ + do { \ + maptype ## _free_((map), (fn)); \ + (map) = NULL; \ + } while (0) + +#define strmap_free(map, fn) MAP_FREE_AND_NULL(strmap, (map), (fn)) +#define digestmap_free(map, fn) MAP_FREE_AND_NULL(digestmap, (map), (fn)) +#define digest256map_free(map, fn) MAP_FREE_AND_NULL(digest256map, (map), (fn)) + #undef DECLARE_MAP_FNS /** Iterates over the key-value pairs in a map map in order. @@ -528,9 +540,9 @@ void* strmap_remove_lc(strmap_t *map, const char *key); return (valtype*)digestmap_remove((digestmap_t*)map, key); \ } \ ATTR_UNUSED static inline void \ - prefix##f##ree(maptype *map, void (*free_val)(void*)) \ + prefix##f##ree_(maptype *map, void (*free_val)(void*)) \ { \ - digestmap_free((digestmap_t*)map, free_val); \ + digestmap_free_((digestmap_t*)map, free_val); \ } \ ATTR_UNUSED static inline int \ prefix##isempty(maptype *map) \ @@ -614,10 +626,12 @@ bitarray_expand(bitarray_t *ba, } /** Free the bit array ba. */ static inline void -bitarray_free(bitarray_t *ba) +bitarray_free_(bitarray_t *ba) { tor_free(ba); } +#define bitarray_free(ba) FREE_AND_NULL(bitarray_t, bitarray_free_, (ba)) + /** Set the bitth bit in b to 1. */ static inline void bitarray_set(bitarray_t *b, int bit) @@ -679,7 +693,8 @@ digestset_contains(const digestset_t *set, const char *digest) #undef BIT digestset_t *digestset_new(int max_elements); -void digestset_free(digestset_t* set); +void digestset_free_(digestset_t* set); +#define digestset_free(set) FREE_AND_NULL(digestset_t, digestset_free_, (set)) /* These functions, given an array of n_elements, return the * nth lowest element. nth=0 gives the lowest element; diff --git a/src/common/crypto.c b/src/common/crypto.c index 380f038f42..575bfd0c06 100644 --- a/src/common/crypto.c +++ b/src/common/crypto.c @@ -529,7 +529,7 @@ crypto_pk_new,(void)) * are released, free the key. */ void -crypto_pk_free(crypto_pk_t *env) +crypto_pk_free_(crypto_pk_t *env) { if (!env) return; @@ -592,7 +592,7 @@ crypto_cipher_new(const char *key) /** Free a symmetric cipher. */ void -crypto_cipher_free(crypto_cipher_t *env) +crypto_cipher_free_(crypto_cipher_t *env) { if (!env) return; @@ -1977,7 +1977,7 @@ crypto_digest512_new(digest_algorithm_t algorithm) /** Deallocate a digest object. */ void -crypto_digest_free(crypto_digest_t *digest) +crypto_digest_free_(crypto_digest_t *digest) { if (!digest) return; @@ -2224,7 +2224,7 @@ crypto_xof_squeeze_bytes(crypto_xof_t *xof, uint8_t *out, size_t len) /** Cleanse and deallocate a XOF object. */ void -crypto_xof_free(crypto_xof_t *xof) +crypto_xof_free_(crypto_xof_t *xof) { if (!xof) return; @@ -2777,7 +2777,7 @@ crypto_expand_key_material_rfc5869_sha256( /** Free a DH key exchange object. */ void -crypto_dh_free(crypto_dh_t *dh) +crypto_dh_free_(crypto_dh_t *dh) { if (!dh) return; diff --git a/src/common/crypto.h b/src/common/crypto.h index f9aeeee2c0..eca115fa79 100644 --- a/src/common/crypto.h +++ b/src/common/crypto.h @@ -19,6 +19,7 @@ #include "torint.h" #include "testsupport.h" #include "compat.h" +#include "util.h" #include #include "keccak-tiny/keccak-tiny.h" @@ -146,7 +147,8 @@ int crypto_global_cleanup(void); /* environment setup */ MOCK_DECL(crypto_pk_t *,crypto_pk_new,(void)); -void crypto_pk_free(crypto_pk_t *env); +void crypto_pk_free_(crypto_pk_t *env); +#define crypto_pk_free(pk) FREE_AND_NULL(crypto_pk_t, crypto_pk_free_, (pk)) void crypto_set_tls_dh_prime(void); crypto_cipher_t *crypto_cipher_new(const char *key); @@ -155,7 +157,9 @@ crypto_cipher_t *crypto_cipher_new_with_iv(const char *key, const char *iv); crypto_cipher_t *crypto_cipher_new_with_iv_and_bits(const uint8_t *key, const uint8_t *iv, int bits); -void crypto_cipher_free(crypto_cipher_t *env); +void crypto_cipher_free_(crypto_cipher_t *env); +#define crypto_cipher_free(c) \ + FREE_AND_NULL(crypto_cipher_t, crypto_cipher_free_, (c)) /* public key crypto */ MOCK_DECL(int, crypto_pk_generate_key_with_bits,(crypto_pk_t *env, int bits)); @@ -258,7 +262,9 @@ int crypto_digest_algorithm_parse_name(const char *name); crypto_digest_t *crypto_digest_new(void); crypto_digest_t *crypto_digest256_new(digest_algorithm_t algorithm); crypto_digest_t *crypto_digest512_new(digest_algorithm_t algorithm); -void crypto_digest_free(crypto_digest_t *digest); +void crypto_digest_free_(crypto_digest_t *digest); +#define crypto_digest_free(d) \ + FREE_AND_NULL(crypto_digest_t, crypto_digest_free_, (d)) void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data, size_t len); void crypto_digest_get_digest(crypto_digest_t *digest, @@ -276,7 +282,9 @@ void crypto_mac_sha3_256(uint8_t *mac_out, size_t len_out, crypto_xof_t *crypto_xof_new(void); void crypto_xof_add_bytes(crypto_xof_t *xof, const uint8_t *data, size_t len); void crypto_xof_squeeze_bytes(crypto_xof_t *xof, uint8_t *out, size_t len); -void crypto_xof_free(crypto_xof_t *xof); +void crypto_xof_free_(crypto_xof_t *xof); +#define crypto_xof_free(xof) \ + FREE_AND_NULL(crypto_xof_t, crypto_xof_free_, (xof)) /* Key negotiation */ #define DH_TYPE_CIRCUIT 1 @@ -291,7 +299,8 @@ int crypto_dh_get_public(crypto_dh_t *dh, char *pubkey_out, ssize_t crypto_dh_compute_secret(int severity, crypto_dh_t *dh, const char *pubkey, size_t pubkey_len, char *secret_out, size_t secret_out_len); -void crypto_dh_free(crypto_dh_t *dh); +void crypto_dh_free_(crypto_dh_t *dh); +#define crypto_dh_free(dh) FREE_AND_NULL(crypto_dh_t, crypto_dh_free_, (dh)) int crypto_expand_key_material_TAP(const uint8_t *key_in, size_t key_in_len, diff --git a/src/common/crypto_ed25519.c b/src/common/crypto_ed25519.c index 94b23e31b9..26523e3126 100644 --- a/src/common/crypto_ed25519.c +++ b/src/common/crypto_ed25519.c @@ -622,7 +622,7 @@ ed25519_pubkey_read_from_file(ed25519_public_key_t *pubkey_out, /** Release all storage held for kp. */ void -ed25519_keypair_free(ed25519_keypair_t *kp) +ed25519_keypair_free_(ed25519_keypair_t *kp) { if (! kp) return; diff --git a/src/common/crypto_ed25519.h b/src/common/crypto_ed25519.h index 8d13a487d6..74269ccffd 100644 --- a/src/common/crypto_ed25519.h +++ b/src/common/crypto_ed25519.h @@ -7,6 +7,7 @@ #include "testsupport.h" #include "torint.h" #include "crypto_curve25519.h" +#include "util.h" #define ED25519_PUBKEY_LEN 32 #define ED25519_SECKEY_LEN 64 @@ -117,7 +118,9 @@ int ed25519_pubkey_read_from_file(ed25519_public_key_t *pubkey_out, char **tag_out, const char *filename); -void ed25519_keypair_free(ed25519_keypair_t *kp); +void ed25519_keypair_free_(ed25519_keypair_t *kp); +#define ed25519_keypair_free(kp) \ + FREE_AND_NULL(ed25519_keypair_t, ed25519_keypair_free_, (kp)) int ed25519_pubkey_eq(const ed25519_public_key_t *key1, const ed25519_public_key_t *key2); diff --git a/src/common/di_ops.c b/src/common/di_ops.c index 7c0b4e7630..90e9357c8e 100644 --- a/src/common/di_ops.c +++ b/src/common/di_ops.c @@ -148,7 +148,7 @@ struct di_digest256_map_t { /** Release all storage held in map, calling free_fn on each value * as we go. */ void -dimap_free(di_digest256_map_t *map, dimap_free_fn free_fn) +dimap_free_(di_digest256_map_t *map, dimap_free_fn free_fn) { while (map) { di_digest256_map_t *victim = map; diff --git a/src/common/di_ops.h b/src/common/di_ops.h index e79973ba52..67d9c9f0df 100644 --- a/src/common/di_ops.h +++ b/src/common/di_ops.h @@ -37,7 +37,12 @@ int safe_mem_is_zero(const void *mem, size_t sz); typedef struct di_digest256_map_t di_digest256_map_t; typedef void (*dimap_free_fn)(void *); -void dimap_free(di_digest256_map_t *map, dimap_free_fn free_fn); +void dimap_free_(di_digest256_map_t *map, dimap_free_fn free_fn); +#define dimap_free(map, free_fn) \ + do { \ + dimap_free_((map), (free_fn)); \ + (map) = NULL; \ + } while (0) void dimap_add_entry(di_digest256_map_t **map, const uint8_t *key, void *val); void *dimap_search(const di_digest256_map_t *map, const uint8_t *key, diff --git a/src/common/handles.h b/src/common/handles.h index a610753a1c..aef8cd89ef 100644 --- a/src/common/handles.h +++ b/src/common/handles.h @@ -59,7 +59,7 @@ #define HANDLE_DECL(name, structname, linkage) \ typedef struct name ## _handle_t name ## _handle_t; \ linkage name ## _handle_t *name ## _handle_new(struct structname *object); \ - linkage void name ## _handle_free(name ## _handle_t *); \ + linkage void name ## _handle_free_(name ## _handle_t *); \ linkage struct structname *name ## _handle_get(name ## _handle_t *); \ linkage void name ## _handles_clear(struct structname *object); @@ -113,7 +113,7 @@ } \ \ linkage void \ - name ## _handle_free(struct name ## _handle_t *ref) \ + name ## _handle_free_(struct name ## _handle_t *ref) \ { \ if (! ref) return; \ name ## _handle_head_t *head = ref->head; \ diff --git a/src/common/log.c b/src/common/log.c index 0becb5ce03..80055fda04 100644 --- a/src/common/log.c +++ b/src/common/log.c @@ -63,7 +63,9 @@ typedef struct logfile_t { * log for each log domain? */ } logfile_t; -static void log_free(logfile_t *victim); +static void log_free_(logfile_t *victim); +#define log_free(lg) \ + FREE_AND_NULL(logfile_t, log_free_, (lg)) /** Helper: map a log severity to descriptive string. */ static inline const char * @@ -385,9 +387,12 @@ pending_log_message_new(int severity, log_domain_mask_t domain, return m; } +#define pending_log_message_free(msg) \ + FREE_AND_NULL(pending_log_message_t, pending_log_message_free_, (msg)) + /** Release all storage held by msg. */ static void -pending_log_message_free(pending_log_message_t *msg) +pending_log_message_free_(pending_log_message_t *msg) { if (!msg) return; @@ -721,7 +726,7 @@ log_fn_ratelim_(ratelim_t *ratelim, int severity, log_domain_mask_t domain, /** Free all storage held by victim. */ static void -log_free(logfile_t *victim) +log_free_(logfile_t *victim) { if (!victim) return; diff --git a/src/common/memarea.c b/src/common/memarea.c index b059987e0e..fa7b69a43c 100644 --- a/src/common/memarea.c +++ b/src/common/memarea.c @@ -153,7 +153,7 @@ memarea_new(void) /** Free area, invalidating all pointers returned from memarea_alloc() * and friends for this area */ void -memarea_drop_all(memarea_t *area) +memarea_drop_all_(memarea_t *area) { memarea_chunk_t *chunk, *next; for (chunk = area->first; chunk; chunk = next) { diff --git a/src/common/memarea.h b/src/common/memarea.h index c3d954e1ce..5207e8a5bd 100644 --- a/src/common/memarea.h +++ b/src/common/memarea.h @@ -8,7 +8,12 @@ typedef struct memarea_t memarea_t; memarea_t *memarea_new(void); -void memarea_drop_all(memarea_t *area); +void memarea_drop_all_(memarea_t *area); +#define memarea_drop_all(area) \ + do { \ + memarea_drop_all_(area); \ + (area) = NULL; \ + } while (0) void memarea_clear(memarea_t *area); int memarea_owns_ptr(const memarea_t *area, const void *ptr); void *memarea_alloc(memarea_t *area, size_t sz); diff --git a/src/common/procmon.c b/src/common/procmon.c index 26c11823e8..abcbbeaa21 100644 --- a/src/common/procmon.c +++ b/src/common/procmon.c @@ -325,7 +325,7 @@ tor_process_monitor_poll_cb(evutil_socket_t unused1, short unused2, /** Free the process-termination monitor procmon. */ void -tor_process_monitor_free(tor_process_monitor_t *procmon) +tor_process_monitor_free_(tor_process_monitor_t *procmon) { if (procmon == NULL) return; diff --git a/src/common/procmon.h b/src/common/procmon.h index 10ead11ba8..63777e4111 100644 --- a/src/common/procmon.h +++ b/src/common/procmon.h @@ -27,7 +27,9 @@ tor_process_monitor_t *tor_process_monitor_new(struct event_base *base, tor_procmon_callback_t cb, void *cb_arg, const char **msg); -void tor_process_monitor_free(tor_process_monitor_t *procmon); +void tor_process_monitor_free_(tor_process_monitor_t *procmon); +#define tor_process_monitor_free(procmon) \ + FREE_AND_NULL(tor_process_monitor_t, tor_process_monitor_free_, (procmon)) #endif /* !defined(TOR_PROCMON_H) */ diff --git a/src/common/sandbox.c b/src/common/sandbox.c index aec9857e94..c5936f4030 100644 --- a/src/common/sandbox.c +++ b/src/common/sandbox.c @@ -1521,8 +1521,12 @@ cached_getaddrinfo_items_eq(const cached_getaddrinfo_item_t *a, return (a->family == b->family) && 0 == strcmp(a->name, b->name); } +#define cached_getaddrinfo_item_free(item) \ + FREE_AND_NULL(cached_getaddrinfo_item_t, \ + cached_getaddrinfo_item_free_, (item)) + static void -cached_getaddrinfo_item_free(cached_getaddrinfo_item_t *item) +cached_getaddrinfo_item_free_(cached_getaddrinfo_item_t *item) { if (item == NULL) return; diff --git a/src/common/storagedir.c b/src/common/storagedir.c index c471ea911f..e2c7b4bb87 100644 --- a/src/common/storagedir.c +++ b/src/common/storagedir.c @@ -59,7 +59,7 @@ storage_dir_new(const char *dirname, int max_files) * Drop all in-RAM storage for d. Does not delete any files. */ void -storage_dir_free(storage_dir_t *d) +storage_dir_free_(storage_dir_t *d) { if (d == NULL) return; diff --git a/src/common/storagedir.h b/src/common/storagedir.h index 3de0afc361..d99bd7ec52 100644 --- a/src/common/storagedir.h +++ b/src/common/storagedir.h @@ -9,7 +9,10 @@ struct config_line_t; struct sandbox_cfg_elem; storage_dir_t * storage_dir_new(const char *dirname, int n_files); -void storage_dir_free(storage_dir_t *d); +void storage_dir_free_(storage_dir_t *d); +#define storage_dir_free(d) \ + FREE_AND_NULL(storage_dir_t, storage_dir_free_, (d)) + int storage_dir_register_with_sandbox(storage_dir_t *d, struct sandbox_cfg_elem **cfg); const smartlist_t *storage_dir_list(storage_dir_t *d); diff --git a/src/common/timers.c b/src/common/timers.c index c8e09414f4..93cde7de5f 100644 --- a/src/common/timers.c +++ b/src/common/timers.c @@ -245,7 +245,7 @@ timer_new(timer_cb_fn_t cb, void *arg) * scheduled. */ void -timer_free(tor_timer_t *t) +timer_free_(tor_timer_t *t) { if (! t) return; diff --git a/src/common/timers.h b/src/common/timers.h index d4d4fb00a9..6d27f3e01e 100644 --- a/src/common/timers.h +++ b/src/common/timers.h @@ -17,7 +17,8 @@ void timer_get_cb(const tor_timer_t *t, timer_cb_fn_t *cb_out, void **arg_out); void timer_schedule(tor_timer_t *t, const struct timeval *delay); void timer_disable(tor_timer_t *t); -void timer_free(tor_timer_t *t); +void timer_free_(tor_timer_t *t); +#define timer_free(t) FREE_AND_NULL(tor_timer_t, timer_free_, (t)) void timers_initialize(void); void timers_shutdown(void); diff --git a/src/common/tortls.c b/src/common/tortls.c index 197c5e8d3b..407603248f 100644 --- a/src/common/tortls.c +++ b/src/common/tortls.c @@ -644,7 +644,7 @@ static const char CLIENT_CIPHER_LIST[] = /** Free all storage held in cert */ void -tor_x509_cert_free(tor_x509_cert_t *cert) +tor_x509_cert_free_(tor_x509_cert_t *cert) { if (! cert) return; @@ -1792,7 +1792,7 @@ tor_tls_is_server(tor_tls_t *tls) * underlying file descriptor. */ void -tor_tls_free(tor_tls_t *tls) +tor_tls_free_(tor_tls_t *tls) { if (!tls) return; diff --git a/src/common/tortls.h b/src/common/tortls.h index 6145f7dbc9..1dbf0b332f 100644 --- a/src/common/tortls.h +++ b/src/common/tortls.h @@ -216,7 +216,8 @@ void tor_tls_set_renegotiate_callback(tor_tls_t *tls, void (*cb)(tor_tls_t *, void *arg), void *arg); int tor_tls_is_server(tor_tls_t *tls); -void tor_tls_free(tor_tls_t *tls); +void tor_tls_free_(tor_tls_t *tls); +#define tor_tls_free(tls) FREE_AND_NULL(tor_tls_t, tor_tls_free_, (tls)) int tor_tls_peer_has_cert(tor_tls_t *tls); MOCK_DECL(tor_x509_cert_t *,tor_tls_get_peer_cert,(tor_tls_t *tls)); MOCK_DECL(tor_x509_cert_t *,tor_tls_get_own_cert,(tor_tls_t *tls)); @@ -263,7 +264,9 @@ void check_no_tls_errors_(const char *fname, int line); void tor_tls_log_one_error(tor_tls_t *tls, unsigned long err, int severity, int domain, const char *doing); -void tor_x509_cert_free(tor_x509_cert_t *cert); +void tor_x509_cert_free_(tor_x509_cert_t *cert); +#define tor_x509_cert_free(c) \ + FREE_AND_NULL(tor_x509_cert_t, tor_x509_cert_free_, (c)) tor_x509_cert_t *tor_x509_cert_decode(const uint8_t *certificate, size_t certificate_len); void tor_x509_cert_get_der(const tor_x509_cert_t *cert, diff --git a/src/common/util.c b/src/common/util.c index 7dc5e8144d..67e97811c6 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -4713,7 +4713,7 @@ environment_variable_names_equal(const char *s1, const char *s2) /** Free env (assuming it was produced by * process_environment_make). */ void -process_environment_free(process_environment_t *env) +process_environment_free_(process_environment_t *env) { if (env == NULL) return; diff --git a/src/common/util.h b/src/common/util.h index 6bc853da26..8dc64ce9fa 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -80,12 +80,22 @@ extern int dmalloc_free(const char *file, const int line, void *pnt, * This is a macro. If you need a function pointer to release memory from * tor_malloc(), use tor_free_(). */ +#ifdef __GNUC__ +#define tor_free(p) STMT_BEGIN \ + typeof(&(p)) tor_free__tmpvar = &(p); \ + if (PREDICT_LIKELY((*tor_free__tmpvar)!=NULL)) { \ + raw_free(*tor_free__tmpvar); \ + *tor_free__tmpvar=NULL; \ + } \ + STMT_END +#else #define tor_free(p) STMT_BEGIN \ if (PREDICT_LIKELY((p)!=NULL)) { \ raw_free(p); \ (p)=NULL; \ } \ STMT_END +#endif #endif /* defined(USE_DMALLOC) */ #define tor_malloc(size) tor_malloc_(size DMALLOC_ARGS) @@ -109,6 +119,17 @@ extern int dmalloc_free(const char *file, const int line, void *pnt, void tor_log_mallinfo(int severity); +/* Helper macro: free a variable of type 'typename' using freefn, and + * set the variable to NULL. + */ +#define FREE_AND_NULL(typename, freefn, var) \ + do { \ + /* only evaluate (var) once. */ \ + typename **tmp__free__ptr ## freefn = &(var); \ + freefn(*tmp__free__ptr ## freefn); \ + (*tmp__free__ptr ## freefn) = NULL; \ + } while (0) + /** Macro: yield a pointer to the field at position off within the * structure st. Example: *
@@ -423,7 +444,9 @@ struct process_environment_t {
 };
 
 process_environment_t *process_environment_make(struct smartlist_t *env_vars);
-void process_environment_free(process_environment_t *env);
+void process_environment_free_(process_environment_t *env);
+#define process_environment_free(env) \
+  FREE_AND_NULL(process_environment_t, process_environment_free_, (env))
 
 struct smartlist_t *get_current_process_environment_variables(void);
 
diff --git a/src/common/workqueue.c b/src/common/workqueue.c
index 42723224d3..ec96959b7d 100644
--- a/src/common/workqueue.c
+++ b/src/common/workqueue.c
@@ -148,12 +148,15 @@ workqueue_entry_new(workqueue_reply_t (*fn)(void*, void*),
   return ent;
 }
 
+#define workqueue_entry_free(ent) \
+  FREE_AND_NULL(workqueue_entry_t, workqueue_entry_free_, (ent))
+
 /**
  * Release all storage held in ent. Call only when ent is not on
  * any queue.
  */
 static void
-workqueue_entry_free(workqueue_entry_t *ent)
+workqueue_entry_free_(workqueue_entry_t *ent)
 {
   if (!ent)
     return;
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 _ent. */
 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 _ent. */
 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 address (which must map to ent) 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 bridge. */
 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 address -- 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 port */
 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 options. */
 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 conn 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 conn 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 cell. */
 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 state. */
 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 cache.
  */
 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 diff. */
 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 job.
  */
 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 job
  */
 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 ev. */
 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 dc. */
 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 req.
  */
 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 spooled. */
 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 s. */
 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 restriction */
 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 state.
  */
 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 e. */
 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 cmd. */
 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 ent. */
 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 desc. */
 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 hs_token. */
 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 data */
 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 rs. */
 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 rs. */
 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 sig */
 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 ns. */
 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 
 
 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 node  */
 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 keys. */
 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 victim. */
 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 policy. */
 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 p. */
 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 p. */
 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 req. */
 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 addr. */
 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. failure
@@ -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 desc.
  */
 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 service.
  */
 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_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);
+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. */
 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 router. */
 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 extrainfo */
 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 sd. */
 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 e. */
 static void
-extrainfo_free_(void *e)
+extrainfo_free_void(void *e)
 {
-  extrainfo_free(e);
+  extrainfo_free_(e);
 }
 
 /** Free all storage held by a routerlist rl. */
 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 cert. */
 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 ds. */
 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 routerset. */
 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 commit, 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 cert. */
 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 certs */
 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 transport. */
 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);
 
diff --git a/src/test/test-timers.c b/src/test/test-timers.c
index 99715f4333..a0b5b535c2 100644
--- a/src/test/test-timers.c
+++ b/src/test/test-timers.c
@@ -133,7 +133,7 @@ main(int argc, char **argv)
     ret = 0;
   }
 
-  timer_free(NULL);
+  timer_free_(NULL);
 
   for (i = 0; i < N_TIMERS; ++i) {
     timer_free(timers[i]);
diff --git a/src/test/test.c b/src/test/test.c
index 432df35c34..c2e285635c 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -345,8 +345,8 @@ test_onion_queues(void *arg)
   tt_int_op(0,OP_EQ, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
 
  done:
-  circuit_free(TO_CIRCUIT(circ1));
-  circuit_free(TO_CIRCUIT(circ2));
+  circuit_free_(TO_CIRCUIT(circ1));
+  circuit_free_(TO_CIRCUIT(circ2));
   tor_free(create1);
   tor_free(create2);
   tor_free(onionskin);
@@ -616,7 +616,7 @@ test_rend_fns(void *arg)
  done:
   if (descs) {
     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_free(descs);
   }
   if (parsed)
diff --git a/src/test/test_address.c b/src/test/test_address.c
index f36ff6998b..9c88d37a41 100644
--- a/src/test/test_address.c
+++ b/src/test/test_address.c
@@ -763,7 +763,7 @@ test_address_get_if_addrs_list_internal(void *arg)
   tt_assert(!smartlist_contains_ipv6_tor_addr(results));
 
  done:
-  free_interface_address_list(results);
+  interface_address_list_free(results);
   return;
 }
 
@@ -792,7 +792,7 @@ test_address_get_if_addrs_list_no_internal(void *arg)
   tt_assert(!smartlist_contains_ipv6_tor_addr(results));
 
  done:
-  free_interface_address_list(results);
+  interface_address_list_free(results);
   return;
 }
 
@@ -834,7 +834,7 @@ test_address_get_if_addrs6_list_internal(void *arg)
   }
 
  done:
-  free_interface_address6_list(results);
+  interface_address6_list_free(results);
   teardown_capture_of_logs();
   return;
 }
@@ -878,7 +878,7 @@ test_address_get_if_addrs6_list_no_internal(void *arg)
 
  done:
   teardown_capture_of_logs();
-  free_interface_address6_list(results);
+  interface_address6_list_free(results);
   return;
 }
 
@@ -943,8 +943,8 @@ test_address_get_if_addrs_internal_fail(void *arg)
  done:
   UNMOCK(get_interface_addresses_raw);
   UNMOCK(get_interface_address6_via_udp_socket_hack);
-  free_interface_address6_list(results1);
-  free_interface_address6_list(results2);
+  interface_address6_list_free(results1);
+  interface_address6_list_free(results2);
   return;
 }
 
@@ -971,8 +971,8 @@ test_address_get_if_addrs_no_internal_fail(void *arg)
  done:
   UNMOCK(get_interface_addresses_raw);
   UNMOCK(get_interface_address6_via_udp_socket_hack);
-  free_interface_address6_list(results1);
-  free_interface_address6_list(results2);
+  interface_address6_list_free(results1);
+  interface_address6_list_free(results2);
   return;
 }
 
diff --git a/src/test/test_cell_queue.c b/src/test/test_cell_queue.c
index 69e89b69b0..df987f82ce 100644
--- a/src/test/test_cell_queue.c
+++ b/src/test/test_cell_queue.c
@@ -130,8 +130,8 @@ test_circuit_n_cells(void *arg)
   tt_int_op(n_cells_in_circ_queues(TO_CIRCUIT(origin_c)), OP_EQ, 2);
 
  done:
-  circuit_free(TO_CIRCUIT(or_c));
-  circuit_free(TO_CIRCUIT(origin_c));
+  circuit_free_(TO_CIRCUIT(or_c));
+  circuit_free_(TO_CIRCUIT(origin_c));
 }
 
 struct testcase_t cell_queue_tests[] = {
diff --git a/src/test/test_channel.c b/src/test/test_channel.c
index 38b69a9ad3..55d43a04e5 100644
--- a/src/test/test_channel.c
+++ b/src/test/test_channel.c
@@ -654,7 +654,7 @@ test_channel_outbound_cell(void *arg)
 
  done:
   if (circ) {
-    circuit_free(TO_CIRCUIT(circ));
+    circuit_free_(TO_CIRCUIT(circ));
   }
   tor_free(p_cell);
   channel_free_all();
diff --git a/src/test/test_circuitlist.c b/src/test/test_circuitlist.c
index f622704ec5..d170009a9c 100644
--- a/src/test/test_circuitlist.c
+++ b/src/test/test_circuitlist.c
@@ -141,7 +141,7 @@ test_clist_maps(void *arg)
   /* Okay, now free ch2 and make sure that the circuit ID is STILL not
    * usable, because we haven't declared the destroy to be nonpending */
   tt_int_op(cdm.ncalls, OP_EQ, 0);
-  circuit_free(TO_CIRCUIT(or_c2));
+  circuit_free_(TO_CIRCUIT(or_c2));
   or_c2 = NULL; /* prevent free */
   tt_int_op(cdm.ncalls, OP_EQ, 2);
   memset(&cdm, 0, sizeof(cdm));
@@ -160,9 +160,9 @@ test_clist_maps(void *arg)
 
  done:
   if (or_c1)
-    circuit_free(TO_CIRCUIT(or_c1));
+    circuit_free_(TO_CIRCUIT(or_c1));
   if (or_c2)
-    circuit_free(TO_CIRCUIT(or_c2));
+    circuit_free_(TO_CIRCUIT(or_c2));
   if (ch1)
     tor_free(ch1->cmux);
   if (ch2)
@@ -234,11 +234,11 @@ test_rend_token_maps(void *arg)
   /* Marking a circuit makes it not get returned any more */
   circuit_mark_for_close(TO_CIRCUIT(c1), END_CIRC_REASON_FINISHED);
   tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_rend_circ_relay_side(tok1));
-  circuit_free(TO_CIRCUIT(c1));
+  circuit_free_(TO_CIRCUIT(c1));
   c1 = NULL;
 
   /* Freeing a circuit makes it not get returned any more. */
-  circuit_free(TO_CIRCUIT(c2));
+  circuit_free_(TO_CIRCUIT(c2));
   c2 = NULL;
   tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_intro_circ_v2_relay_side(tok2));
 
@@ -275,15 +275,15 @@ test_rend_token_maps(void *arg)
 
  done:
   if (c1)
-    circuit_free(TO_CIRCUIT(c1));
+    circuit_free_(TO_CIRCUIT(c1));
   if (c2)
-    circuit_free(TO_CIRCUIT(c2));
+    circuit_free_(TO_CIRCUIT(c2));
   if (c3)
-    circuit_free(TO_CIRCUIT(c3));
+    circuit_free_(TO_CIRCUIT(c3));
   if (c4)
-    circuit_free(TO_CIRCUIT(c4));
+    circuit_free_(TO_CIRCUIT(c4));
   if (c5)
-    circuit_free(TO_CIRCUIT(c5));
+    circuit_free_(TO_CIRCUIT(c5));
 }
 
 static void
@@ -452,10 +452,10 @@ test_hs_circuitmap_isolation(void *arg)
   }
 
  done:
-  circuit_free(TO_CIRCUIT(circ1));
-  circuit_free(TO_CIRCUIT(circ2));
-  circuit_free(TO_CIRCUIT(circ3));
-  circuit_free(TO_CIRCUIT(circ4));
+  circuit_free_(TO_CIRCUIT(circ1));
+  circuit_free_(TO_CIRCUIT(circ2));
+  circuit_free_(TO_CIRCUIT(circ3));
+  circuit_free_(TO_CIRCUIT(circ4));
 }
 
 struct testcase_t circuitlist_tests[] = {
diff --git a/src/test/test_dir.c b/src/test/test_dir.c
index 83734ccfd9..ee7aea5b84 100644
--- a/src/test/test_dir.c
+++ b/src/test/test_dir.c
@@ -567,7 +567,7 @@ test_dir_routerinfo_parsing(void *arg)
 static void
 routerinfo_free_wrapper_(void *arg)
 {
-  routerinfo_free(arg);
+  routerinfo_free_(arg);
 }
 
 static void
@@ -664,7 +664,7 @@ test_dir_extrainfo_parsing(void *arg)
   escaped(NULL);
   extrainfo_free(ei);
   routerinfo_free(ri);
-  digestmap_free((digestmap_t*)map, routerinfo_free_wrapper_);
+  digestmap_free_((digestmap_t*)map, routerinfo_free_wrapper_);
 }
 
 static void
@@ -760,7 +760,7 @@ test_dir_parse_router_list(void *arg)
   smartlist_free(chunks);
   routerinfo_free(ri);
   if (map) {
-    digestmap_free((digestmap_t*)map, routerinfo_free_wrapper_);
+    digestmap_free_((digestmap_t*)map, routerinfo_free_wrapper_);
     router_get_routerlist()->identity_map =
       (struct digest_ri_map_t*)digestmap_new();
   }
diff --git a/src/test/test_dir_handle_get.c b/src/test/test_dir_handle_get.c
index 28ecc14c49..ca64dce5fe 100644
--- a/src/test/test_dir_handle_get.c
+++ b/src/test/test_dir_handle_get.c
@@ -88,7 +88,7 @@ test_dir_handle_get_bad_request(void *data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -117,7 +117,7 @@ test_dir_handle_get_v1_command_not_found(void *data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -164,7 +164,7 @@ test_dir_handle_get_v1_command(void *data)
   done:
     UNMOCK(connection_write_to_buf_impl_);
     UNMOCK(get_dirportfrontpage);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     tor_free(body);
 }
@@ -190,7 +190,7 @@ test_dir_handle_get_not_found(void *data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -225,7 +225,7 @@ test_dir_handle_get_robots_txt(void *data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     tor_free(body);
 }
@@ -254,7 +254,7 @@ test_dir_handle_get_rendezvous2_not_found_if_not_encrypted(void *data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -282,7 +282,7 @@ test_dir_handle_get_rendezvous2_on_encrypted_conn_with_invalid_desc_id(
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -315,7 +315,7 @@ test_dir_handle_get_rendezvous2_on_encrypted_conn_not_well_formed(void *data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -344,7 +344,7 @@ test_dir_handle_get_rendezvous2_not_found(void *data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     rend_cache_free_all();
 }
@@ -424,7 +424,7 @@ test_dir_handle_get_rendezvous2_on_encrypted_conn_success(void *data)
     UNMOCK(connection_write_to_buf_impl_);
     NS_UNMOCK(router_get_my_routerinfo);
 
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     tor_free(body);
     rend_encoded_v2_service_descriptor_free(desc_holder);
@@ -457,7 +457,7 @@ test_dir_handle_get_micro_d_not_found(void *data)
   done:
     UNMOCK(connection_write_to_buf_impl_);
 
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -542,7 +542,7 @@ test_dir_handle_get_micro_d(void *data)
     UNMOCK(connection_write_to_buf_impl_);
 
     or_options_free(mock_options); mock_options = NULL;
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     tor_free(body);
     smartlist_free(list);
@@ -596,7 +596,7 @@ test_dir_handle_get_micro_d_server_busy(void *data)
     UNMOCK(connection_write_to_buf_impl_);
 
     or_options_free(mock_options); mock_options = NULL;
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     smartlist_free(list);
     microdesc_free_all();
@@ -633,7 +633,7 @@ test_dir_handle_get_networkstatus_bridges_not_found_without_auth(void *data)
     UNMOCK(get_options);
     UNMOCK(connection_write_to_buf_impl_);
     or_options_free(mock_options); mock_options = NULL;
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -673,7 +673,7 @@ test_dir_handle_get_networkstatus_bridges(void *data)
     UNMOCK(get_options);
     UNMOCK(connection_write_to_buf_impl_);
     or_options_free(mock_options); mock_options = NULL;
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -710,7 +710,7 @@ test_dir_handle_get_networkstatus_bridges_not_found_wrong_auth(void *data)
     UNMOCK(get_options);
     UNMOCK(connection_write_to_buf_impl_);
     or_options_free(mock_options); mock_options = NULL;
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -738,7 +738,7 @@ test_dir_handle_get_server_descriptors_not_found(void* data)
   done:
     UNMOCK(connection_write_to_buf_impl_);
     or_options_free(mock_options); mock_options = NULL;
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -799,7 +799,7 @@ test_dir_handle_get_server_descriptors_all(void* data)
   done:
     NS_UNMOCK(router_get_my_routerinfo);
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     tor_free(body);
 
@@ -903,7 +903,7 @@ test_dir_handle_get_server_descriptors_authority(void* data)
     UNMOCK(connection_write_to_buf_impl_);
     tor_free(mock_routerinfo->cache_info.signed_descriptor_body);
     tor_free(mock_routerinfo);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     tor_free(body);
     crypto_pk_free(identity_pkey);
@@ -975,7 +975,7 @@ test_dir_handle_get_server_descriptors_fp(void* data)
     UNMOCK(connection_write_to_buf_impl_);
     tor_free(mock_routerinfo->cache_info.signed_descriptor_body);
     tor_free(mock_routerinfo);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     tor_free(body);
     crypto_pk_free(identity_pkey);
@@ -1039,7 +1039,7 @@ test_dir_handle_get_server_descriptors_d(void* data)
   done:
     UNMOCK(connection_write_to_buf_impl_);
     tor_free(mock_routerinfo);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     tor_free(body);
     crypto_pk_free(identity_pkey);
@@ -1095,7 +1095,7 @@ test_dir_handle_get_server_descriptors_busy(void* data)
     UNMOCK(get_options);
     UNMOCK(connection_write_to_buf_impl_);
     tor_free(mock_routerinfo);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     crypto_pk_free(identity_pkey);
 
@@ -1126,7 +1126,7 @@ test_dir_handle_get_server_keys_bad_req(void* data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -1152,7 +1152,7 @@ test_dir_handle_get_server_keys_all_not_found(void* data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -1211,7 +1211,7 @@ test_dir_handle_get_server_keys_all(void* data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     tor_free(body);
 
@@ -1241,7 +1241,7 @@ test_dir_handle_get_server_keys_authority_not_found(void* data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -1289,7 +1289,7 @@ test_dir_handle_get_server_keys_authority(void* data)
   done:
     UNMOCK(get_my_v3_authority_cert);
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     tor_free(body);
     authority_cert_free(mock_cert); mock_cert = NULL;
@@ -1317,7 +1317,7 @@ test_dir_handle_get_server_keys_fp_not_found(void* data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -1371,7 +1371,7 @@ test_dir_handle_get_server_keys_fp(void* data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     tor_free(body);
     clear_dir_servers();
@@ -1400,7 +1400,7 @@ test_dir_handle_get_server_keys_sk_not_found(void* data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -1445,7 +1445,7 @@ test_dir_handle_get_server_keys_sk(void* data)
   done:
     UNMOCK(get_my_v3_authority_cert);
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     authority_cert_free(mock_cert); mock_cert = NULL;
     tor_free(header);
     tor_free(body);
@@ -1473,7 +1473,7 @@ test_dir_handle_get_server_keys_fpsk_not_found(void* data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -1530,7 +1530,7 @@ test_dir_handle_get_server_keys_fpsk(void* data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     tor_free(body);
 
@@ -1584,7 +1584,7 @@ test_dir_handle_get_server_keys_busy(void* data)
   done:
     UNMOCK(get_options);
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     or_options_free(mock_options); mock_options = NULL;
 
@@ -1650,7 +1650,7 @@ test_dir_handle_get_status_vote_current_consensus_ns_not_enough_sigs(void* d)
     UNMOCK(connection_write_to_buf_impl_);
     UNMOCK(get_options);
 
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     tor_free(stats);
     smartlist_free(mock_ns_val->voters);
@@ -1691,7 +1691,7 @@ test_dir_handle_get_status_vote_current_consensus_ns_not_found(void* data)
   done:
     UNMOCK(connection_write_to_buf_impl_);
     UNMOCK(get_options);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     tor_free(stats);
     or_options_free(mock_options); mock_options = NULL;
@@ -1765,7 +1765,7 @@ test_dir_handle_get_status_vote_current_consensus_too_old(void *data)
     UNMOCK(networkstatus_get_latest_consensus_by_flavor);
     UNMOCK(connection_write_to_buf_impl_);
     UNMOCK(get_options);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     tor_free(mock_ns_val);
     or_options_free(mock_options); mock_options = NULL;
@@ -1826,7 +1826,7 @@ status_vote_current_consensus_ns_test(char **header, char **body,
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
 }
 
 static void
@@ -1949,7 +1949,7 @@ test_dir_handle_get_status_vote_current_not_found(void* data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -1972,7 +1972,7 @@ status_vote_current_d_test(char **header, char **body, size_t *body_l)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
 }
 
 static void
@@ -1992,7 +1992,7 @@ status_vote_next_d_test(char **header, char **body, size_t *body_l)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
 }
 
 static void
@@ -2117,7 +2117,7 @@ test_dir_handle_get_status_vote_next_not_found(void* data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -2136,7 +2136,7 @@ status_vote_next_consensus_test(char **header, char **body, size_t *body_used)
                       body, body_used, 18, 0);
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
 }
 
 static void
@@ -2176,7 +2176,7 @@ test_dir_handle_get_status_vote_current_authority_not_found(void* data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -2200,7 +2200,7 @@ test_dir_handle_get_status_vote_next_authority_not_found(void* data)
 
   done:
     UNMOCK(connection_write_to_buf_impl_);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
 }
 
@@ -2282,7 +2282,7 @@ status_vote_next_consensus_signatures_test(char **header, char **body,
                       body, body_used, 22, 0);
 
   done:
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     UNMOCK(connection_write_to_buf_impl_);
 }
 
@@ -2430,7 +2430,7 @@ test_dir_handle_get_status_vote_next_authority(void* data)
   done:
     UNMOCK(connection_write_to_buf_impl_);
     UNMOCK(get_my_v3_authority_cert);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     tor_free(body);
     authority_cert_free(mock_cert); mock_cert = NULL;
@@ -2512,7 +2512,7 @@ test_dir_handle_get_status_vote_current_authority(void* data)
   done:
     UNMOCK(connection_write_to_buf_impl_);
     UNMOCK(get_my_v3_authority_cert);
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
     tor_free(header);
     tor_free(body);
     authority_cert_free(mock_cert); mock_cert = NULL;
diff --git a/src/test/test_dns.c b/src/test/test_dns.c
index 66b231b6da..1fee01d2c0 100644
--- a/src/test/test_dns.c
+++ b/src/test/test_dns.c
@@ -124,7 +124,7 @@ static int n_connection_free = 0;
 static connection_t *last_freed_conn = NULL;
 
 static void
-NS(connection_free)(connection_t *conn)
+NS(connection_free_)(connection_t *conn)
 {
    n_connection_free++;
 
@@ -267,7 +267,7 @@ NS(test_main)(void *arg)
    */
 
   NS_MOCK(dns_cancel_pending_resolve);
-  NS_MOCK(connection_free);
+  NS_MOCK(connection_free_);
 
   exitconn->on_circuit = &(on_circuit->base_);
   exitconn->base_.purpose = EXIT_PURPOSE_RESOLVE;
@@ -294,7 +294,7 @@ NS(test_main)(void *arg)
   NS_UNMOCK(send_resolved_cell);
   NS_UNMOCK(send_resolved_hostname_cell);
   NS_UNMOCK(dns_cancel_pending_resolve);
-  NS_UNMOCK(connection_free);
+  NS_UNMOCK(connection_free_);
   tor_free(on_circuit);
   tor_free(exitconn);
   tor_free(nextconn);
diff --git a/src/test/test_entryconn.c b/src/test/test_entryconn.c
index c29b1a7126..9d8a072c77 100644
--- a/src/test/test_entryconn.c
+++ b/src/test/test_entryconn.c
@@ -34,7 +34,7 @@ entryconn_rewrite_teardown(const struct testcase_t *tc, void *arg)
   (void)tc;
   entry_connection_t *ec = arg;
   if (ec)
-    connection_free_(ENTRY_TO_CONN(ec));
+    connection_free_minimal(ENTRY_TO_CONN(ec));
   addressmap_free_all();
   return 1;
 }
@@ -156,8 +156,8 @@ test_entryconn_rewrite_automap_ipv4(void *arg)
             ec->socks_request->address);
 
  done:
-  connection_free_(ENTRY_TO_CONN(ec2));
-  connection_free_(ENTRY_TO_CONN(ec3));
+  connection_free_minimal(ENTRY_TO_CONN(ec2));
+  connection_free_minimal(ENTRY_TO_CONN(ec3));
 }
 
 /* Automap on resolve, connect to automapped address, resolve again and get
@@ -230,9 +230,9 @@ test_entryconn_rewrite_automap_ipv6(void *arg)
             ec->socks_request->address);
 
  done:
-  connection_free_(ENTRY_TO_CONN(ec));
-  connection_free_(ENTRY_TO_CONN(ec2));
-  connection_free_(ENTRY_TO_CONN(ec3));
+  connection_free_minimal(ENTRY_TO_CONN(ec));
+  connection_free_minimal(ENTRY_TO_CONN(ec2));
+  connection_free_minimal(ENTRY_TO_CONN(ec3));
 }
 
 #if 0
@@ -283,7 +283,7 @@ test_entryconn_rewrite_automap_reverse(void *arg)
   tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
 
  done:
-  connection_free_(ENTRY_TO_CONN(ec2));
+  connection_free_minimal(ENTRY_TO_CONN(ec2));
 }
 #endif /* 0 */
 
@@ -333,7 +333,7 @@ test_entryconn_rewrite_cached_dns_ipv4(void *arg)
   tt_str_op(ec2->socks_request->address, OP_EQ, "240.240.241.241");
 
  done:
-  connection_free_(ENTRY_TO_CONN(ec2));
+  connection_free_minimal(ENTRY_TO_CONN(ec2));
 }
 
 /* Rewrite because of cached DNS entry. */
@@ -385,8 +385,8 @@ test_entryconn_rewrite_cached_dns_ipv6(void *arg)
   tt_str_op(ec2->socks_request->address, OP_EQ, "[::f00f]");
 
  done:
-  connection_free_(ENTRY_TO_CONN(ec));
-  connection_free_(ENTRY_TO_CONN(ec2));
+  connection_free_minimal(ENTRY_TO_CONN(ec));
+  connection_free_minimal(ENTRY_TO_CONN(ec2));
 }
 
 /* Fail to connect to unmapped address in virtual range. */
@@ -426,7 +426,7 @@ test_entryconn_rewrite_unmapped_virtual(void *arg)
   tt_int_op(rr.exit_source, OP_EQ, ADDRMAPSRC_NONE);
 
  done:
-  connection_free_(ENTRY_TO_CONN(ec2));
+  connection_free_minimal(ENTRY_TO_CONN(ec2));
 }
 
 /* Rewrite because of mapaddress option */
@@ -507,7 +507,7 @@ test_entryconn_rewrite_automap_exit(void *arg)
   tt_int_op(rr.end_reason, OP_EQ, END_STREAM_REASON_TORPROTOCOL);
 
  done:
-  connection_free_(ENTRY_TO_CONN(ec2));
+  connection_free_minimal(ENTRY_TO_CONN(ec2));
 }
 
 /* Rewrite into .exit because of mapaddress */
@@ -618,9 +618,9 @@ test_entryconn_rewrite_mapaddress_automap_onion(void *arg)
   */
 
  done:
-  connection_free_(ENTRY_TO_CONN(ec2));
-  connection_free_(ENTRY_TO_CONN(ec3));
-  connection_free_(ENTRY_TO_CONN(ec4));
+  connection_free_minimal(ENTRY_TO_CONN(ec2));
+  connection_free_minimal(ENTRY_TO_CONN(ec3));
+  connection_free_minimal(ENTRY_TO_CONN(ec4));
 }
 
 static void
@@ -678,8 +678,8 @@ test_entryconn_rewrite_mapaddress_automap_onion_common(entry_connection_t *ec,
                            "abcdefghijklmnop.onion"));
 
  done:
-  connection_free_(ENTRY_TO_CONN(ec2));
-  connection_free_(ENTRY_TO_CONN(ec3));
+  connection_free_minimal(ENTRY_TO_CONN(ec2));
+  connection_free_minimal(ENTRY_TO_CONN(ec3));
 }
 
 /* This time is the same, but we start with a mapping from a non-onion
diff --git a/src/test/test_entrynodes.c b/src/test/test_entrynodes.c
index 80ebebe3f8..1cff3828c4 100644
--- a/src/test/test_entrynodes.c
+++ b/src/test/test_entrynodes.c
@@ -2372,8 +2372,8 @@ upgrade_circuits_cleanup(const struct testcase_t *testcase, void *ptr)
   // circuit_guard_state_free(data->guard2_state); // held in circ2
   guard_selection_free(data->gs);
   smartlist_free(data->all_origin_circuits);
-  circuit_free(TO_CIRCUIT(data->circ1));
-  circuit_free(TO_CIRCUIT(data->circ2));
+  circuit_free_(TO_CIRCUIT(data->circ1));
+  circuit_free_(TO_CIRCUIT(data->circ2));
   tor_free(data);
   return big_fake_network_cleanup(testcase, NULL);
 }
diff --git a/src/test/test_extorport.c b/src/test/test_extorport.c
index e18deb2700..cadef257f1 100644
--- a/src/test/test_extorport.c
+++ b/src/test/test_extorport.c
@@ -58,11 +58,11 @@ test_ext_or_id_map(void *arg)
 
  done:
   if (c1)
-    connection_free_(TO_CONN(c1));
+    connection_free_minimal(TO_CONN(c1));
   if (c2)
-    connection_free_(TO_CONN(c2));
+    connection_free_minimal(TO_CONN(c2));
   if (c3)
-    connection_free_(TO_CONN(c3));
+    connection_free_minimal(TO_CONN(c3));
   tor_free(idp);
   tor_free(idp2);
   connection_or_clear_ext_or_id_map();
@@ -145,7 +145,7 @@ test_ext_or_write_command(void *arg)
 
  done:
   if (c1)
-    connection_free_(TO_CONN(c1));
+    connection_free_minimal(TO_CONN(c1));
   tor_free(cp);
   tor_free(buf);
   UNMOCK(connection_write_to_buf_impl_);
@@ -591,7 +591,7 @@ test_ext_or_handshake(void *arg)
   UNMOCK(connection_write_to_buf_impl_);
   UNMOCK(crypto_rand);
   if (conn)
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
 #undef CONTAINS
 #undef WRITE
 }
diff --git a/src/test/test_handles.c b/src/test/test_handles.c
index 7ddee6e376..eb1e1f1bbe 100644
--- a/src/test/test_handles.c
+++ b/src/test/test_handles.c
@@ -13,6 +13,8 @@ typedef struct demo_t {
 } demo_t;
 
 HANDLE_DECL(demo, demo_t, static)
+#define demo_handle_free(h)    \
+  FREE_AND_NULL(demo_handle_t, demo_handle_free_, (h))
 HANDLE_IMPL(demo, demo_t, static)
 
 static demo_t *
diff --git a/src/test/test_hs_cache.c b/src/test/test_hs_cache.c
index 91b13be862..458ce1a92e 100644
--- a/src/test/test_hs_cache.c
+++ b/src/test/test_hs_cache.c
@@ -259,7 +259,7 @@ helper_fetch_desc_from_hsdir(const ed25519_public_key_t *blinded_key)
  done:
   tor_free(hsdir_query_str);
   if (conn)
-    connection_free_(TO_CONN(conn));
+    connection_free_minimal(TO_CONN(conn));
 
   return received_desc;
 }
diff --git a/src/test/test_hs_client.c b/src/test/test_hs_client.c
index 750920fac0..7ee7210bc9 100644
--- a/src/test/test_hs_client.c
+++ b/src/test/test_hs_client.c
@@ -227,10 +227,10 @@ test_e2e_rend_circuit_setup_legacy(void *arg)
   tt_ptr_op(TO_EDGE_CONN(conn)->on_circuit, OP_EQ, TO_CIRCUIT(or_circ));
 
  done:
-  connection_free_(conn);
+  connection_free_minimal(conn);
   if (or_circ)
     tor_free(TO_CIRCUIT(or_circ)->n_chan);
-  circuit_free(TO_CIRCUIT(or_circ));
+  circuit_free_(TO_CIRCUIT(or_circ));
 }
 
 /* Test: Ensure that setting up v3 rendezvous circuits works correctly. */
@@ -297,10 +297,10 @@ test_e2e_rend_circuit_setup(void *arg)
   tt_ptr_op(TO_EDGE_CONN(conn)->on_circuit, OP_EQ, TO_CIRCUIT(or_circ));
 
  done:
-  connection_free_(conn);
+  connection_free_minimal(conn);
   if (or_circ)
     tor_free(TO_CIRCUIT(or_circ)->n_chan);
-  circuit_free(TO_CIRCUIT(or_circ));
+  circuit_free_(TO_CIRCUIT(or_circ));
 }
 
 /** Test client logic for picking intro points from a descriptor. Also test how
@@ -560,7 +560,7 @@ test_descriptor_fetch(void *arg)
     smartlist_add(get_connection_array(), TO_CONN(dir_conn));
     ret = hs_client_refetch_hsdesc(&service_pk);
     smartlist_remove(get_connection_array(), TO_CONN(dir_conn));
-    connection_free_(TO_CONN(dir_conn));
+    connection_free_minimal(TO_CONN(dir_conn));
     tt_int_op(ret, OP_EQ, HS_CLIENT_FETCH_PENDING);
   }
 
@@ -579,7 +579,7 @@ test_descriptor_fetch(void *arg)
   tt_int_op(ec->edge_.end_reason, OP_EQ, END_STREAM_REASON_RESOLVEFAILED);
 
  done:
-  connection_free_(ENTRY_TO_CONN(ec));
+  connection_free_minimal(ENTRY_TO_CONN(ec));
   UNMOCK(networkstatus_get_live_consensus);
   UNMOCK(router_have_minimum_dir_info);
   hs_free_all();
diff --git a/src/test/test_hs_intropoint.c b/src/test/test_hs_intropoint.c
index 0cae2de7e1..6a7962b212 100644
--- a/src/test/test_hs_intropoint.c
+++ b/src/test/test_hs_intropoint.c
@@ -194,7 +194,7 @@ test_establish_intro_wrong_purpose(void *arg)
   tt_int_op(retval, OP_EQ, -1);
 
  done:
-  circuit_free(TO_CIRCUIT(intro_circ));
+  circuit_free_(TO_CIRCUIT(intro_circ));
 }
 
 /* Prepare a circuit for accepting an ESTABLISH_INTRO cell */
@@ -228,7 +228,7 @@ test_establish_intro_wrong_keytype(void *arg)
   tt_int_op(retval, OP_EQ, -1);
 
  done:
-  circuit_free(TO_CIRCUIT(intro_circ));
+  circuit_free_(TO_CIRCUIT(intro_circ));
 }
 
 /* Send an ESTABLISH_INTRO cell with an unknown auth key type. Should fail. */
@@ -263,7 +263,7 @@ test_establish_intro_wrong_keytype2(void *arg)
   tt_int_op(retval, OP_EQ, -1);
 
  done:
-  circuit_free(TO_CIRCUIT(intro_circ));
+  circuit_free_(TO_CIRCUIT(intro_circ));
 }
 
 /* Send a legit ESTABLISH_INTRO cell but with a wrong MAC. Should fail. */
@@ -333,7 +333,7 @@ test_establish_intro_wrong_mac(void *arg)
 
  done:
   trn_cell_establish_intro_free(cell);
-  circuit_free(TO_CIRCUIT(intro_circ));
+  circuit_free_(TO_CIRCUIT(intro_circ));
 }
 
 /* Send a legit ESTABLISH_INTRO cell but with a wrong auth key length. Should
@@ -378,7 +378,7 @@ test_establish_intro_wrong_auth_key_len(void *arg)
 
  done:
   trn_cell_establish_intro_free(cell);
-  circuit_free(TO_CIRCUIT(intro_circ));
+  circuit_free_(TO_CIRCUIT(intro_circ));
 }
 
 /* Send a legit ESTABLISH_INTRO cell but with a wrong sig length. Should
@@ -423,7 +423,7 @@ test_establish_intro_wrong_sig_len(void *arg)
 
  done:
   trn_cell_establish_intro_free(cell);
-  circuit_free(TO_CIRCUIT(intro_circ));
+  circuit_free_(TO_CIRCUIT(intro_circ));
 }
 
 /* Send a legit ESTABLISH_INTRO cell but slightly change the signature. Should
@@ -460,7 +460,7 @@ test_establish_intro_wrong_sig(void *arg)
   tt_int_op(retval, OP_EQ, -1);
 
  done:
-  circuit_free(TO_CIRCUIT(intro_circ));
+  circuit_free_(TO_CIRCUIT(intro_circ));
 }
 
 /* Helper function: Send a well-formed v3 ESTABLISH_INTRO cell to
@@ -629,8 +629,8 @@ test_intro_point_registration(void *arg)
 
  done:
   crypto_pk_free(legacy_auth_key);
-  circuit_free(TO_CIRCUIT(intro_circ));
-  circuit_free(TO_CIRCUIT(legacy_intro_circ));
+  circuit_free_(TO_CIRCUIT(intro_circ));
+  circuit_free_(TO_CIRCUIT(legacy_intro_circ));
   trn_cell_establish_intro_free(establish_intro_cell);
   test_circuitmap_free_all();
 
@@ -650,7 +650,7 @@ test_introduce1_suitable_circuit(void *arg)
     circ = or_circuit_new(0, NULL);
     circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_OR);
     ret = circuit_is_suitable_for_introduce1(circ);
-    circuit_free(TO_CIRCUIT(circ));
+    circuit_free_(TO_CIRCUIT(circ));
     tt_int_op(ret, OP_EQ, 1);
   }
 
@@ -659,7 +659,7 @@ test_introduce1_suitable_circuit(void *arg)
     circ = or_circuit_new(0, NULL);
     circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_INTRO_POINT);
     ret = circuit_is_suitable_for_introduce1(circ);
-    circuit_free(TO_CIRCUIT(circ));
+    circuit_free_(TO_CIRCUIT(circ));
     tt_int_op(ret, OP_EQ, 0);
   }
 
@@ -670,7 +670,7 @@ test_introduce1_suitable_circuit(void *arg)
     /* Bogus pointer, the check is against NULL on n_chan. */
     circ->base_.n_chan = (channel_t *) circ;
     ret = circuit_is_suitable_for_introduce1(circ);
-    circuit_free(TO_CIRCUIT(circ));
+    circuit_free_(TO_CIRCUIT(circ));
     tt_int_op(ret, OP_EQ, 0);
   }
 
@@ -681,7 +681,7 @@ test_introduce1_suitable_circuit(void *arg)
     circuit_change_purpose(TO_CIRCUIT(circ), CIRCUIT_PURPOSE_OR);
     circ->already_received_introduce1 = 1;
     ret = circuit_is_suitable_for_introduce1(circ);
-    circuit_free(TO_CIRCUIT(circ));
+    circuit_free_(TO_CIRCUIT(circ));
     tt_int_op(ret, OP_EQ, 0);
   }
 
@@ -800,7 +800,7 @@ test_received_introduce1_handling(void *arg)
     circ = helper_create_intro_circuit();
     ret = hs_intro_received_introduce1(circ, buf, DIGEST_LEN - 1);
     tt_int_op(ret, OP_EQ, -1);
-    circuit_free(TO_CIRCUIT(circ));
+    circuit_free_(TO_CIRCUIT(circ));
   }
 
   /* We have a unit test only for the suitability of a circuit to receive an
@@ -813,7 +813,7 @@ test_received_introduce1_handling(void *arg)
     memset(test, 0, sizeof(test));
     ret = handle_introduce1(circ, test, sizeof(test));
     tor_free(circ->p_chan);
-    circuit_free(TO_CIRCUIT(circ));
+    circuit_free_(TO_CIRCUIT(circ));
     tt_int_op(ret, OP_EQ, -1);
   }
 
@@ -838,8 +838,8 @@ test_received_introduce1_handling(void *arg)
     memcpy(auth_key.pubkey, cell_auth_key, ED25519_PUBKEY_LEN);
     hs_circuitmap_register_intro_circ_v3_relay_side(service_circ, &auth_key);
     ret = hs_intro_received_introduce1(circ, request, request_len);
-    circuit_free(TO_CIRCUIT(circ));
-    circuit_free(TO_CIRCUIT(service_circ));
+    circuit_free_(TO_CIRCUIT(circ));
+    circuit_free_(TO_CIRCUIT(service_circ));
     tt_int_op(ret, OP_EQ, 0);
   }
 
@@ -867,8 +867,8 @@ test_received_introduce1_handling(void *arg)
     memcpy(token, legacy_key_id, sizeof(token));
     hs_circuitmap_register_intro_circ_v2_relay_side(service_circ, token);
     ret = hs_intro_received_introduce1(circ, request, request_len);
-    circuit_free(TO_CIRCUIT(circ));
-    circuit_free(TO_CIRCUIT(service_circ));
+    circuit_free_(TO_CIRCUIT(circ));
+    circuit_free_(TO_CIRCUIT(service_circ));
     tt_int_op(ret, OP_EQ, 0);
   }
 
diff --git a/src/test/test_hs_service.c b/src/test/test_hs_service.c
index 3084c6b958..99aa4ae7b4 100644
--- a/src/test/test_hs_service.c
+++ b/src/test/test_hs_service.c
@@ -182,7 +182,7 @@ test_e2e_rend_circuit_setup(void *arg)
   tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
 
  done:
-  circuit_free(TO_CIRCUIT(or_circ));
+  circuit_free_(TO_CIRCUIT(or_circ));
 }
 
 /* Helper: Return a newly allocated and initialized origin circuit with
@@ -655,7 +655,7 @@ test_intro_circuit_opened(void *arg)
   teardown_capture_of_logs();
 
  done:
-  circuit_free(TO_CIRCUIT(circ));
+  circuit_free_(TO_CIRCUIT(circ));
   hs_free_all();
   UNMOCK(circuit_mark_for_close_);
   UNMOCK(relay_send_command_from_edge_);
@@ -730,7 +730,7 @@ test_intro_established(void *arg)
 
  done:
   if (circ)
-    circuit_free(TO_CIRCUIT(circ));
+    circuit_free_(TO_CIRCUIT(circ));
   hs_free_all();
   UNMOCK(circuit_mark_for_close_);
 }
@@ -772,7 +772,7 @@ test_rdv_circuit_opened(void *arg)
   tt_int_op(TO_CIRCUIT(circ)->purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
 
  done:
-  circuit_free(TO_CIRCUIT(circ));
+  circuit_free_(TO_CIRCUIT(circ));
   hs_free_all();
   UNMOCK(circuit_mark_for_close_);
   UNMOCK(relay_send_command_from_edge_);
@@ -852,7 +852,7 @@ test_introduce2(void *arg)
   or_state_free(dummy_state);
   dummy_state = NULL;
   if (circ)
-    circuit_free(TO_CIRCUIT(circ));
+    circuit_free_(TO_CIRCUIT(circ));
   hs_free_all();
   UNMOCK(circuit_mark_for_close_);
 }
@@ -936,7 +936,7 @@ test_service_event(void *arg)
 
  done:
   hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
-  circuit_free(TO_CIRCUIT(circ));
+  circuit_free_(TO_CIRCUIT(circ));
   hs_free_all();
   UNMOCK(circuit_mark_for_close_);
 }
@@ -1490,8 +1490,8 @@ test_rendezvous1_parsing(void *arg)
    * would need an extra circuit and some more stuff but it's doable. */
 
  done:
-  circuit_free(TO_CIRCUIT(service_circ));
-  circuit_free(TO_CIRCUIT(client_circ));
+  circuit_free_(TO_CIRCUIT(service_circ));
+  circuit_free_(TO_CIRCUIT(client_circ));
   hs_service_free(service);
   hs_free_all();
   UNMOCK(relay_send_command_from_edge_);
diff --git a/src/test/test_link_handshake.c b/src/test/test_link_handshake.c
index 422d419078..6840072d76 100644
--- a/src/test/test_link_handshake.c
+++ b/src/test/test_link_handshake.c
@@ -302,8 +302,8 @@ test_link_handshake_certs_ok(void *arg)
   mock_own_cert = mock_peer_cert = NULL;
   memset(c1->identity_digest, 0, sizeof(c1->identity_digest));
   memset(c2->identity_digest, 0, sizeof(c2->identity_digest));
-  connection_free_(TO_CONN(c1));
-  connection_free_(TO_CONN(c2));
+  connection_free_minimal(TO_CONN(c1));
+  connection_free_minimal(TO_CONN(c2));
   tor_free(cell1);
   tor_free(cell2);
   certs_cell_free(cc1);
@@ -343,7 +343,7 @@ recv_certs_cleanup(const struct testcase_t *test, void *obj)
     tor_free(d->cell);
     certs_cell_free(d->ccell);
     connection_or_clear_identity(d->c);
-    connection_free_(TO_CONN(d->c));
+    connection_free_minimal(TO_CONN(d->c));
     circuitmux_free(d->chan->base_.cmux);
     tor_free(d->chan);
     crypto_pk_free(d->key1);
@@ -930,7 +930,7 @@ test_link_handshake_send_authchallenge(void *arg)
 
  done:
   UNMOCK(connection_or_write_var_cell_to_buf);
-  connection_free_(TO_CONN(c1));
+  connection_free_minimal(TO_CONN(c1));
   tor_free(cell1);
   tor_free(cell2);
   crypto_pk_free(rsa0);
@@ -955,7 +955,7 @@ recv_authchallenge_cleanup(const struct testcase_t *test, void *obj)
 
   if (d) {
     tor_free(d->cell);
-    connection_free_(TO_CONN(d->c));
+    connection_free_minimal(TO_CONN(d->c));
     circuitmux_free(d->chan->base_.cmux);
     tor_free(d->chan);
     tor_free(d);
@@ -1158,8 +1158,8 @@ authenticate_data_cleanup(const struct testcase_t *test, void *arg)
     tor_free(d->cell);
     connection_or_clear_identity(d->c1);
     connection_or_clear_identity(d->c2);
-    connection_free_(TO_CONN(d->c1));
-    connection_free_(TO_CONN(d->c2));
+    connection_free_minimal(TO_CONN(d->c1));
+    connection_free_minimal(TO_CONN(d->c2));
     circuitmux_free(d->chan2->base_.cmux);
     tor_free(d->chan2);
     crypto_pk_free(d->key1);
diff --git a/src/test/test_oom.c b/src/test/test_oom.c
index 973501c3d7..c172fe60c7 100644
--- a/src/test/test_oom.c
+++ b/src/test/test_oom.c
@@ -341,7 +341,7 @@ test_oom_streambuf(void *arg)
   circuit_free(c5);
 
   SMARTLIST_FOREACH(edgeconns, edge_connection_t *, ec,
-                    connection_free_(TO_CONN(ec)));
+                    connection_free_minimal(TO_CONN(ec)));
   smartlist_free(edgeconns);
 
   UNMOCK(circuit_mark_for_close_);
diff --git a/src/test/test_policy.c b/src/test/test_policy.c
index 83dca2d431..f8aa8ac40b 100644
--- a/src/test/test_policy.c
+++ b/src/test/test_policy.c
@@ -1318,7 +1318,7 @@ mock_get_interface_address6_list(int severity,
   return clone_list;
 
  done:
-  free_interface_address6_list(clone_list);
+  interface_address6_list_free(clone_list);
   return NULL;
 }
 
@@ -1393,11 +1393,11 @@ test_policies_reject_interface_address(void *arg)
 
  done:
   addr_policy_list_free(policy);
-  free_interface_address6_list(public_ipv4_addrs);
-  free_interface_address6_list(public_ipv6_addrs);
+  interface_address6_list_free(public_ipv4_addrs);
+  interface_address6_list_free(public_ipv6_addrs);
 
   UNMOCK(get_interface_address6_list);
-  /* we don't use free_interface_address6_list on these lists because their
+  /* we don't use interface_address6_list_free on these lists because their
    * address pointers are stack-based */
   smartlist_free(mock_ipv4_addrs);
   smartlist_free(mock_ipv6_addrs);
diff --git a/src/test/test_rendcache.c b/src/test/test_rendcache.c
index 9354dd0480..9f6cfc4a22 100644
--- a/src/test/test_rendcache.c
+++ b/src/test/test_rendcache.c
@@ -834,7 +834,7 @@ test_rend_cache_failure_entry_free(void *data)
   (void)data;
 
   // Test that it can deal with a NULL argument
-  rend_cache_failure_entry_free(NULL);
+  rend_cache_failure_entry_free_(NULL);
 
  /* done: */
  /*  (void)0; */
@@ -963,7 +963,7 @@ test_rend_cache_entry_free(void *data)
   rend_cache_entry_t *e;
 
   // Handles NULL correctly
-  rend_cache_entry_free(NULL);
+  rend_cache_entry_free_(NULL);
 
   // Handles NULL descriptor correctly
   e = tor_malloc_zero(sizeof(rend_cache_entry_t));
@@ -1135,7 +1135,7 @@ test_rend_cache_failure_intro_entry_free(void *data)
   rend_cache_failure_intro_t *entry;
 
   // Handles a null argument
-  rend_cache_failure_intro_entry_free(NULL);
+  rend_cache_failure_intro_entry_free_(NULL);
 
   // Handles a non-null argument
   entry = rend_cache_failure_intro_entry_new(INTRO_POINT_FAILURE_TIMEOUT);
@@ -1148,7 +1148,7 @@ test_rend_cache_failure_purge(void *data)
   (void)data;
 
   // Handles a null 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 = NULL;
 
   rend_cache_failure_purge();
diff --git a/src/test/test_replay.c b/src/test/test_replay.c
index c379cafa7c..d8dcc7370c 100644
--- a/src/test/test_replay.c
+++ b/src/test/test_replay.c
@@ -74,7 +74,7 @@ static void
 test_replaycache_free_null(void *arg)
 {
   (void)arg;
-  replaycache_free(NULL);
+  replaycache_free_(NULL);
   /* Assert that we're here without horrible death */
   tt_assert(1);
 
diff --git a/src/test/test_routerlist.c b/src/test/test_routerlist.c
index fd29e8f172..c19d66ef9d 100644
--- a/src/test/test_routerlist.c
+++ b/src/test/test_routerlist.c
@@ -520,7 +520,7 @@ test_directory_guard_fetch_with_no_dirinfo(void *arg)
  done:
   tor_free(consensus_text_md);
   tor_free(dummy_state);
-  connection_free_(TO_CONN(conn));
+  connection_free_minimal(TO_CONN(conn));
   entry_guards_free_all();
   teardown_capture_of_logs();
 }
diff --git a/src/test/test_routerset.c b/src/test/test_routerset.c
index 6da2275c35..c541324674 100644
--- a/src/test/test_routerset.c
+++ b/src/test/test_routerset.c
@@ -699,7 +699,7 @@ NS(test_main)(void *arg)
 static void
 NS(test_main)(void *arg)
 {
-  const routerset_t *set;
+  routerset_t *set;
   int needs_geoip;
   (void)arg;
 
@@ -709,14 +709,14 @@ NS(test_main)(void *arg)
 
   set = routerset_new();
   needs_geoip = routerset_needs_geoip(set);
-  routerset_free((routerset_t *)set);
+  routerset_free(set);
   tt_int_op(needs_geoip, OP_EQ, 0);
   set = NULL;
 
   set = routerset_new();
   smartlist_add(set->country_names, tor_strndup("xx", 2));
   needs_geoip = routerset_needs_geoip(set);
-  routerset_free((routerset_t *)set);
+  routerset_free(set);
   set = NULL;
   tt_int_op(needs_geoip, OP_NE, 0);
 
@@ -1947,7 +1947,7 @@ NS(test_main)(void *arg)
 
  done:
   tor_free(s);
-  routerset_free((routerset_t *)set);
+  routerset_free(set);
 }
 
 #undef NS_SUBMODULE
@@ -2084,28 +2084,28 @@ NS(test_main)(void *arg)
  * Structural test for routerset_free, where the routerset is NULL.
  */
 
-NS_DECL(void, smartlist_free, (smartlist_t *sl));
+NS_DECL(void, smartlist_free_, (smartlist_t *sl));
 
 static void
 NS(test_main)(void *arg)
 {
   (void)arg;
 
-  NS_MOCK(smartlist_free);
+  NS_MOCK(smartlist_free_);
 
-  routerset_free(NULL);
+  routerset_free_(NULL);
 
-  tt_int_op(CALLED(smartlist_free), OP_EQ, 0);
+  tt_int_op(CALLED(smartlist_free_), OP_EQ, 0);
 
   done:
     ;
 }
 
 void
-NS(smartlist_free)(smartlist_t *s)
+NS(smartlist_free_)(smartlist_t *s)
 {
   (void)s;
-  CALLED(smartlist_free)++;
+  CALLED(smartlist_free_)++;
 }
 
 #undef NS_SUBMODULE
@@ -2115,9 +2115,9 @@ NS(smartlist_free)(smartlist_t *s)
  * Structural test for routerset_free.
  */
 
-NS_DECL(void, smartlist_free, (smartlist_t *sl));
-NS_DECL(void, strmap_free,(strmap_t *map, void (*free_val)(void*)));
-NS_DECL(void, digestmap_free, (digestmap_t *map, void (*free_val)(void*)));
+NS_DECL(void, smartlist_free_, (smartlist_t *sl));
+NS_DECL(void, strmap_free_,(strmap_t *map, void (*free_val)(void*)));
+NS_DECL(void, digestmap_free_, (digestmap_t *map, void (*free_val)(void*)));
 
 static void
 NS(test_main)(void *arg)
@@ -2125,39 +2125,39 @@ NS(test_main)(void *arg)
   routerset_t *routerset = routerset_new();
   (void)arg;
 
-  NS_MOCK(smartlist_free);
-  NS_MOCK(strmap_free);
-  NS_MOCK(digestmap_free);
+  NS_MOCK(smartlist_free_);
+  NS_MOCK(strmap_free_);
+  NS_MOCK(digestmap_free_);
 
   routerset_free(routerset);
 
-  tt_int_op(CALLED(smartlist_free), OP_NE, 0);
-  tt_int_op(CALLED(strmap_free), OP_NE, 0);
-  tt_int_op(CALLED(digestmap_free), OP_NE, 0);
+  tt_int_op(CALLED(smartlist_free_), OP_NE, 0);
+  tt_int_op(CALLED(strmap_free_), OP_NE, 0);
+  tt_int_op(CALLED(digestmap_free_), OP_NE, 0);
 
   done:
     ;
 }
 
 void
-NS(smartlist_free)(smartlist_t *s)
+NS(smartlist_free_)(smartlist_t *s)
 {
-  CALLED(smartlist_free)++;
-  smartlist_free__real(s);
+  CALLED(smartlist_free_)++;
+  smartlist_free___real(s);
 }
 
 void
-NS(strmap_free)(strmap_t *map, void (*free_val)(void*))
+NS(strmap_free_)(strmap_t *map, void (*free_val)(void*))
 {
-  CALLED(strmap_free)++;
-  strmap_free__real(map, free_val);
+  CALLED(strmap_free_)++;
+  strmap_free___real(map, free_val);
 }
 
 void
-NS(digestmap_free)(digestmap_t *map, void (*free_val)(void*))
+NS(digestmap_free_)(digestmap_t *map, void (*free_val)(void*))
 {
-  CALLED(digestmap_free)++;
-  digestmap_free__real(map, free_val);
+  CALLED(digestmap_free_)++;
+  digestmap_free___real(map, free_val);
 }
 
 #undef NS_SUBMODULE
diff --git a/src/test/test_shared_random.c b/src/test/test_shared_random.c
index b0278e8a1d..96494904e3 100644
--- a/src/test/test_shared_random.c
+++ b/src/test/test_shared_random.c
@@ -1351,7 +1351,7 @@ test_state_update(void *arg)
   tt_assert(state->current_srv);
 
  done:
-  sr_state_free();
+  sr_state_free_all();
   UNMOCK(get_my_v3_authority_cert);
 }