]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/rebase: do not assign default backend to non-constant field
authorPatrick Steinhardt <ps@pks.im>
Fri, 7 Jun 2024 06:39:26 +0000 (08:39 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 7 Jun 2024 17:30:55 +0000 (10:30 -0700)
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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rebase.c

index 14d4f0a5e6dfbbe23c7587d5cfa46cbc1cf1c2cf..adc990b55e62dcc361aeea6222976bc8b8ab881b 100644 (file)
@@ -135,7 +135,7 @@ struct rebase_options {
                .type = REBASE_UNSPECIFIED,             \
                .empty = EMPTY_UNSPECIFIED,             \
                .keep_empty = 1,                        \
-               .default_backend = "merge",             \
+               .default_backend = xstrdup("merge"),    \
                .flags = REBASE_NO_QUIET,               \
                .git_am_opts = STRVEC_INIT,             \
                .exec = STRING_LIST_INIT_NODUP,         \
@@ -151,6 +151,19 @@ struct rebase_options {
                .strategy_opts = STRING_LIST_INIT_NODUP,\
        }
 
+static void rebase_options_release(struct rebase_options *opts)
+{
+       free(opts->default_backend);
+       free(opts->reflog_action);
+       free(opts->head_name);
+       strvec_clear(&opts->git_am_opts);
+       free(opts->gpg_sign_opt);
+       string_list_clear(&opts->exec, 0);
+       free(opts->strategy);
+       string_list_clear(&opts->strategy_opts, 0);
+       strbuf_release(&opts->git_format_patch_opt);
+}
+
 static struct replay_opts get_replay_opts(const struct rebase_options *opts)
 {
        struct replay_opts replay = REPLAY_OPTS_INIT;
@@ -796,6 +809,7 @@ static int rebase_config(const char *var, const char *value,
        }
 
        if (!strcmp(var, "rebase.backend")) {
+               FREE_AND_NULL(opts->default_backend);
                return git_config_string(&opts->default_backend, var, value);
        }
 
@@ -1833,14 +1847,7 @@ run_rebase:
 cleanup:
        strbuf_release(&buf);
        strbuf_release(&revisions);
-       free(options.reflog_action);
-       free(options.head_name);
-       strvec_clear(&options.git_am_opts);
-       free(options.gpg_sign_opt);
-       string_list_clear(&options.exec, 0);
-       free(options.strategy);
-       string_list_clear(&options.strategy_opts, 0);
-       strbuf_release(&options.git_format_patch_opt);
+       rebase_options_release(&options);
        free(squash_onto_name);
        free(keep_base_onto_name);
        return !!ret;