From 01002a168a1a4c9a24315e54fc70c346cc97a799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20R=C3=BChsen?= Date: Mon, 16 Apr 2018 13:18:47 +0200 Subject: [PATCH] Fix homedir memory leaks * src/hsts.c: Use opt.homedir * src/init.c: Likewise * src/main.c: Likewise * src/netrc.c: Likewise * src/options.h (struct options): Add homedir --- src/hsts.c | 14 +++++--------- src/init.c | 18 +++++++++--------- src/main.c | 12 ++++++------ src/netrc.c | 9 +++------ src/options.h | 2 ++ 5 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/hsts.c b/src/hsts.c index 4f707301..61ca413d 100644 --- a/src/hsts.c +++ b/src/hsts.c @@ -624,19 +624,17 @@ hsts_store_close (hsts_store_t store) static char * get_hsts_store_filename (void) { - char *home = NULL, *filename = NULL; + char *filename = NULL; FILE *fp = NULL; - home = home_dir (); - if (home) + if (opt.homedir) { - filename = aprintf ("%s/.wget-hsts-test", home); + filename = aprintf ("%s/.wget-hsts-test", opt.homedir); fp = fopen (filename, "w"); if (fp) fclose (fp); } - xfree (home); return filename; } @@ -789,14 +787,13 @@ const char* test_hsts_read_database (void) { hsts_store_t table; - char *home = home_dir(); char *file = NULL; FILE *fp = NULL; time_t created = time(NULL) - 10; - if (home) + if (opt.homedir) { - file = aprintf ("%s/.wget-hsts-testing", home); + file = aprintf ("%s/.wget-hsts-testing", opt.homedir); fp = fopen (file, "w"); if (fp) { @@ -821,7 +818,6 @@ test_hsts_read_database (void) unlink (file); } xfree (file); - xfree (home); } return NULL; diff --git a/src/init.c b/src/init.c index 724ba4c8..e2ceea1e 100644 --- a/src/init.c +++ b/src/init.c @@ -593,17 +593,14 @@ wgetrc_env_file_name (void) char * wgetrc_user_file_name (void) { - char *home; char *file = NULL; /* If that failed, try $HOME/.wgetrc (or equivalent). */ #ifdef __VMS file = "SYS$LOGIN:.wgetrc"; #else /* def __VMS */ - home = home_dir (); - if (home) - file = aprintf ("%s/.wgetrc", home); - xfree (home); + if (opt.homedir) + file = aprintf ("%s/.wgetrc", opt.homedir); #endif /* def __VMS [else] */ if (!file) @@ -906,7 +903,6 @@ setval_internal_tilde (int comind, const char *com, const char *val) { bool ret; int homelen; - char *home; char **pstring; ret = setval_internal (comind, com, val); @@ -916,17 +912,20 @@ setval_internal_tilde (int comind, const char *com, const char *val) && ret && (*val == '~' && ISSEP (val[1]))) { pstring = commands[comind].place; - home = home_dir (); - if (home) + if (opt.homedir) { + char *home = xstrdup(opt.homedir); homelen = strlen (home); while (homelen && ISSEP (home[homelen - 1])) home[--homelen] = '\0'; + xfree (*pstring); + /* Skip the leading "~/". */ for (++val; ISSEP (*val); val++) ; *pstring = concat_strings (home, "/", val, (char *)0); + xfree (home); } } return ret; @@ -1884,12 +1883,12 @@ decode_string (const char *val, const struct decode_item *items, int itemcount, } extern struct ptimer *timer; +extern int cleaned_up; /* Free the memory allocated by global variables. */ void cleanup (void) { - static int cleaned_up; /* Free external resources, close files, etc. */ if (cleaned_up++) @@ -2002,6 +2001,7 @@ cleanup (void) xfree (opt.encoding_remote); xfree (opt.hsts_file); + xfree (opt.homedir); xfree (exec_name); xfree (program_argstring); ptimer_destroy (timer); timer = NULL; diff --git a/src/main.c b/src/main.c index 677afa05..f30334fb 100644 --- a/src/main.c +++ b/src/main.c @@ -172,16 +172,12 @@ hsts_store_t hsts_store; static char* get_hsts_database (void) { - char *home; - if (opt.hsts_file) return xstrdup (opt.hsts_file); - home = home_dir (); - if (home) + if (opt.homedir) { - char *dir = aprintf ("%s/.wget-hsts", home); - xfree(home); + char *dir = aprintf ("%s/.wget-hsts", opt.homedir); return dir; } @@ -1338,6 +1334,7 @@ There is NO WARRANTY, to the extent permitted by law.\n"), stdout) < 0) const char *program_name; /* Needed by lib/error.c. */ const char *program_argstring; /* Needed by wget_warc.c. */ struct ptimer *timer; +int cleaned_up; int main (int argc, char **argv) @@ -1351,6 +1348,8 @@ main (int argc, char **argv) bool noconfig = false; bool append_to_log = false; + cleaned_up = 0; /* do cleanup later */ + timer = ptimer_new (); double start_time = ptimer_measure (timer); @@ -1396,6 +1395,7 @@ main (int argc, char **argv) /* Load the hard-coded defaults. */ defaults (); + opt.homedir = home_dir(); init_switches (); diff --git a/src/netrc.c b/src/netrc.c index 70017399..a1c1628e 100644 --- a/src/netrc.c +++ b/src/netrc.c @@ -91,18 +91,15 @@ search_netrc (const char *host, const char **acc, const char **passwd, #else /* def __VMS */ - char *home = home_dir (); - netrc_list = NULL; processed_netrc = 1; - if (home) + if (opt.homedir) { int err; struct stat buf; - char *path = (char *)alloca (strlen (home) + 1 + char *path = (char *)alloca (strlen (opt.homedir) + 1 + strlen (NETRC_FILE_NAME) + 1); - sprintf (path, "%s/%s", home, NETRC_FILE_NAME); - xfree (home); + sprintf (path, "%s/%s", opt.homedir, NETRC_FILE_NAME); err = stat (path, &buf); if (err == 0) netrc_list = parse_netrc (path); diff --git a/src/options.h b/src/options.h index 30845a1b..4e6fe929 100644 --- a/src/options.h +++ b/src/options.h @@ -339,6 +339,8 @@ struct options bool hsts; char *hsts_file; #endif + + const char *homedir; /* the homedir of the running process */ }; extern struct options opt;