Skip to content

Commit

Permalink
Free conf_opt, conf_arg upon error in config-file parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
gperciva committed Feb 3, 2016
1 parent 6839ab2 commit 2a88fb5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tar/bsdtar.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ bsdtar_init(void)
bsdtar->siginfo = NULL;
bsdtar->substitution = NULL;
bsdtar->conffile = NULL;
bsdtar->conf_opt = NULL;
bsdtar->conf_arg = NULL;

/* We don't have bsdtar->progname yet, so we can't use bsdtar_errc. */
if (atexit(bsdtar_atexit)) {
Expand All @@ -194,6 +196,8 @@ bsdtar_atexit(void)
free(bsdtar->homedir);
free(bsdtar->option_csv_filename);
free(bsdtar->conffile);
free(bsdtar->conf_opt);
free(bsdtar->conf_arg);

/* Free matching and (if applicable) substitution patterns. */
cleanup_exclusions(bsdtar);
Expand Down Expand Up @@ -1287,6 +1291,9 @@ configfile_helper(struct bsdtar *bsdtar, const char *line)
if ((conf_opt = strdup(line)) == NULL)
bsdtar_errc(bsdtar, 1, errno, "Out of memory");

/* Record pointer for freeing upon error. */
bsdtar->conf_opt = conf_opt;

/*
* Detect any trailing whitespace. This could happen before string
* duplication, but to reduce the number of diffs to a later version,
Expand Down Expand Up @@ -1340,6 +1347,9 @@ configfile_helper(struct bsdtar *bsdtar, const char *line)
conf_arg_malloced = NULL;
}

/* Record pointer for freeing upon error. */
bsdtar->conf_arg = conf_arg;

/* Process the configuration option. */
dooption(bsdtar, conf_opt, conf_arg, 1);

Expand All @@ -1349,6 +1359,10 @@ configfile_helper(struct bsdtar *bsdtar, const char *line)
/* Free memory allocated by strdup. */
free(conf_opt);

/* Memory has been freed. */
bsdtar->conf_arg = NULL;
bsdtar->conf_opt = NULL;

return (0);
}

Expand Down
2 changes: 2 additions & 0 deletions tar/bsdtar.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ struct bsdtar {

/* Config-file parsing strings. */
char *conffile;
char *conf_opt;
char *conf_arg;

/* Used for --dryrun with tarsnap.conf.sample with a missing keyfile. */
int config_file_keyfile_failed;
Expand Down

0 comments on commit 2a88fb5

Please sign in to comment.