Bug: git diagnose crashes with Segmentation fault outside of git repository
Thank you for filling out a Git bug report!
Please answer the following questions to help us understand your issue.
What did you do before the bug happened? (Steps to reproduce your issue) Run git diagnose outside of any git repository
What did you expect to happen? (Expected behavior) No crash, reasonable diagnose output
What happened instead? (Actual behavior)git diagnose crashed with Segmentation fault
$ git diagnose Collecting diagnostic info
git version 2.41.0
cpu: x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
Repository root: (null)
Available space on '/tmp': 7.75 GiB (mount flags 0x6) Segmentation fault (core dumped)
What's different between what you expected and what actually happened? Segmentation fault, truncated output
Anything else you want to add:git diagnose does not crash when run within some git repository
Please review the rest of the bug report below.
You can delete any lines you don't wish to share.
[System Info]
git version:
git version 2.41.0
cpu: x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
uname: Linux 6.5.6-200.fc38.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Oct 6 19:02:35 UTC 2023 x86_64
compiler info: gnuc: 13.1
libc info: glibc: 2.37
$SHELL (typically, interactive shell): /bin/bash
[Enabled Hooks]
not run from a git repository - no hooks to show
PATCH 1/1 diagnose: require repository
When git diagnose is run from outside a repo, it begins collecting various information before eventually hitting a segmentation fault, leaving an incomplete zip file behind.
Switch from the gentle setup to requiring a git directory. Without a git repo, there isn't really much to diagnose.
We could possibly do a best-effort collection of information about the machine and then give up. That would roughly be today's behavior but with a controlled exit rather than a segfault. However, the purpose of this tool is largely to create a zip archive. Rather than creating an empty zip file or no zip file at all, and having to explain that behavior, it seems more helpful to bail out clearly and early with a succinct error message.
---
Thanks for the report. This could be one way of fixing this.
I haven't found anything in the original submission [1] discussing this "_GENTLY". I didn't see anything in the implementation or the tests suggesting that it was intentional to run outside a git repo.
[1] https://lore.kernel.org/git/[email protected]/t/#mc66904caab6bc79e57eaf5063df268b2725b6fcc
t/t0092-diagnose.sh | 5 +++++ git.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-)
t/t0092-diagnose.sh +5 -0
@@ -5,6 +5,11 @@ test_description='git diagnose' TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh +test_expect_success 'nothing to diagnose without repo' ' + nongit test_must_fail git diagnose 2>err && + grep "not a git repository" err +' + test_expect_success UNZIP 'creates diagnostics zip archive' ' test_when_finished rm -rf report &&
git.c +1 -1
@@ -525,7 +525,7 @@ static struct cmd_struct commands[] = { { "credential-cache--daemon", cmd_credential_cache_daemon }, { "credential-store", cmd_credential_store }, { "describe", cmd_describe, RUN_SETUP }, - { "diagnose", cmd_diagnose, RUN_SETUP_GENTLY }, + { "diagnose", cmd_diagnose, RUN_SETUP }, { "diff", cmd_diff, NO_PARSEOPT }, { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, { "diff-index", cmd_diff_index, RUN_SETUP | NO_PARSEOPT },
Signature · 1 line
2.42.0.399.g85a82e71e0
Re: [PATCH] diagnose: require repository
Quoted 2 lines · Martin Ågren
Switch from the gentle setup to requiring a git directory. Without a git repo, there isn't really much to diagnose.
This patch works for me. Thanks.
Signature · 1 line
Kristoffer Haugsbakk
Re: [PATCH] diagnose: require repository
In reply to Martin ÅgrenQuoted 7 lines · Martin Ågren
When
git diagnoseis run from outside a repo, it begins collecting various information before eventually hitting a segmentation fault, leaving an incomplete zip file behind.Switch from the gentle setup to requiring a git directory. Without a git repo, there isn't really much to diagnose.
Quoted 7 lines
We could possibly do a best-effort collection of information about the machine and then give up. That would roughly be today's behavior but with a controlled exit rather than a segfault. However, the purpose of this tool is largely to create a zip archive. Rather than creating an empty zip file or no zip file at all, and having to explain that behavior, it seems more helpful to bail out clearly and early with a succinct error message.
Without having thought things through, offhand I agree with your "no repository? there is nothing worth tarring up then" assessment.
Because "git bugreport --diag" unconditionally spawns "git diagnose", the former may also want to be extra careful, perhaps like the attached patch.
builtin/bugreport.c | 5 +++++ 1 file changed, 5 insertions(+)
builtin/bugreport.c +5 -0
@@ -146,6 +146,11 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix) report_path.buf); } +if (!startup_info->have_repository && diagnose !=DIAGNOSE_NONE ) { +warning (_ ("no repository--diagnostic output disabled" )); + diagnose =DIAGNOSE_NONE ; + } +/* Prepare diagnostics, if requested */ if (diagnose !=DIAGNOSE_NONE ) {struct strbuf zip_path =STRBUF_INIT ;
Re: [PATCH] diagnose: require repository
Quoted 11 lines · Junio C Hamano
Quoted 8 lines · Martin Ågren
Switch from the gentle setup to requiring a git directory. Without a git repo, there isn't really much to diagnose.
We could possibly do a best-effort collection of information about the machine and then give up. That would roughly be today's behavior but with a controlled exit rather than a segfault. However, the purpose of this tool is largely to create a zip archive. Rather than creating an empty zip file or no zip file at all, and having to explain that
Correcting myself: The zip archive would actually contain diagnostics.log with some general info about the machine and Git build.
Quoted 9 lines
Quoted 2 lines
behavior, it seems more helpful to bail out clearly and early with a succinct error message.
Without having thought things through, offhand I agree with your "no repository? there is nothing worth tarring up then" assessment.
Because "git bugreport --diag" unconditionally spawns "git diagnose", the former may also want to be extra careful, perhaps like the attached patch.
Good point. TBH, I had no idea about git bugreport --diagnose.
builtin/bugreport.c int cmd_bugreport(int argc, const char **argv, const char *prefix) · line 153
Quoted 5 lines
builtin/bugreport.c +5
-0+if (!startup_info->have_repository && diagnose !=DIAGNOSE_NONE ) { +warning (_ ("no repository--diagnostic output disabled" )); + diagnose =DIAGNOSE_NONE ; + } +
When the user explicitly provides that option, it seems unfortunate to me to drop it. Yes, we'd warn, but git bugreport then pops a text editor, so you would only see the warning after finishing up the report. (Maybe. By the time you quit your editor, you might not consider checking the terminal for warnings and such.)
So I'm inclined to instead just die if we see the option outside a repo. If diagnose the command fundamentally requires a repo (as with my patch) it seems surprising to me to not have --diagnose the option behave the same.
Martin
Re: [PATCH] diagnose: require repository
Quoted 3 lines · Martin Ågren
Correcting myself: The zip archive would actually contain
diagnostics.logwith some general info about the machine and Git build.
So it could contain some useful information without a specific repository, perhaps.
Quoted 1 line
Good point. TBH, I had no idea about
git bugreport --diagnose.
You are not alone ;-) I didn't, either. Before responding to your patch, that is.
Quoted 12 lines
builtin/bugreport.c int cmd_bugreport(int argc, const char **argv, const char *prefix) · line 153
Quoted 5 lines
builtin/bugreport.c +5
-0+if (!startup_info->have_repository && diagnose !=DIAGNOSE_NONE ) { +warning (_ ("no repository--diagnostic output disabled" )); + diagnose =DIAGNOSE_NONE ; + } +When the user explicitly provides that option, it seems unfortunate to me to drop it. Yes, we'd warn, but
git bugreportthen pops a text editor, so you would only see the warning after finishing up the report. (Maybe. By the time you quit your editor, you might not consider checking the terminal for warnings and such.)
Quoted 4 lines
So I'm inclined to instead just die if we see the option outside a repo. If
diagnosethe command fundamentally requires a repo (as with my patch) it seems surprising to me to not have--diagnosethe option behave the same.
I have no strong opinion. Victoria is on Cc: already, whose name appears a lot more often than mine in the shortlog for "diagnose" stuff, so I'll defer to her area expertise.
Thanks.
Re: [PATCH] diagnose: require repository
In reply to Martin ÅgrenQuoted 24 lines · Martin Ågren
Quoted 9 lines
Quoted 2 lines
behavior, it seems more helpful to bail out clearly and early with a succinct error message.
Without having thought things through, offhand I agree with your "no repository? there is nothing worth tarring up then" assessment.
Because "git bugreport --diag" unconditionally spawns "git diagnose", the former may also want to be extra careful, perhaps like the attached patch.
Good point. TBH, I had no idea about
git bugreport --diagnose.builtin/bugreport.c int cmd_bugreport(int argc, const char **argv, const char *prefix) · line 153
Quoted 5 lines
builtin/bugreport.c +5
-0+if (!startup_info->have_repository && diagnose !=DIAGNOSE_NONE ) { +warning (_ ("no repository--diagnostic output disabled" )); + diagnose =DIAGNOSE_NONE ; + } +When the user explicitly provides that option, it seems unfortunate to me to drop it. Yes, we'd warn, but
git bugreportthen pops a text editor, so you would only see the warning after finishing up the report. (Maybe. By the time you quit your editor, you might not consider checking the terminal for warnings and such.)
Quoted 4 lines
So I'm inclined to instead just die if we see the option outside a repo. If
diagnosethe command fundamentally requires a repo (as with my patch) it seems surprising to me to not have--diagnosethe option behave the same.
I agree - it was an oversight on my part to not firmly require the existence of a repository with 'git diagnose', and the same applies to 'bugreport --diagnose'.
For reference, there is one other usage of 'git diagnose' (in 'scalar diagnose'). However, it's already guarded by 'setup_git_directory()' so it shouldn't need to be updated.
Quoted 2 lines
Martin
Quoted 16 lines · On Sat, Oct 14, 2023 at 2:00 PM ks1322 ks1322 <[email protected]> wrote:
Thank you for filling out a Git bug report!
Please answer the following questions to help us understand your issue.What did you do before the bug happened? (Steps to reproduce your issue) Run
git diagnoseoutside of any git repositoryWhat did you expect to happen? (Expected behavior) No crash, reasonable diagnose output
What happened instead? (Actual behavior)
git diagnosecrashed with Segmentation fault$ git diagnose Collecting diagnostic info
Quoted 9 lines
git version 2.41.0
cpu:x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path:/bin/sh
Repository root: (null)
Available space on '/tmp': 7.75 GiB (mount flags0x6) Segmentation fault (core dumped)
Yeah, this is a valid bug report that I can reproduce. It looks like such bugs have existed in this command since it was created last year.
Best,
Christian.