Skip to content

Commit 03a4e26

Browse files
dschogitster
authored andcommitted
sequencer: plug memory leaks for the option values
The sequencer is our attempt to lib-ify cherry-pick. Yet it behaves like a one-shot command when it reads its configuration: memory is allocated and released only when the command exits. This is kind of okay for git-cherry-pick, which *is* a one-shot command. All the work to make the sequencer its work horse was done to allow using the functionality as a library function, though, including proper clean-up after use. To remedy that, take custody of the option values in question, allocating and duping literal constants as needed and freeing them at end. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 285abf5 commit 03a4e26

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

builtin/revert.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
174174

175175
if (argc > 1)
176176
usage_with_options(usage_str, options);
177+
178+
/* These option values will be free()d */
179+
opts->gpg_sign = xstrdup_or_null(opts->gpg_sign);
180+
opts->strategy = xstrdup_or_null(opts->strategy);
177181
}
178182

179183
int cmd_revert(int argc, const char **argv, const char *prefix)

sequencer.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
117117
static void remove_sequencer_state(const struct replay_opts *opts)
118118
{
119119
struct strbuf dir = STRBUF_INIT;
120+
int i;
121+
122+
free(opts->gpg_sign);
123+
free(opts->strategy);
124+
for (i = 0; i < opts->xopts_nr; i++)
125+
free(opts->xopts[i]);
126+
free(opts->xopts);
120127

121128
strbuf_addf(&dir, "%s", get_dir(opts));
122129
remove_dir_recursively(&dir, 0);
@@ -280,7 +287,7 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
280287
struct merge_options o;
281288
struct tree *result, *next_tree, *base_tree, *head_tree;
282289
int clean;
283-
const char **xopt;
290+
char **xopt;
284291
static struct lock_file index_lock;
285292

286293
hold_locked_index(&index_lock, 1);
@@ -583,7 +590,8 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
583590

584591
commit_list_insert(base, &common);
585592
commit_list_insert(next, &remotes);
586-
res |= try_merge_command(opts->strategy, opts->xopts_nr, opts->xopts,
593+
res |= try_merge_command(opts->strategy,
594+
opts->xopts_nr, (const char **)opts->xopts,
587595
common, sha1_to_hex(head), remotes);
588596
free_commit_list(common);
589597
free_commit_list(remotes);
@@ -783,6 +791,16 @@ static int read_populate_todo(struct commit_list **todo_list,
783791
return 0;
784792
}
785793

794+
static int git_config_string_dup(char **dest,
795+
const char *var, const char *value)
796+
{
797+
if (!value)
798+
return config_error_nonbool(var);
799+
free(*dest);
800+
*dest = xstrdup(value);
801+
return 0;
802+
}
803+
786804
static int populate_opts_cb(const char *key, const char *value, void *data)
787805
{
788806
struct replay_opts *opts = data;
@@ -803,9 +821,9 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
803821
else if (!strcmp(key, "options.mainline"))
804822
opts->mainline = git_config_int(key, value);
805823
else if (!strcmp(key, "options.strategy"))
806-
git_config_string(&opts->strategy, key, value);
824+
git_config_string_dup(&opts->strategy, key, value);
807825
else if (!strcmp(key, "options.gpg-sign"))
808-
git_config_string(&opts->gpg_sign, key, value);
826+
git_config_string_dup(&opts->gpg_sign, key, value);
809827
else if (!strcmp(key, "options.strategy-option")) {
810828
ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
811829
opts->xopts[opts->xopts_nr++] = xstrdup(value);

sequencer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ struct replay_opts {
3434

3535
int mainline;
3636

37-
const char *gpg_sign;
37+
char *gpg_sign;
3838

3939
/* Merge strategy */
40-
const char *strategy;
41-
const char **xopts;
40+
char *strategy;
41+
char **xopts;
4242
size_t xopts_nr, xopts_alloc;
4343

4444
/* Only used by REPLAY_NONE */

0 commit comments

Comments
 (0)