{"thread":{"id":"66847","subject":"[PATCH] for-each-ref: avoid segfault on --no-format","startedAt":"2026-11-13T16:14:10Z","lastAt":"2026-11-16T06:45:45Z","messageCount":6,"participants":["aeron-gh","Junio C Hamano","Abhishek Agarwal","Patrick Steinhardt"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"558405","messageId":"20261113161413.15688-1-agab0323@gmail.com","threadId":"66847","inReplyTo":null,"subject":"[PATCH] for-each-ref: avoid segfault on --no-format","fromName":"aeron-gh","fromEmail":"agab0323@gmail.com","sentAt":"2026-11-13T16:14:10Z","receivedAt":"2026-11-13T16:14:10Z","isPatch":true,"sender":{"key":"agab0323@gmail.com","avatar":null},"body":"The \"--format\" option is declared with OPT_STRING(), so the\nparse-options machinery also accepts \"--no-format\" and sets the\nformat string to NULL. The default format is assigned before the\ncommand line is parsed, so nothing puts it back, and the NULL is\ndereferenced in verify_ref_format():\n\n    $ git for-each-ref --no-format\n    Segmentation fault\n\n\"git refs list\" shares the code and crashes the same way. The\nproblem dates back to c3428da87f (Make builtin-for-each-ref.c use\nparse-opts., 2007-10-13).\n\n\"git branch\" and \"git tag\" have the same option and fall back to\ntheir default format when it is NULL. Do the same here, and assign\nthe default after parsing the command line when no format is left.\n\"--no-format\" then cancels an earlier \"--format\" and gives the\ndefault output.\n\nMarking the option with PARSE_OPT_NONEG would also stop the crash,\nbut then \"--no-format\" would work for \"git branch\" and \"git tag\"\nand be an error here.\n\nSigned-off-by: aeron-gh <agab0323@gmail.com>\n---\n builtin/for-each-ref.c  | 4 ++--\n t/for-each-ref-tests.sh | 6 ++++++\n 2 files changed, 8 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex 4a2fc421db..4390f3f5e9 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -52,14 +52,14 @@ int for_each_ref_core(int argc, const char **argv, const char *prefix, struct re\n \t\tOPT_END(),\n \t};\n \n-\tformat.format = \"%(objectname) %(objecttype)\\t%(refname)\";\n-\n \trepo_config(repo, git_default_config, NULL);\n \n \t/* Set default (refname) sorting */\n \tstring_list_append(&sorting_options, \"refname\");\n \n \tparse_options(argc, argv, prefix, opts, usage, 0);\n+\tif (!format.format)\n+\t\tformat.format = \"%(objectname) %(objecttype)\\t%(refname)\";\n \tif (format.array_opts.max_count < 0) {\n \t\terror(\"invalid --count argument: `%d'\", format.array_opts.max_count);\n \t\tusage_with_options(usage, opts);\ndiff --git a/t/for-each-ref-tests.sh b/t/for-each-ref-tests.sh\nindex 4991a82b91..3e0108a7db 100644\n--- a/t/for-each-ref-tests.sh\n+++ b/t/for-each-ref-tests.sh\n@@ -285,6 +285,12 @@ test_expect_success 'Check invalid atoms names are errors' '\n \ttest_must_fail ${git_for_each_ref} --format=\"%(INVALID)\" refs/heads\n '\n \n+test_expect_success '--no-format restores the default format' '\n+\t${git_for_each_ref} refs/heads >expect &&\n+\t${git_for_each_ref} --format=\"%(refname)\" --no-format refs/heads >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'Check format specifiers are ignored in naming date atoms' '\n \t${git_for_each_ref} --format=\"%(authordate)\" refs/heads &&\n \t${git_for_each_ref} --format=\"%(authordate:default) %(authordate)\" refs/heads &&\n-- \n2.56.0\n\n\n"},{"id":"558428","messageId":"xmqqjymg9ycq.fsf@gitster.g","threadId":"66847","inReplyTo":"20261113161413.15688-1-agab0323@gmail.com","subject":"Re: [PATCH] for-each-ref: avoid segfault on --no-format","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-13T22:10:29Z","receivedAt":"2026-11-13T22:10:29Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"aeron-gh <agab0323@gmail.com> writes:\n\n> The \"--format\" option is declared with OPT_STRING(), so the\n> parse-options machinery also accepts \"--no-format\" and sets the\n> format string to NULL. The default format is assigned before the\n> command line is parsed, so nothing puts it back, and the NULL is\n> dereferenced in verify_ref_format():\n>\n>     $ git for-each-ref --no-format\n>     Segmentation fault\n\nThat is bad.  PARSE_OPT_NONEG is necessary, I guess?  Another\nplausible interpretation may be to exit without showing anything,\nbut that does not sound like a very useful behaviour.\n\n> Marking the option with PARSE_OPT_NONEG would also stop the crash,\n> but then \"--no-format\" would work for \"git branch\" and \"git tag\"\n> and be an error here.\n\nShouldn't \"git branch --no-format\" also error out?\n\n> Signed-off-by: aeron-gh <agab0323@gmail.com>\n\nPlease check [[dco]] and [[real-name]] A few sections in\nDocumentation/SubmittingPatches.\n\nThanks.\n\n"},{"id":"558434","messageId":"xmqqse148gyf.fsf@gitster.g","threadId":"66847","inReplyTo":"xmqqjymg9ycq.fsf@gitster.g","subject":"Re: [PATCH] for-each-ref: avoid segfault on --no-format","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-13T23:11:36Z","receivedAt":"2026-11-13T23:11:36Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n>> Marking the option with PARSE_OPT_NONEG would also stop the crash,\n>> but then \"--no-format\" would work for \"git branch\" and \"git tag\"\n>> and be an error here.\n>\n> Shouldn't \"git branch --no-format\" also error out?\n\nHmph, \"git branch --no-format\" seems to take it as an instruction to\nrevert to the default format.  Making \"--no-format\" mean the same\nthing in \"for-each-ref\" may make sense.\n\nThe implementation of \"git branch\" side is quite clean; if\nformat.format has a value, use it, or fill it with default.  Perhaps\nmimicking it would give us the right behaviour?  I didn't check.\n\n"},{"id":"558445","messageId":"20261114045358.26780-1-agab0323@gmail.com","threadId":"66847","inReplyTo":"xmqqse148gyf.fsf@gitster.g","subject":"Re: [PATCH] for-each-ref: avoid segfault on --no-format","fromName":"Abhishek Agarwal","fromEmail":"agab0323@gmail.com","sentAt":"2026-11-14T04:53:58Z","receivedAt":"2026-11-14T04:53:58Z","isPatch":true,"sender":{"key":"agab0323@gmail.com","avatar":null},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> The implementation of \"git branch\" side is quite clean; if\n> format.format has a value, use it, or fill it with default.  Perhaps\n> mimicking it would give us the right behaviour?  I didn't check.\n\nYes, the patch does the same thing.  After parse_options() it has\n\n\tif (!format.format)\n\t\tformat.format = \"%(objectname) %(objecttype)\\t%(refname)\";\n\nso \"--no-format\" gives the default output, like it does for \"git\nbranch\" and \"git tag\".  The new test checks that \"--format=<fmt>\n--no-format\" prints the same as giving no option at all.\n\nI will send a v2 with the name in the sign-off fixed.  I do not plan\nany code change, unless u see something else.\n\nThanks.\n\n"},{"id":"558470","messageId":"20261114183718.57552-1-agab0323@gmail.com","threadId":"66847","inReplyTo":"20261113161413.15688-1-agab0323@gmail.com","subject":"[PATCH v2] for-each-ref: avoid segfault on --no-format","fromName":"Abhishek Agarwal","fromEmail":"agab0323@gmail.com","sentAt":"2026-11-14T18:37:18Z","receivedAt":"2026-11-14T18:37:18Z","isPatch":true,"sender":{"key":"agab0323@gmail.com","avatar":null},"body":"The \"--format\" option is declared with OPT_STRING(), so the\nparse-options machinery also accepts \"--no-format\" and sets the\nformat string to NULL. The default format is assigned before the\ncommand line is parsed, so nothing puts it back, and the NULL is\ndereferenced in verify_ref_format():\n\n    $ git for-each-ref --no-format\n    Segmentation fault\n\n\"git refs list\" shares the code and crashes the same way. The\nproblem dates back to c3428da87f (Make builtin-for-each-ref.c use\nparse-opts., 2007-10-13).\n\n\"git branch\" and \"git tag\" have the same option and fall back to\ntheir default format when it is NULL. Do the same here, and assign\nthe default after parsing the command line when no format is left.\n\"--no-format\" then cancels an earlier \"--format\" and gives the\ndefault output.\n\nMarking the option with PARSE_OPT_NONEG would also stop the crash,\nbut then \"--no-format\" would work for \"git branch\" and \"git tag\"\nand be an error here.\n\nSigned-off-by: Abhishek Agarwal <agab0323@gmail.com>\n---\n\nNotes:\n    Changes since v1:\n    - Use \"Abhishek Agarwal\" in the author and Signed-off-by lines\n      instead of \"aeron-gh\".\n    - No code change.\n\n builtin/for-each-ref.c  | 4 ++--\n t/for-each-ref-tests.sh | 6 ++++++\n 2 files changed, 8 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex 4a2fc421db..4390f3f5e9 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -52,14 +52,14 @@ int for_each_ref_core(int argc, const char **argv, const char *prefix, struct re\n \t\tOPT_END(),\n \t};\n \n-\tformat.format = \"%(objectname) %(objecttype)\\t%(refname)\";\n-\n \trepo_config(repo, git_default_config, NULL);\n \n \t/* Set default (refname) sorting */\n \tstring_list_append(&sorting_options, \"refname\");\n \n \tparse_options(argc, argv, prefix, opts, usage, 0);\n+\tif (!format.format)\n+\t\tformat.format = \"%(objectname) %(objecttype)\\t%(refname)\";\n \tif (format.array_opts.max_count < 0) {\n \t\terror(\"invalid --count argument: `%d'\", format.array_opts.max_count);\n \t\tusage_with_options(usage, opts);\ndiff --git a/t/for-each-ref-tests.sh b/t/for-each-ref-tests.sh\nindex 4991a82b91..3e0108a7db 100644\n--- a/t/for-each-ref-tests.sh\n+++ b/t/for-each-ref-tests.sh\n@@ -285,6 +285,12 @@ test_expect_success 'Check invalid atoms names are errors' '\n \ttest_must_fail ${git_for_each_ref} --format=\"%(INVALID)\" refs/heads\n '\n \n+test_expect_success '--no-format restores the default format' '\n+\t${git_for_each_ref} refs/heads >expect &&\n+\t${git_for_each_ref} --format=\"%(refname)\" --no-format refs/heads >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'Check format specifiers are ignored in naming date atoms' '\n \t${git_for_each_ref} --format=\"%(authordate)\" refs/heads &&\n \t${git_for_each_ref} --format=\"%(authordate:default) %(authordate)\" refs/heads &&\n-- \n2.56.0\n\n\n"},{"id":"558515","messageId":"avqmmRF9tbFLvlM8@pks.im","threadId":"66847","inReplyTo":"20261114183718.57552-1-agab0323@gmail.com","subject":"Re: [PATCH v2] for-each-ref: avoid segfault on --no-format","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-11-16T06:45:45Z","receivedAt":"2026-11-16T06:45:45Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Sun, Nov 15, 2026 at 12:07:18AM +0530, Abhishek Agarwal wrote:\n> The \"--format\" option is declared with OPT_STRING(), so the\n> parse-options machinery also accepts \"--no-format\" and sets the\n> format string to NULL. The default format is assigned before the\n> command line is parsed, so nothing puts it back, and the NULL is\n> dereferenced in verify_ref_format():\n> \n>     $ git for-each-ref --no-format\n>     Segmentation fault\n> \n> \"git refs list\" shares the code and crashes the same way. The\n> problem dates back to c3428da87f (Make builtin-for-each-ref.c use\n> parse-opts., 2007-10-13).\n> \n> \"git branch\" and \"git tag\" have the same option and fall back to\n> their default format when it is NULL. Do the same here, and assign\n> the default after parsing the command line when no format is left.\n> \"--no-format\" then cancels an earlier \"--format\" and gives the\n> default output.\n\nThat behaviour is a bit on the weird side if you ask me, but so be it.\nIt's definitely better compared to crashing.\n\n> Marking the option with PARSE_OPT_NONEG would also stop the crash,\n> but then \"--no-format\" would work for \"git branch\" and \"git tag\"\n> and be an error here.\n\nYeah, being consistent is the better pick, I guess.\n\n> diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\n> index 4a2fc421db..4390f3f5e9 100644\n> --- a/builtin/for-each-ref.c\n> +++ b/builtin/for-each-ref.c\n> @@ -52,14 +52,14 @@ int for_each_ref_core(int argc, const char **argv, const char *prefix, struct re\n>  \t\tOPT_END(),\n>  \t};\n>  \n> -\tformat.format = \"%(objectname) %(objecttype)\\t%(refname)\";\n> -\n>  \trepo_config(repo, git_default_config, NULL);\n>  \n>  \t/* Set default (refname) sorting */\n>  \tstring_list_append(&sorting_options, \"refname\");\n>  \n>  \tparse_options(argc, argv, prefix, opts, usage, 0);\n> +\tif (!format.format)\n> +\t\tformat.format = \"%(objectname) %(objecttype)\\t%(refname)\";\n>  \tif (format.array_opts.max_count < 0) {\n>  \t\terror(\"invalid --count argument: `%d'\", format.array_opts.max_count);\n>  \t\tusage_with_options(usage, opts);\n\nMakes sense.\n\n> diff --git a/t/for-each-ref-tests.sh b/t/for-each-ref-tests.sh\n> index 4991a82b91..3e0108a7db 100644\n> --- a/t/for-each-ref-tests.sh\n> +++ b/t/for-each-ref-tests.sh\n> @@ -285,6 +285,12 @@ test_expect_success 'Check invalid atoms names are errors' '\n>  \ttest_must_fail ${git_for_each_ref} --format=\"%(INVALID)\" refs/heads\n>  '\n>  \n> +test_expect_success '--no-format restores the default format' '\n> +\t${git_for_each_ref} refs/heads >expect &&\n> +\t${git_for_each_ref} --format=\"%(refname)\" --no-format refs/heads >actual &&\n> +\ttest_cmp expect actual\n> +'\n\nAnd the test looks obviously good to me, as well. So this patch looks\ngood to me overall, thanks!\n\nPatrick\n\n"}]}