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 *
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;

View File

@ -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;

View File

@ -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 ();

View File

@ -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);

View File

@ -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;