Skip to content

Commit fc06676

Browse files
pks-tgitster
authored andcommitted
builtin/rebase: always store allocated string in options.strategy
The `struct rebase_options::strategy` field is a `char *`, but we do end up assigning string constants to it in two cases: - When being passed a `--strategy=` option via the command line. - When being passed a strategy option via `--strategy-option=`, but not a strategy. This will cause warnings once we enable `-Wwrite-strings`. Ideally, we'd just convert the field to be a `const char *`. But we also assign to this field via the GIT_TEST_MERGE_ALGORITHM envvar, which we have to strdup(3P) into it. Instead, refactor the code to make sure that we only ever assign allocated strings to this field. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 25a47ff commit fc06676

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

builtin/rebase.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
10611061
{
10621062
struct rebase_options options = REBASE_OPTIONS_INIT;
10631063
const char *branch_name;
1064+
const char *strategy_opt = NULL;
10641065
int ret, flags, total_argc, in_progress = 0;
10651066
int keep_base = 0;
10661067
int ok_to_skip_pre_rebase = 0;
@@ -1175,7 +1176,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
11751176
PARSE_OPT_OPTARG, parse_opt_rebase_merges),
11761177
OPT_BOOL(0, "fork-point", &options.fork_point,
11771178
N_("use 'merge-base --fork-point' to refine upstream")),
1178-
OPT_STRING('s', "strategy", &options.strategy,
1179+
OPT_STRING('s', "strategy", &strategy_opt,
11791180
N_("strategy"), N_("use the given merge strategy")),
11801181
OPT_STRING_LIST('X', "strategy-option", &options.strategy_opts,
11811182
N_("option"),
@@ -1484,13 +1485,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
14841485
}
14851486
}
14861487

1487-
if (options.strategy_opts.nr && !options.strategy)
1488-
options.strategy = "ort";
1489-
1490-
if (options.strategy) {
1491-
options.strategy = xstrdup(options.strategy);
1488+
if (strategy_opt)
1489+
options.strategy = xstrdup(strategy_opt);
1490+
else if (options.strategy_opts.nr && !options.strategy)
1491+
options.strategy = xstrdup("ort");
1492+
if (options.strategy)
14921493
imply_merge(&options, "--strategy");
1493-
}
14941494

14951495
if (options.root && !options.onto_name)
14961496
imply_merge(&options, "--root without --onto");

0 commit comments

Comments
 (0)