]> git.ipfire.org Git - thirdparty/git.git/commitdiff
stash: tell setup_revisions() to free our allocated strings
authorJeff King <peff@peff.net>
Fri, 19 Sep 2025 22:40:27 +0000 (18:40 -0400)
committerJunio C Hamano <gitster@pobox.com>
Sun, 21 Sep 2025 20:46:12 +0000 (13:46 -0700)
In "git stash show", we do a first pass of parsing our command line
options by splitting them into revision args and stash args. These are
stored in strvecs, and we pass the revision args to setup_revisions().

But setup_revisions() may modify the argv we pass it, causing us to leak
some of the entries. In particular, if it sees a "--" string, that will
be dropped from argv. This is the same as other cases addressed by
f92dbdbc6a (revisions API: don't leak memory on argv elements that need
free()-ing, 2022-08-02), and we should fix it the same way: by passing
the free_removed_argv_elements option to setup_revisions().

I've added a test here which fails when built with SANITIZE=leak because
it calls "git stash show --". This by itself is not a very
interesting invocation, because there is nothing after the "--", and
thus the "--" is not really doing anything.

But I think the current parsing in show_stash() is a little
questionable. It splits the arguments into revision options and stash
options solely based on the presence of a leading dash, with no regard
to "--" at all. So:

  git stash show -- foo

will take "foo" as a stash option before we even pass anything to
setup_revisions(). And something like:

  git stash show -- 1

will show stash@{1}. But I would expect anything after the "--" to be a
pathspec. So in this example it would show only the part of the diff
that touched "foo". And something like:

  git stash show -p 1 -- foo

would treat "1" as a stash and "foo" as a pathspec.

That may be something we want to fix, but I want to focus here on the
leak-fixing without changing behavior. So this test is a little odd, but
does what we want without locking us in to any particular behavior (we
only care that "--" by itself does not change the output nor leak).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/stash.c
t/t3903-stash.sh

index 1977e50df27fc57b01dbc0a2d4485d0aadf8aaaf..01751ce28d625dfc558e25ed260244c5ef38d6af 100644 (file)
@@ -956,6 +956,7 @@ static void diff_include_untracked(const struct stash_info *info, struct diff_op
 static int show_stash(int argc, const char **argv, const char *prefix,
                      struct repository *repo UNUSED)
 {
+       struct setup_revision_opt opt = { .free_removed_argv_elements = 1 };
        int i;
        int ret = -1;
        struct stash_info info = STASH_INFO_INIT;
@@ -1014,7 +1015,7 @@ static int show_stash(int argc, const char **argv, const char *prefix,
                }
        }
 
-       argc = setup_revisions(revision_args.nr, revision_args.v, &rev, NULL);
+       argc = setup_revisions(revision_args.nr, revision_args.v, &rev, &opt);
        if (argc > 1)
                goto usage;
        if (!rev.diffopt.output_format) {
index 0bb4648e3639b23da75c7dba000d272d0e8bab1b..1c9e589bbec55e05492980cd686ead5c378e5f6e 100755 (executable)
@@ -1741,4 +1741,10 @@ test_expect_success 'submodules does not affect the branch recorded in stash mes
        )
 '
 
+test_expect_success 'stash show handles --' '
+       git stash show >expect &&
+       git stash show -- >actual &&
+       test_cmp expect actual
+'
+
 test_done