builtin/merge: always store allocated strings in `pull_twohead`

The `pull_twohead` configuration may sometimes contain an allocated string, and sometimes it may contain a string constant. Refactor this to instead always store an allocated string such that we can release its resources without risk. While at it, manage the lifetime of other config strings, as well. Note that we explicitly don't free `cleanup_arg` here. This is because the variable may be assigned a string constant via command line options. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 7, 2024 at 08:39 UTC 71e01a0ebd50fed1d494e1b05374ec0977437248
1 file changed +11 -7
builtin/merge.c
+11 -7
@@ -611,17 +611,19 @@ static int git_merge_config(const char *k, const char *v,
611 return 0;
612 }
613
614 - if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
614 + if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat")) {
615 show_diffstat = git_config_bool(k, v);
616 - else if (!strcmp(k, "merge.verifysignatures"))
616 + } else if (!strcmp(k, "merge.verifysignatures")) {
617 verify_signatures = git_config_bool(k, v);
618 - else if (!strcmp(k, "pull.twohead"))
618 + } else if (!strcmp(k, "pull.twohead")) {
619 + FREE_AND_NULL(pull_twohead);
620 return git_config_string(&pull_twohead, k, v);
620 - else if (!strcmp(k, "pull.octopus"))
621 + } else if (!strcmp(k, "pull.octopus")) {
622 + FREE_AND_NULL(pull_octopus);
623 return git_config_string(&pull_octopus, k, v);
622 - else if (!strcmp(k, "commit.cleanup"))
624 + } else if (!strcmp(k, "commit.cleanup")) {
625 return git_config_string(&cleanup_arg, k, v);
624 - else if (!strcmp(k, "merge.ff")) {
626 + } else if (!strcmp(k, "merge.ff")) {
627 int boolval = git_parse_maybe_bool(v);
628 if (0 <= boolval) {
629 fast_forward = boolval ? FF_ALLOW : FF_NO;
@@ -1294,7 +1296,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1296 if (!pull_twohead) {
1297 char *default_strategy = getenv("GIT_TEST_MERGE_ALGORITHM");
1298 if (default_strategy && !strcmp(default_strategy, "ort"))
1297 - pull_twohead = "ort";
1299 + pull_twohead = xstrdup("ort");
1300 }
1301
1302 init_diff_ui_defaults();
@@ -1793,6 +1795,8 @@ done:
1795 }
1796 strbuf_release(&buf);
1797 free(branch_to_free);
1798 + free(pull_twohead);
1799 + free(pull_octopus);
1800 discard_index(the_repository->index);
1801 return ret;
1802 }