Skip to content

Commit

Permalink
Free conf_arg upon error in config-file parsing
Browse files Browse the repository at this point in the history
This moves conf_arg_malloced into the "global" bsdtar struct because if
there's a config file error, we'll exit inside dooption(), leaving the
previous local variable conf_arg_malloced un-freed.
  • Loading branch information
gperciva committed Feb 27, 2016
1 parent 5bfc267 commit 5be74bc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
12 changes: 7 additions & 5 deletions tar/bsdtar.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ bsdtar_init(void)
bsdtar->keyfile = 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 Down Expand Up @@ -198,6 +199,7 @@ bsdtar_atexit(void)
free(bsdtar->keyfile);
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 @@ -1298,7 +1300,6 @@ configfile_helper(struct bsdtar *bsdtar, const char *line)
{
char * conf_arg;
size_t optlen;
char * conf_arg_malloced;
size_t len;

/* Skip any leading whitespace. */
Expand Down Expand Up @@ -1357,21 +1358,22 @@ configfile_helper(struct bsdtar *bsdtar, const char *line)
if ((conf_arg != NULL) && (conf_arg[0] == '~') &&
(bsdtar->homedir != NULL)) {
/* Construct expanded argument string. */
if (asprintf(&conf_arg_malloced, "%s%s",
if (asprintf(&bsdtar->conf_arg, "%s%s",
bsdtar->homedir, &conf_arg[1]) == -1)
bsdtar_errc(bsdtar, 1, errno, "Out of memory");

/* Use the expanded argument string hereafter. */
conf_arg = conf_arg_malloced;
conf_arg = bsdtar->conf_arg;
} else {
conf_arg_malloced = NULL;
bsdtar->conf_arg = NULL;
}

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

/* Free expanded argument or NULL. */
free(conf_arg_malloced);
free(bsdtar->conf_arg);
bsdtar->conf_arg = NULL;

/* Free memory allocated by strdup. */
free(bsdtar->conf_opt);
Expand Down
1 change: 1 addition & 0 deletions tar/bsdtar.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ 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 5be74bc

Please sign in to comment.