clean up memory leaks, confusions

still one memory leak remaining here.


svn:r620
This commit is contained in:
Roger Dingledine 2003-10-18 02:18:22 +00:00
parent 2093f60760
commit af3fc006a5

View file

@ -32,7 +32,6 @@ void routerlist_free(routerinfo_t *list);
static char *eat_whitespace(char *s); static char *eat_whitespace(char *s);
static char *eat_whitespace_no_nl(char *s); static char *eat_whitespace_no_nl(char *s);
static char *find_whitespace(char *s); static char *find_whitespace(char *s);
static void router_free_exit_policy(routerinfo_t *router);
static int router_add_exit_policy_from_string(routerinfo_t *router, char *s); static int router_add_exit_policy_from_string(routerinfo_t *router, char *s);
static int router_add_exit_policy(routerinfo_t *router, static int router_add_exit_policy(routerinfo_t *router,
directory_token_t *tok); directory_token_t *tok);
@ -137,44 +136,45 @@ void router_get_directory(directory_t **pdirectory) {
*pdirectory = directory; *pdirectory = directory;
} }
/* delete a list of routers from memory */ /* delete a router from memory */
void routerinfo_free(routerinfo_t *router) void routerinfo_free(routerinfo_t *router)
{ {
struct exit_policy_t *e = NULL, *etmp = NULL; struct exit_policy_t *e;
if (!router) if (!router)
return; return;
if (router->address) if (router->address)
free(router->address); free(router->address);
if (router->nickname)
free(router->nickname);
if (router->onion_pkey) if (router->onion_pkey)
crypto_free_pk_env(router->onion_pkey); crypto_free_pk_env(router->onion_pkey);
if (router->link_pkey) if (router->link_pkey)
crypto_free_pk_env(router->link_pkey); crypto_free_pk_env(router->link_pkey);
if (router->identity_pkey) if (router->identity_pkey)
crypto_free_pk_env(router->identity_pkey); crypto_free_pk_env(router->identity_pkey);
e = router->exit_policy; while (router->exit_policy) {
while (e) { e = router->exit_policy;
etmp = e->next; router->exit_policy = e->next;
if (e->string) free(e->string); if (e->string) free(e->string);
if (e->address) free(e->address); if (e->address) free(e->address);
if (e->port) free(e->port); if (e->port) free(e->port);
free(e); free(e);
e = etmp;
} }
free(router); free(router);
} }
void directory_free(directory_t *directory) void directory_free(directory_t *dir)
{ {
int i; int i;
for (i = 0; i < directory->n_routers; ++i) for (i = 0; i < dir->n_routers; ++i)
routerinfo_free(directory->routers[i]); routerinfo_free(dir->routers[i]);
if (directory->routers) if (dir->routers)
free(directory->routers); free(dir->routers);
if(directory->software_versions) if(dir->software_versions)
free(directory->software_versions); free(dir->software_versions);
free(directory); free(dir);
} }
void router_mark_as_down(char *nickname) { void router_mark_as_down(char *nickname) {
@ -681,6 +681,9 @@ int router_get_list_from_string_impl(char **s, directory_t **dest,
router = router_get_entry_from_string(s); router = router_get_entry_from_string(s);
if (!router) { if (!router) {
log_fn(LOG_WARN, "Error reading router"); log_fn(LOG_WARN, "Error reading router");
for(i=0;i<rarray_len;i++)
routerinfo_free(rarray[i]);
free(rarray);
return -1; return -1;
} }
if (rarray_len >= MAX_ROUTERS_IN_DIR) { if (rarray_len >= MAX_ROUTERS_IN_DIR) {
@ -794,8 +797,6 @@ routerinfo_t *router_get_entry_from_string(char**s) {
router = tor_malloc(sizeof(routerinfo_t)); router = tor_malloc(sizeof(routerinfo_t));
memset(router,0,sizeof(routerinfo_t)); /* zero it out first */ memset(router,0,sizeof(routerinfo_t)); /* zero it out first */
/* C doesn't guarantee that NULL is represented by 0 bytes. You'll
thank me for this someday. */
router->onion_pkey = router->identity_pkey = router->link_pkey = NULL; router->onion_pkey = router->identity_pkey = router->link_pkey = NULL;
if (tok->val.cmd.n_args != 6) { if (tok->val.cmd.n_args != 6) {
@ -919,34 +920,12 @@ routerinfo_t *router_get_entry_from_string(char**s) {
err: err:
router_release_token(tok); router_release_token(tok);
if(router->address) routerinfo_free(router);
free(router->address);
if(router->link_pkey)
crypto_free_pk_env(router->link_pkey);
if(router->onion_pkey)
crypto_free_pk_env(router->onion_pkey);
if(router->identity_pkey)
crypto_free_pk_env(router->identity_pkey);
router_free_exit_policy(router);
free(router);
return NULL; return NULL;
#undef ARGS #undef ARGS
#undef NEXT_TOKEN #undef NEXT_TOKEN
} }
static void router_free_exit_policy(routerinfo_t *router) {
struct exit_policy_t *tmpe;
while(router->exit_policy) {
tmpe = router->exit_policy;
router->exit_policy = tmpe->next;
free(tmpe->string);
free(tmpe->address);
free(tmpe->port);
free(tmpe);
}
}
void router_add_exit_policy_from_config(routerinfo_t *router) { void router_add_exit_policy_from_config(routerinfo_t *router) {
char *s = options.ExitPolicy, *e; char *s = options.ExitPolicy, *e;
int last=0; int last=0;
@ -1132,6 +1111,7 @@ int router_rebuild_descriptor(void) {
address = localhostname; address = localhostname;
if(!strchr(address,'.')) { if(!strchr(address,'.')) {
log_fn(LOG_WARN,"fqdn '%s' has only one element. Misconfigured machine?",address); log_fn(LOG_WARN,"fqdn '%s' has only one element. Misconfigured machine?",address);
log_fn(LOG_WARN,"Try setting the Address line in your config file.");
return -1; return -1;
} }
} }