Skip to content

Commit 25a47ff

Browse files
pks-tgitster
authored andcommitted
builtin/rebase: do not assign default backend to non-constant field
The `struct rebase_options::default_backend` field is a non-constant string, but is being assigned a constant via `REBASE_OPTIONS_INIT`. Fix this by using `xstrdup()` to assign the variable and introduce a new function `rebase_options_release()` that releases memory held by the structure, including the newly-allocated variable. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6d1f198 commit 25a47ff

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

builtin/rebase.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ struct rebase_options {
135135
.type = REBASE_UNSPECIFIED, \
136136
.empty = EMPTY_UNSPECIFIED, \
137137
.keep_empty = 1, \
138-
.default_backend = "merge", \
138+
.default_backend = xstrdup("merge"), \
139139
.flags = REBASE_NO_QUIET, \
140140
.git_am_opts = STRVEC_INIT, \
141141
.exec = STRING_LIST_INIT_NODUP, \
@@ -151,6 +151,19 @@ struct rebase_options {
151151
.strategy_opts = STRING_LIST_INIT_NODUP,\
152152
}
153153

154+
static void rebase_options_release(struct rebase_options *opts)
155+
{
156+
free(opts->default_backend);
157+
free(opts->reflog_action);
158+
free(opts->head_name);
159+
strvec_clear(&opts->git_am_opts);
160+
free(opts->gpg_sign_opt);
161+
string_list_clear(&opts->exec, 0);
162+
free(opts->strategy);
163+
string_list_clear(&opts->strategy_opts, 0);
164+
strbuf_release(&opts->git_format_patch_opt);
165+
}
166+
154167
static struct replay_opts get_replay_opts(const struct rebase_options *opts)
155168
{
156169
struct replay_opts replay = REPLAY_OPTS_INIT;
@@ -796,6 +809,7 @@ static int rebase_config(const char *var, const char *value,
796809
}
797810

798811
if (!strcmp(var, "rebase.backend")) {
812+
FREE_AND_NULL(opts->default_backend);
799813
return git_config_string(&opts->default_backend, var, value);
800814
}
801815

@@ -1833,14 +1847,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
18331847
cleanup:
18341848
strbuf_release(&buf);
18351849
strbuf_release(&revisions);
1836-
free(options.reflog_action);
1837-
free(options.head_name);
1838-
strvec_clear(&options.git_am_opts);
1839-
free(options.gpg_sign_opt);
1840-
string_list_clear(&options.exec, 0);
1841-
free(options.strategy);
1842-
string_list_clear(&options.strategy_opts, 0);
1843-
strbuf_release(&options.git_format_patch_opt);
1850+
rebase_options_release(&options);
18441851
free(squash_onto_name);
18451852
free(keep_base_onto_name);
18461853
return !!ret;

0 commit comments

Comments
 (0)