for-each-ref: avoid segfault on --no-format
PATCH 1/1 for-each-ref: avoid segfault on --no-format
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.
--- builtin/for-each-ref.c | 4 ++-- t/for-each-ref-tests.sh | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-)
builtin/for-each-ref.c +2 -2
@@ -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);
t/for-each-ref-tests.sh +6 -0
@@ -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 &&
Signature · 1 line
2.56.0
Quoted 8 lines · aeron-gh
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 inverify_ref_format():$ git for-each-ref --no-format Segmentation fault
That is bad. PARSE_OPT_NONEG is necessary, I guess? Another plausible interpretation may be to exit without showing anything, but that does not sound like a very useful behaviour.
Quoted 3 lines
Marking the option with
PARSE_OPT_NONEGwould also stop the crash, but then "--no-format" would work for "git branch" and "git tag" and be an error here.
Shouldn't "git branch --no-format" also error out?
Quoted 1 line
Signed-off-by aeron-gh <[email protected]>
Please check [[dco]] and [[real-name]] A few sections in Documentation/SubmittingPatches.
Thanks.
Quoted 5 lines · Junio C Hamano
Quoted 3 lines
Marking the option with
PARSE_OPT_NONEGwould also stop the crash, but then "--no-format" would work for "git branch" and "git tag" and be an error here.Shouldn't "git branch --no-format" also error out?
Hmph, "git branch --no-format" seems to take it as an instruction to revert to the default format. Making "--no-format" mean the same thing in "for-each-ref" may make sense.
The implementation of "git branch" side is quite clean; if format.format has a value, use it, or fill it with default. Perhaps mimicking it would give us the right behaviour? I didn't check.
Quoted 3 lines · Junio C Hamano
The implementation of "git branch" side is quite clean; if format.format has a value, use it, or fill it with default. Perhaps mimicking it would give us the right behaviour? I didn't check.
Yes, the patch does the same thing. After parse_options() it has
if (!format.format) format.format = "%(objectname) %(objecttype)\t%(refname)";
so "--no-format" gives the default output, like it does for "git branch" and "git tag". The new test checks that "--format=<fmt> --no-format" prints the same as giving no option at all.
I will send a v2 with the name in the sign-off fixed. I do not plan any code change, unless u see something else.
Thanks.
PATCH v2 1/1 for-each-ref: avoid segfault on --no-format
In reply to aeron-ghThe "--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.
---
Notes:
Changes since v1:
- Use "Abhishek Agarwal" in the author and Signed-off-by lines instead of "aeron-gh".
- No code change.
builtin/for-each-ref.c | 4 ++-- t/for-each-ref-tests.sh | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-)
builtin/for-each-ref.c +2 -2
@@ -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);
t/for-each-ref-tests.sh +6 -0
@@ -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 &&
Signature · 1 line
2.56.0
Quoted 13 lines · Abhishek Agarwal
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 inverify_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(Makebuiltin-for-each-ref.cuse parse-opts., 2007-10-13).
Quoted 5 lines
"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.
Quoted 3 lines
Marking the option with
PARSE_OPT_NONEGwould 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.
Quoted 11 lines
builtin/for-each-ref.c +0
-2@@ -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 );
builtin/for-each-ref.c int for_each_ref_core(int argc, const char **argv, const char *prefix, struct re · line 65
Quoted 10 lines
builtin/for-each-ref.c +2
-0/* 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.
t/for-each-ref-tests.sh test_expect_success 'Check invalid atoms names are errors' ' · line 292
Quoted 13 lines
t/for-each-ref-tests.sh +5
-0@@ -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