Make all the free() functions from container.h clear their targets

This commit is contained in:
Nick Mathewson 2017-11-17 11:46:25 -05:00
parent c1bdb80aba
commit 94db8f32e4
7 changed files with 57 additions and 35 deletions

View file

@ -42,7 +42,7 @@ smartlist_new,(void))
* list's elements. * list's elements.
*/ */
MOCK_IMPL(void, MOCK_IMPL(void,
smartlist_free,(smartlist_t *sl)) smartlist_free_,(smartlist_t *sl))
{ {
if (!sl) if (!sl)
return; return;
@ -1335,7 +1335,7 @@ digest256map_assign_key(digest256map_entry_t *ent, const uint8_t *key)
* those entries. If free_val is provided, invoked it every value in \ * those entries. If free_val is provided, invoked it every value in \
* <b>map</b>. */ \ * <b>map</b>. */ \
MOCK_IMPL(void, \ 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; \ prefix##_entry_t **ent, **next, *this; \
if (!map) \ if (!map) \
@ -1525,7 +1525,7 @@ digestset_new(int max_elements)
/** Free all storage held in <b>set</b>. */ /** Free all storage held in <b>set</b>. */
void void
digestset_free(digestset_t *set) digestset_free_(digestset_t *set)
{ {
if (!set) if (!set)
return; return;

View file

@ -28,7 +28,9 @@ typedef struct smartlist_t {
} smartlist_t; } smartlist_t;
MOCK_DECL(smartlist_t *, smartlist_new, (void)); 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, (sl))
void smartlist_clear(smartlist_t *sl); void smartlist_clear(smartlist_t *sl);
void smartlist_add(smartlist_t *sl, void *element); void smartlist_add(smartlist_t *sl, void *element);
void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2); 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##set(maptype *map, keytype key, void *val); \
void* prefix##get(const maptype *map, keytype key); \ void* prefix##get(const maptype *map, keytype key); \
void* prefix##remove(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##isempty(const maptype *map); \
int prefix##size(const maptype *map); \ int prefix##size(const maptype *map); \
prefix##iter_t *prefix##iter_init(maptype *map); \ prefix##iter_t *prefix##iter_init(maptype *map); \
@ -368,6 +370,16 @@ DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_);
* table. */ * table. */
DECLARE_MAP_FNS(digest256map_t, const uint8_t *, digest256map_); 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 #undef DECLARE_MAP_FNS
/** Iterates over the key-value pairs in a map <b>map</b> in order. /** Iterates over the key-value pairs in a map <b>map</b> in order.
@ -528,9 +540,9 @@ void* strmap_remove_lc(strmap_t *map, const char *key);
return (valtype*)digestmap_remove((digestmap_t*)map, key); \ return (valtype*)digestmap_remove((digestmap_t*)map, key); \
} \ } \
ATTR_UNUSED static inline void \ 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 \ ATTR_UNUSED static inline int \
prefix##isempty(maptype *map) \ prefix##isempty(maptype *map) \
@ -614,10 +626,12 @@ bitarray_expand(bitarray_t *ba,
} }
/** Free the bit array <b>ba</b>. */ /** Free the bit array <b>ba</b>. */
static inline void static inline void
bitarray_free(bitarray_t *ba) bitarray_free_(bitarray_t *ba)
{ {
tor_free(ba); tor_free(ba);
} }
#define bitarray_free(ba) FREE_AND_NULL(bitarray, (ba))
/** Set the <b>bit</b>th bit in <b>b</b> to 1. */ /** Set the <b>bit</b>th bit in <b>b</b> to 1. */
static inline void static inline void
bitarray_set(bitarray_t *b, int bit) bitarray_set(bitarray_t *b, int bit)
@ -679,7 +693,8 @@ digestset_contains(const digestset_t *set, const char *digest)
#undef BIT #undef BIT
digestset_t *digestset_new(int max_elements); 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, (set))
/* These functions, given an <b>array</b> of <b>n_elements</b>, return the /* These functions, given an <b>array</b> of <b>n_elements</b>, return the
* <b>nth</b> lowest element. <b>nth</b>=0 gives the lowest element; * <b>nth</b> lowest element. <b>nth</b>=0 gives the lowest element;

View file

@ -778,7 +778,7 @@ static or_options_t *global_default_options = NULL;
/** Name of most recently read torrc file. */ /** Name of most recently read torrc file. */
static char *torrc_fname = NULL; static char *torrc_fname = NULL;
/** Name of the most recently read torrc-defaults file.*/ /** 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. */ /** Configuration options set by command line. */
static config_line_t *global_cmdline_options = NULL; static config_line_t *global_cmdline_options = NULL;
/** Non-configuration options set by the command line */ /** Non-configuration options set by the command line */
@ -977,6 +977,8 @@ config_free_all(void)
tor_free(the_short_tor_version); tor_free(the_short_tor_version);
tor_free(the_tor_version); tor_free(the_tor_version);
have_parsed_cmdline = 0;
} }
/** Make <b>address</b> -- a piece of information related to our operation as /** Make <b>address</b> -- a piece of information related to our operation as

View file

@ -3698,6 +3698,7 @@ router_free_all(void)
crypto_pk_free(lastonionkey); crypto_pk_free(lastonionkey);
crypto_pk_free(server_identitykey); crypto_pk_free(server_identitykey);
crypto_pk_free(client_identitykey); crypto_pk_free(client_identitykey);
tor_mutex_free(key_lock); tor_mutex_free(key_lock);
routerinfo_free(desc_routerinfo); routerinfo_free(desc_routerinfo);
extrainfo_free(desc_extrainfo); extrainfo_free(desc_extrainfo);

View file

@ -143,6 +143,10 @@ DECLARE_TYPED_DIGESTMAP_FNS(dsmap_, digest_ds_map_t, download_status_t)
#define DSMAP_FOREACH(map, keyvar, valvar) \ #define DSMAP_FOREACH(map, keyvar, valvar) \
DIGESTMAP_FOREACH(dsmap_to_digestmap(map), keyvar, download_status_t *, \ DIGESTMAP_FOREACH(dsmap_to_digestmap(map), keyvar, download_status_t *, \
valvar) 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 */ /* Forward declaration for cert_list_t */
typedef struct cert_list_t cert_list_t; typedef struct cert_list_t cert_list_t;

View file

@ -664,7 +664,7 @@ test_dir_extrainfo_parsing(void *arg)
escaped(NULL); escaped(NULL);
extrainfo_free(ei); extrainfo_free(ei);
routerinfo_free(ri); routerinfo_free(ri);
digestmap_free((digestmap_t*)map, routerinfo_free_wrapper_); digestmap_free_((digestmap_t*)map, routerinfo_free_wrapper_);
} }
static void static void
@ -760,7 +760,7 @@ test_dir_parse_router_list(void *arg)
smartlist_free(chunks); smartlist_free(chunks);
routerinfo_free(ri); routerinfo_free(ri);
if (map) { if (map) {
digestmap_free((digestmap_t*)map, routerinfo_free_wrapper_); digestmap_free_((digestmap_t*)map, routerinfo_free_wrapper_);
router_get_routerlist()->identity_map = router_get_routerlist()->identity_map =
(struct digest_ri_map_t*)digestmap_new(); (struct digest_ri_map_t*)digestmap_new();
} }

View file

@ -2084,28 +2084,28 @@ NS(test_main)(void *arg)
* Structural test for routerset_free, where the routerset is NULL. * 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 static void
NS(test_main)(void *arg) NS(test_main)(void *arg)
{ {
(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: done:
; ;
} }
void void
NS(smartlist_free)(smartlist_t *s) NS(smartlist_free_)(smartlist_t *s)
{ {
(void)s; (void)s;
CALLED(smartlist_free)++; CALLED(smartlist_free_)++;
} }
#undef NS_SUBMODULE #undef NS_SUBMODULE
@ -2115,9 +2115,9 @@ NS(smartlist_free)(smartlist_t *s)
* Structural test for routerset_free. * Structural test for routerset_free.
*/ */
NS_DECL(void, smartlist_free, (smartlist_t *sl)); NS_DECL(void, smartlist_free_, (smartlist_t *sl));
NS_DECL(void, strmap_free,(strmap_t *map, void (*free_val)(void*))); 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, digestmap_free_, (digestmap_t *map, void (*free_val)(void*)));
static void static void
NS(test_main)(void *arg) NS(test_main)(void *arg)
@ -2125,39 +2125,39 @@ NS(test_main)(void *arg)
routerset_t *routerset = routerset_new(); routerset_t *routerset = routerset_new();
(void)arg; (void)arg;
NS_MOCK(smartlist_free); NS_MOCK(smartlist_free_);
NS_MOCK(strmap_free); NS_MOCK(strmap_free_);
NS_MOCK(digestmap_free); NS_MOCK(digestmap_free_);
routerset_free(routerset); routerset_free(routerset);
tt_int_op(CALLED(smartlist_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(strmap_free_), OP_NE, 0);
tt_int_op(CALLED(digestmap_free), OP_NE, 0); tt_int_op(CALLED(digestmap_free_), OP_NE, 0);
done: done:
; ;
} }
void void
NS(smartlist_free)(smartlist_t *s) NS(smartlist_free_)(smartlist_t *s)
{ {
CALLED(smartlist_free)++; CALLED(smartlist_free_)++;
smartlist_free__real(s); smartlist_free___real(s);
} }
void void
NS(strmap_free)(strmap_t *map, void (*free_val)(void*)) NS(strmap_free_)(strmap_t *map, void (*free_val)(void*))
{ {
CALLED(strmap_free)++; CALLED(strmap_free_)++;
strmap_free__real(map, free_val); strmap_free___real(map, free_val);
} }
void void
NS(digestmap_free)(digestmap_t *map, void (*free_val)(void*)) NS(digestmap_free_)(digestmap_t *map, void (*free_val)(void*))
{ {
CALLED(digestmap_free)++; CALLED(digestmap_free_)++;
digestmap_free__real(map, free_val); digestmap_free___real(map, free_val);
} }
#undef NS_SUBMODULE #undef NS_SUBMODULE