Commit

66f4856110a7577c12f97ae905c95a6f38adba9d

revision: hang on to "freed" argv elements

In setup_revisions() we rewrite the incoming argv array, losing
references to the strings it contains. For a synthetic argv array
constructed from heap strings, that traditionally meant we leaked those
allocated strings.

We fixed the leak in cd43948798 (revision: manage memory ownership of
argv in setup_revisions(), 2025-09-19). Now callers can tell the
revision code that argv entries are allocated and should be freed, which
it will do before overwriting them.

But this introduced a new bug! The overwritten entries go away as soon
as option parsing is finished, but a few options may actually create new
references to those strings. And once we free the strings, those stale
references become use-after-free bugs. For example, running:

  git stash show --src-prefix=foo/

demonstrates the problem:

  1. The stash command generates its own synthetic argv (because it has
     to treat the stash specifiers specially) which it then passes to
     setup_revisions().

  2. Parsing will create a reference to the partial string "foo/" in
     revs.diffopt.a_prefix.

  3. When setup_revisions() finishes, we rewrite argv to throw away
     parsed strings. This frees the entry holding "--src-prefix=foo",
     at which point we have a dangling reference in revs.diffopt.

  4. We generate an actual diff, accessing garbage memory via
     revs.diffopt.a_prefix. The output is usually garbled, but ASan also
     detects this reliably.

One obvious fix here is to allocate new strings when we pull data out of
the argv array. But doing so is error prone (every string option must
remember to do it or risk a subtle bug), and creates more questions
about memory ownership (e.g., some callers assign string literals
directly to a_prefix, and we would not want to free those).

Instead we can fix this centrally by delaying the free() calls. We'll
collect any "freed" strings in a new array, hold on to it for the life
of the rev_info struct, and then release it at the end. We can easily
use a strvec for this, since it handles growth and cleanup for us.

This fixes the prefix case above (which is now tested in t3903), and
should fix any other stray cases. Though I could not find any; we use
OPT_STRING only in the prefix diff options, and very few revision opts
store strings. Those that do (like --format and --encoding) already make
a copy of the string. They do not need for us to hold on to the memory
longer, but it does not hurt them if we do.

One may note that combined with cd43948798 we have approached a simpler
solution in a roundabout way. We are still hacking up argv, but now
carefully constructing a parallel argv of old strings we've overwritten
(and will eventually free). In an alternate universe, we could instead
leave the original argv pristine and return a new reduced-size argv.
This is conceptually simpler, though it does mean that every caller must
free that new argv array itself (not the entries). That's not something
they traditionally had to do, so it would mean tweaking every caller.

So even though the combination of this cd43948798 and this patch is a
little convoluted, it should make things just work (no leaks and no
use-after-free) without modifying any callers.

Reported-by: Nicolas Le Cam <niko.lecam@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King <peff@peff.net> · 2026-09-01 06:28 UTC · committed by Junio C Hamano

Browse files
3 files changed, +54 −8
modified revision.c
@@ -2307,9 +2307,27 @@ static timestamp_t parse_age(const char *arg)
2307 2307 return num;
2308 2308 }
2309 2309
2310 + /*
2311 + * When asked to free argv strings, we should not do so immediately. Some
2312 + * option parsing may have stored a reference to the string (either the whole
2313 + * thing, or a substring inside it). We should keep it valid until the rev_info
2314 + * struct itself is freed.
2315 + *
2316 + * Note that we take a const str for the convenience of callers (who have the
2317 + * usual const argv array, even when opt->free_removed_argv_elements is set).
2318 + * We cast away the const on their behalf.
2319 + */
2320 + static void mark_argv_for_free(struct rev_info *revs, const char *str)
2321 + {
2322 + if (!str)
2323 + return;
2324 + strvec_push_nodup(&revs->argv_to_free, (char *)str);
2325 + }
2326 +
2310 2327 static void overwrite_argv(int *argc, const char **argv,
2311 2328 const char **value,
2312 - const struct setup_revision_opt *opt)
2329 + const struct setup_revision_opt *opt,
2330 + struct rev_info *revs)
2313 2331 {
2314 2332 /*
2315 2333 * Detect the case when we are overwriting ourselves. The assignment
@@ -2318,7 +2336,7 @@ static void overwrite_argv(int *argc, const char **argv,
2318 2336 */
2319 2337 if (*value != argv[*argc]) {
2320 2338 if (opt && opt->free_removed_argv_elements)
2321 - free((char *)argv[*argc]);
2339 + mark_argv_for_free(revs, argv[*argc]);
2322 2340 argv[*argc] = *value;
2323 2341 *value = NULL;
2324 2342 }
@@ -2346,7 +2364,7 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
2346 2364 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2347 2365 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2348 2366 {
2349 - overwrite_argv(unkc, unkv, &argv[0], opt);
2367 + overwrite_argv(unkc, unkv, &argv[0], opt, revs);
2350 2368 return 1;
2351 2369 }
2352 2370
@@ -2738,7 +2756,7 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
2738 2756 } else {
2739 2757 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2740 2758 if (!opts)
2741 - overwrite_argv(unkc, unkv, &argv[0], opt);
2759 + overwrite_argv(unkc, unkv, &argv[0], opt, revs);
2742 2760 return opts;
2743 2761 }
2744 2762
@@ -3038,7 +3056,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
3038 3056 if (strcmp(arg, "--"))
3039 3057 continue;
3040 3058 if (opt && opt->free_removed_argv_elements)
3041 - free((char *)argv[i]);
3059 + mark_argv_for_free(revs, argv[i]);
3042 3060 argv[i] = NULL;
3043 3061 argc = i;
3044 3062 if (argv[i + 1])
@@ -3068,7 +3086,8 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
3068 3086
3069 3087 if (!strcmp(arg, "--stdin")) {
3070 3088 if (revs->disable_stdin) {
3071 - overwrite_argv(&left, argv, &argv[i], opt);
3089 + overwrite_argv(&left, argv, &argv[i],
3090 + opt, revs);
3072 3091 continue;
3073 3092 }
3074 3093 if (revs->read_from_stdin++)
@@ -3242,7 +3261,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
3242 3261
3243 3262 if (argv) {
3244 3263 if (opt && opt->free_removed_argv_elements)
3245 - free((char *)argv[left]);
3264 + mark_argv_for_free(revs, argv[left]);
3246 3265 argv[left] = NULL;
3247 3266 }
3248 3267
@@ -3264,7 +3283,7 @@ void setup_revisions_from_strvec(struct strvec *argv, struct rev_info *revs,
3264 3283 ret = setup_revisions(argv->nr, argv->v, revs, opt);
3265 3284
3266 3285 for (size_t i = ret; i < argv->nr; i++)
3267 - free((char *)argv->v[i]);
3286 + mark_argv_for_free(revs, argv->v[i]);
3268 3287 argv->nr = ret;
3269 3288 }
3270 3289
@@ -3326,6 +3345,7 @@ void release_revisions(struct rev_info *revs)
3326 3345 oidset_clear(&revs->missing_commits);
3327 3346 release_revisions_bloom_keyvecs(revs);
3328 3347 release_follow_pathspec_slab(revs);
3348 + strvec_clear(&revs->argv_to_free);
3329 3349 }
3330 3350
3331 3351 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
modified revision.h
@@ -396,6 +396,14 @@ struct rev_info {
396 396
397 397 /* Missing commits to be tracked without failing traversal. */
398 398 struct oidset missing_commits;
399 +
400 + /*
401 + * Strings whose ownership has been handed over to us, but which
402 + * we may be referencing in any of the above options (including
403 + * within the diffopt struct). These will remain valid until
404 + * release_revisions() is called.
405 + */
406 + struct strvec argv_to_free;
399 407 };
400 408
401 409 /**
@@ -433,6 +441,7 @@ struct rev_info {
433 441 .commit_format = CMIT_FMT_DEFAULT, \
434 442 .expand_tabs_in_log_default = 8, \
435 443 .rdiff_log_arg = STRVEC_INIT, \
444 + .argv_to_free = STRVEC_INIT, \
436 445 }
437 446
438 447 /**
modified t/t3903-stash.sh
@@ -780,6 +780,23 @@ test_expect_success 'stash show --patience shows diff' '
780 780 diff_cmp expected actual
781 781 '
782 782
783 + test_expect_success 'stash show supports prefixes' '
784 + git reset --hard &&
785 + echo foo >>file &&
786 + git stash &&
787 + cat >expected <<-\EOF &&
788 + diff --git foo/file bar/file
789 + index 7601807..71b52c4 100644
790 + --- foo/file
791 + +++ bar/file
792 + @@ -1 +1,2 @@
793 + baz
794 + +foo
795 + EOF
796 + git stash show --src-prefix=foo/ --dst-prefix=bar/ >actual &&
797 + diff_cmp expected actual
798 + '
799 +
783 800 test_expect_success 'drop: fail early if specified stash is not a stash ref' '
784 801 git stash clear &&
785 802 test_when_finished "git reset --hard HEAD && git stash clear" &&