All of lore.kernel.org
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: Abhishek Agarwal <agab0323@gmail.com>
Cc: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
	Usman Akinyemi <usmanakinyemi202@gmail.com>,
	Victoria Dye <vdye@github.com>,
	Meet Soni <meetsoni3017@gmail.com>
Subject: Re: [PATCH v2] for-each-ref: avoid segfault on --no-format
Date: Mon, 16 Nov 2026 07:45:45 +0100	[thread overview]
Message-ID: <avqmmRF9tbFLvlM8@pks.im> (raw)
In-Reply-To: <20261114183718.57552-1-agab0323@gmail.com>

On Sun, Nov 15, 2026 at 12:07:18AM +0530, Abhishek Agarwal wrote:
> The "--format" option is declared with OPT_STRING(), so the
> parse-options machinery also accepts "--no-format" and sets the
> format string to NULL. The default format is assigned before the
> command line is parsed, so nothing puts it back, and the NULL is
> dereferenced in verify_ref_format():
> 
>     $ git for-each-ref --no-format
>     Segmentation fault
> 
> "git refs list" shares the code and crashes the same way. The
> problem dates back to c3428da87f (Make builtin-for-each-ref.c use
> parse-opts., 2007-10-13).
> 
> "git branch" and "git tag" have the same option and fall back to
> their default format when it is NULL. Do the same here, and assign
> the default after parsing the command line when no format is left.
> "--no-format" then cancels an earlier "--format" and gives the
> default output.

That behaviour is a bit on the weird side if you ask me, but so be it.
It's definitely better compared to crashing.

> Marking the option with PARSE_OPT_NONEG would also stop the crash,
> but then "--no-format" would work for "git branch" and "git tag"
> and be an error here.

Yeah, being consistent is the better pick, I guess.

> diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
> index 4a2fc421db..4390f3f5e9 100644
> --- a/builtin/for-each-ref.c
> +++ b/builtin/for-each-ref.c
> @@ -52,14 +52,14 @@ int for_each_ref_core(int argc, const char **argv, const char *prefix, struct re
>  		OPT_END(),
>  	};
>  
> -	format.format = "%(objectname) %(objecttype)\t%(refname)";
> -
>  	repo_config(repo, git_default_config, NULL);
>  
>  	/* Set default (refname) sorting */
>  	string_list_append(&sorting_options, "refname");
>  
>  	parse_options(argc, argv, prefix, opts, usage, 0);
> +	if (!format.format)
> +		format.format = "%(objectname) %(objecttype)\t%(refname)";
>  	if (format.array_opts.max_count < 0) {
>  		error("invalid --count argument: `%d'", format.array_opts.max_count);
>  		usage_with_options(usage, opts);

Makes sense.

> diff --git a/t/for-each-ref-tests.sh b/t/for-each-ref-tests.sh
> index 4991a82b91..3e0108a7db 100644
> --- a/t/for-each-ref-tests.sh
> +++ b/t/for-each-ref-tests.sh
> @@ -285,6 +285,12 @@ test_expect_success 'Check invalid atoms names are errors' '
>  	test_must_fail ${git_for_each_ref} --format="%(INVALID)" refs/heads
>  '
>  
> +test_expect_success '--no-format restores the default format' '
> +	${git_for_each_ref} refs/heads >expect &&
> +	${git_for_each_ref} --format="%(refname)" --no-format refs/heads >actual &&
> +	test_cmp expect actual
> +'

And the test looks obviously good to me, as well. So this patch looks
good to me overall, thanks!

Patrick

      reply	other threads:[~2026-11-16  6:45 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13 16:14 [PATCH] for-each-ref: avoid segfault on --no-format aeron-gh
2026-11-13 22:10 ` Junio C Hamano
2026-11-13 23:11   ` Junio C Hamano
2026-11-14  4:53     ` Abhishek Agarwal
2026-11-14 18:37 ` [PATCH v2] " Abhishek Agarwal
2026-11-16  6:45   ` Patrick Steinhardt [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=avqmmRF9tbFLvlM8@pks.im \
    --to=ps@pks.im \
    --cc=agab0323@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=meetsoni3017@gmail.com \
    --cc=usmanakinyemi202@gmail.com \
    --cc=vdye@github.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.