git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] for-each-ref: avoid segfault on --no-format

From
Aaeron-gh <agab0323@gmail.com>
Date
Nov 13, 2026, 16:14 UTC
Message-ID
<20261113161413.15688-1-agab0323@gmail.com>

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.

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.

Signed-off-by: aeron-gh <agab0323@gmail.com>
---
 builtin/for-each-ref.c  | 4 ++--
 t/for-each-ref-tests.sh | 6 ++++++
 2 files changed, 8 insertions(+), 2 deletions(-)
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);
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
+'
+
 test_expect_success 'Check format specifiers are ignored in naming date atoms' '
 	${git_for_each_ref} --format="%(authordate)" refs/heads &&
 	${git_for_each_ref} --format="%(authordate:default) %(authordate)" refs/heads &&
-- 
2.56.0
Next: Junio C Hamano
Message 1 of 6 in “for-each-ref: avoid segfault on --no-format”
  1. for-each-ref: avoid segfault on --no-formataeron-gh, Nov 13, 2026
  2. Junio C HamanoNov 13, 2026
  3. Junio C HamanoNov 13, 2026
  4. Abhishek AgarwalNov 14, 2026
  5. for-each-ref: avoid segfault on --no-formatAbhishek Agarwal, Nov 14, 2026
  6. Patrick SteinhardtNov 16, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.