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
This commit is contained in:
Tim Rühsen 2018-04-16 13:18:47 +02:00
parent 73fd57585c
commit 01002a168a
5 changed files with 25 additions and 30 deletions

View File

@ -624,19 +624,17 @@ hsts_store_close (hsts_store_t store)
static char * static char *
get_hsts_store_filename (void) get_hsts_store_filename (void)
{ {
char *home = NULL, *filename = NULL; char *filename = NULL;
FILE *fp = NULL; FILE *fp = NULL;
home = home_dir (); if (opt.homedir)
if (home)
{ {
filename = aprintf ("%s/.wget-hsts-test", home); filename = aprintf ("%s/.wget-hsts-test", opt.homedir);
fp = fopen (filename, "w"); fp = fopen (filename, "w");
if (fp) if (fp)
fclose (fp); fclose (fp);
} }
xfree (home);
return filename; return filename;
} }
@ -789,14 +787,13 @@ const char*
test_hsts_read_database (void) test_hsts_read_database (void)
{ {
hsts_store_t table; hsts_store_t table;
char *home = home_dir();
char *file = NULL; char *file = NULL;
FILE *fp = NULL; FILE *fp = NULL;
time_t created = time(NULL) - 10; 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"); fp = fopen (file, "w");
if (fp) if (fp)
{ {
@ -821,7 +818,6 @@ test_hsts_read_database (void)
unlink (file); unlink (file);
} }
xfree (file); xfree (file);
xfree (home);
} }
return NULL; return NULL;

View File

@ -593,17 +593,14 @@ wgetrc_env_file_name (void)
char * char *
wgetrc_user_file_name (void) wgetrc_user_file_name (void)
{ {
char *home;
char *file = NULL; char *file = NULL;
/* If that failed, try $HOME/.wgetrc (or equivalent). */ /* If that failed, try $HOME/.wgetrc (or equivalent). */
#ifdef __VMS #ifdef __VMS
file = "SYS$LOGIN:.wgetrc"; file = "SYS$LOGIN:.wgetrc";
#else /* def __VMS */ #else /* def __VMS */
home = home_dir (); if (opt.homedir)
if (home) file = aprintf ("%s/.wgetrc", opt.homedir);
file = aprintf ("%s/.wgetrc", home);
xfree (home);
#endif /* def __VMS [else] */ #endif /* def __VMS [else] */
if (!file) if (!file)
@ -906,7 +903,6 @@ setval_internal_tilde (int comind, const char *com, const char *val)
{ {
bool ret; bool ret;
int homelen; int homelen;
char *home;
char **pstring; char **pstring;
ret = setval_internal (comind, com, val); 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]))) && ret && (*val == '~' && ISSEP (val[1])))
{ {
pstring = commands[comind].place; pstring = commands[comind].place;
home = home_dir (); if (opt.homedir)
if (home)
{ {
char *home = xstrdup(opt.homedir);
homelen = strlen (home); homelen = strlen (home);
while (homelen && ISSEP (home[homelen - 1])) while (homelen && ISSEP (home[homelen - 1]))
home[--homelen] = '\0'; home[--homelen] = '\0';
xfree (*pstring);
/* Skip the leading "~/". */ /* Skip the leading "~/". */
for (++val; ISSEP (*val); val++) for (++val; ISSEP (*val); val++)
; ;
*pstring = concat_strings (home, "/", val, (char *)0); *pstring = concat_strings (home, "/", val, (char *)0);
xfree (home);
} }
} }
return ret; return ret;
@ -1884,12 +1883,12 @@ decode_string (const char *val, const struct decode_item *items, int itemcount,
} }
extern struct ptimer *timer; extern struct ptimer *timer;
extern int cleaned_up;
/* Free the memory allocated by global variables. */ /* Free the memory allocated by global variables. */
void void
cleanup (void) cleanup (void)
{ {
static int cleaned_up;
/* Free external resources, close files, etc. */ /* Free external resources, close files, etc. */
if (cleaned_up++) if (cleaned_up++)
@ -2002,6 +2001,7 @@ cleanup (void)
xfree (opt.encoding_remote); xfree (opt.encoding_remote);
xfree (opt.hsts_file); xfree (opt.hsts_file);
xfree (opt.homedir);
xfree (exec_name); xfree (exec_name);
xfree (program_argstring); xfree (program_argstring);
ptimer_destroy (timer); timer = NULL; ptimer_destroy (timer); timer = NULL;

View File

@ -172,16 +172,12 @@ hsts_store_t hsts_store;
static char* static char*
get_hsts_database (void) get_hsts_database (void)
{ {
char *home;
if (opt.hsts_file) if (opt.hsts_file)
return xstrdup (opt.hsts_file); return xstrdup (opt.hsts_file);
home = home_dir (); if (opt.homedir)
if (home)
{ {
char *dir = aprintf ("%s/.wget-hsts", home); char *dir = aprintf ("%s/.wget-hsts", opt.homedir);
xfree(home);
return dir; 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_name; /* Needed by lib/error.c. */
const char *program_argstring; /* Needed by wget_warc.c. */ const char *program_argstring; /* Needed by wget_warc.c. */
struct ptimer *timer; struct ptimer *timer;
int cleaned_up;
int int
main (int argc, char **argv) main (int argc, char **argv)
@ -1351,6 +1348,8 @@ main (int argc, char **argv)
bool noconfig = false; bool noconfig = false;
bool append_to_log = false; bool append_to_log = false;
cleaned_up = 0; /* do cleanup later */
timer = ptimer_new (); timer = ptimer_new ();
double start_time = ptimer_measure (timer); double start_time = ptimer_measure (timer);
@ -1396,6 +1395,7 @@ main (int argc, char **argv)
/* Load the hard-coded defaults. */ /* Load the hard-coded defaults. */
defaults (); defaults ();
opt.homedir = home_dir();
init_switches (); init_switches ();

View File

@ -91,18 +91,15 @@ search_netrc (const char *host, const char **acc, const char **passwd,
#else /* def __VMS */ #else /* def __VMS */
char *home = home_dir ();
netrc_list = NULL; netrc_list = NULL;
processed_netrc = 1; processed_netrc = 1;
if (home) if (opt.homedir)
{ {
int err; int err;
struct stat buf; struct stat buf;
char *path = (char *)alloca (strlen (home) + 1 char *path = (char *)alloca (strlen (opt.homedir) + 1
+ strlen (NETRC_FILE_NAME) + 1); + strlen (NETRC_FILE_NAME) + 1);
sprintf (path, "%s/%s", home, NETRC_FILE_NAME); sprintf (path, "%s/%s", opt.homedir, NETRC_FILE_NAME);
xfree (home);
err = stat (path, &buf); err = stat (path, &buf);
if (err == 0) if (err == 0)
netrc_list = parse_netrc (path); netrc_list = parse_netrc (path);

View File

@ -339,6 +339,8 @@ struct options
bool hsts; bool hsts;
char *hsts_file; char *hsts_file;
#endif #endif
const char *homedir; /* the homedir of the running process */
}; };
extern struct options opt; extern struct options opt;