]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/pull: fix leaking "ff" option
authorPatrick Steinhardt <ps@pks.im>
Thu, 26 Sep 2024 11:46:26 +0000 (13:46 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 27 Sep 2024 15:25:35 +0000 (08:25 -0700)
The `opt_ff` field gets populated either via `OPT_PASSTHRU` via
`config_get_ff()` or when `--rebase` is passed. So we sometimes end up
overriding the value in `opt_ff` with another value, but we do not free
the old value, causing a memory leak.

Adapt the type of the variable to be `char *` and consistently assign
allocated strings to it such that we can easily free it when it is being
overridden.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/pull.c
t/t7601-merge-pull-config.sh

index 4c54d8196fac6e629afa486bd6fe7c9d4b6e0945..5d9d9e467e508d86594dc9b8b9c17ec07909ed3c 100644 (file)
@@ -84,7 +84,7 @@ static const char *opt_squash;
 static const char *opt_commit;
 static const char *opt_edit;
 static const char *cleanup_arg;
-static const char *opt_ff;
+static char *opt_ff;
 static const char *opt_verify_signatures;
 static const char *opt_verify;
 static int opt_autostash = -1;
@@ -1024,8 +1024,10 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
                 * "--rebase" can override a config setting of
                 * pull.ff=only.
                 */
-               if (opt_rebase >= 0 && opt_ff && !strcmp(opt_ff, "--ff-only"))
-                       opt_ff = "--ff";
+               if (opt_rebase >= 0 && opt_ff && !strcmp(opt_ff, "--ff-only")) {
+                       free(opt_ff);
+                       opt_ff = xstrdup("--ff");
+               }
        }
 
        if (opt_rebase < 0)
@@ -1135,7 +1137,8 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
 
                if (can_ff) {
                        /* we can fast-forward this without invoking rebase */
-                       opt_ff = "--ff-only";
+                       free(opt_ff);
+                       opt_ff = xstrdup("--ff-only");
                        ret = run_merge();
                } else {
                        ret = run_rebase(&newbase, &upstream);
index a94387a75f2f488ba6b48ddf4658822d08fc7908..7fd8c086af3f16d2d1de1721fe10605258ee966a 100755 (executable)
@@ -4,6 +4,7 @@ test_description='git merge
 
 Testing pull.* configuration parsing and other things.'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '