Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Christian Couder <christian.couder@gmail.com>
Cc: git@vger.kernel.org,  Patrick Steinhardt <ps@pks.im>,
	 Elijah Newren <newren@gmail.com>,  Jeff King <peff@peff.net>,
	 "brian m . carlson" <sandals@crustytoothpaste.net>,
	 Johannes Schindelin <Johannes.Schindelin@gmx.de>,
	 Justin Tobler <jltobler@gmail.com>
Subject: Re: [PATCH] git: avoid segfault on "git --shallow-file" without a value
Date: Tue, 11 Aug 2026 12:16:31 -0700	[thread overview]
Message-ID: <xmqqcxvo1n8w.fsf@gitster.g> (raw)
In-Reply-To: <20260811121446.2080190-1-christian.couder@gmail.com> (Christian Couder's message of "Tue, 11 Aug 2026 14:14:46 +0200")

Christian Couder <christian.couder@gmail.com> writes:

A great subject line ;-)  It is the simplest reproducer of any bug.

> In "git.c", the other `handle_options()` options that take their value
> as a separate argument, like `--git-dir`, `--namespace` or `-C`, check
> that such an argument actually exists before using it, and error out
> with a message and the usage string otherwise.
>
> The `--shallow-file` option doesn't perform that check. It blindly
> advances past the option and then dereferences the next element of
> `argv`, which is the NULL terminator when no value was given. So
> `git --shallow-file` segfaults:
>
>   $ git --shallow-file
>   Segmentation fault (core dumped)
> ...
> diff --git a/git.c b/git.c
> index e5f1811b6b..96df15b5cd 100644
> --- a/git.c
> +++ b/git.c
> @@ -304,11 +304,15 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
>  			if (envchanged)
>  				*envchanged = 1;
>  		} else if (!strcmp(cmd, "--shallow-file")) {
> -			(*argv)++;
> -			(*argc)--;
> -			setenv(GIT_SHALLOW_FILE_ENVIRONMENT, (*argv)[0], 1);
> +			if (*argc < 2) {
> +				fprintf(stderr, _("no file given for '%s' option\n" ), "--shallow-file");
> +				usage(git_usage_string);
> +			}
> +			setenv(GIT_SHALLOW_FILE_ENVIRONMENT, (*argv)[1], 1);
>  			if (envchanged)
>  				*envchanged = 1;
> +			(*argv)++;
> +			(*argc)--;

It is curious that the fix needs to be so big, when the only change
necessary, as far as I can tell from your problem description, is to
insert 4 line "if (... not enough args ...) { ... barf and die ...}"
block and without anything else.  I think the culprit is this "while
at it" ...

> While at it, let's also set the environment variable before advancing
> past the option, instead of advancing first and using `(*argv)[0]`, so
> that this option looks like the other ones.

... that made the patch more confusing to read than otherwise.

But without reading the preimage of the patch, the result is just as
understandable ;-)  Let's take the patch as-is.

> +test_expect_success 'git --shallow-file without a value' '
> +	test_must_fail git --shallow-file >actual 2>actual.err &&
> +	test_line_count = 0 actual &&
> +	test_grep "no file given for " actual.err &&
> +	test_grep "usage" actual.err
> +'

Do we have similar "oops, you were supposed to give me a value" test
for other things like "--config-env=", "-C", etc.?  Just being
curious, because (1) if there are, this addition belongs there, not
here, and (2) if there aren't, this addition may not be needed, and
(3) if there aren't or if the existing coverage is incomplete,
perhaps we should give a more complete coverage while at it.

With (3), I mean something along the lines of ...

	for opt in -C -c --git-dir --work-tree --namespace --config-env
	do
		test_expect_success "git $opt without a value" '
			test_must_fail git $opt >actual 2>error &&
			test_line_count 0 actual &&
			test_grep usage error
		'
	done

I do not mean to say that (3) is my favorite among these three,
though.

      reply	other threads:[~2026-08-11 19:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-11 12:14 [PATCH] git: avoid segfault on "git --shallow-file" without a value Christian Couder
2026-08-11 19:16 ` Junio C Hamano [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqcxvo1n8w.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=jltobler@gmail.com \
    --cc=newren@gmail.com \
    --cc=peff@peff.net \
    --cc=ps@pks.im \
    --cc=sandals@crustytoothpaste.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox