{"thread":{"id":"60326","subject":"Bug: 'git diff' crashes on corrupted repository","startedAt":"2023-10-08T20:35:27Z","lastAt":"2023-10-08T20:35:27Z","messageCount":1,"participants":["Dominik Paulus"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"482831","messageId":"wawqkhikp5ga4b6talc4zug7mgzwdf36yyc5jqontk6v3zcryy@ds5iqwuj2omw","threadId":"60326","inReplyTo":null,"subject":"Bug: 'git diff' crashes on corrupted repository","fromName":"Dominik Paulus","fromEmail":"git-bugreport@dominik-paulus.ch","sentAt":"2023-10-08T20:27:30Z","receivedAt":"2023-10-08T20:35:27Z","isPatch":false,"sender":{"key":"git-bugreport@dominik-paulus.ch","avatar":null},"body":"What did you do before the bug happened?\nI have a git repository which is stored in a 'Seafile' shared folder[0]. As I\nnoticed, 'Seafile' seems to not correctly sync hidden files/dotfiles, so after\nmodifying the git repository on one host, the \".git\" directory in the Seafile\nreplica on the other host is inconsistent: Specifically, it seems to be\nmissing a subset of the files in '.git/objects'.\n'git fsck' complains about various missing blobs, dangling commits, dangling\ntrees, broken links, and notably also \"error: HEAD: invalid sha1 pointer\n[...]\".\n\nOn this - obviously corrupted - repository, most git commands report \"fatal:\nbad object HEAD\". In contrast, 'git diff' crashes:\n\n$ git status\nfatal: bad object HEAD\n$ git log\nfatal: bad object HEAD\n$ git diff\nSegmentation fault (core dumped)\n$\n\nThis is the stacktrace for this crash (gdb output):\n\n-----\nStarting program: /usr/bin/git diff\nDownloading separate debug info for system-supplied DSO at 0x7ffff7fc8000\n[Thread debugging using libthread_db enabled]\nUsing host libthread_db library \"/usr/lib/libthread_db.so.1\".\n[Detaching after fork from child process 16789]\n\nProgram received signal SIGSEGV, Segmentation fault.\n0x00005555556ff6ac in diff_add_if_missing (r=0x555555966ac0 <the_repo>, to_fetch=0x7fffffffce10, filespec=0x555555975)\n    at /usr/src/debug/git/git-2.42.0/diff.c:6895\n6895\t\tif (filespec && filespec->oid_valid &&\n(gdb) bt\n#0  0x00005555556ff6ac in diff_add_if_missing (r=0x555555966ac0 <the_repo>, to_fetch=0x7fffffffce10, filespec=0x555555975)\n    at /usr/src/debug/git/git-2.42.0/diff.c:6895\n#1  0x00005555556ff7aa in diff_queued_diff_prefetch (repository=0x555555966ac0 <the_repo>)\n    at /usr/src/debug/git/git-2.42.0/diff.c:6911\n#2  0x00005555556f5e74 in diff_populate_filespec (r=0x555555966ac0 <the_repo>, s=0x555555977660, options=0x7fffffffcfe0)\n    at /usr/src/debug/git/git-2.42.0/diff.c:4184\n#3  0x00005555556fee70 in diff_filespec_check_stat_unmatch (r=0x555555966ac0 <the_repo>, p=p@entry=0x5555559755e0)\n    at /usr/src/debug/git/git-2.42.0/diff.c:6839\n#4  0x0000555555709560 in diffcore_skip_stat_unmatch (diffopt=0x7fffffffdb20) at /usr/src/debug/git/git-2.42.0/diff.c:6857\n#5  diffcore_std (options=<optimized out>) at /usr/src/debug/git/git-2.42.0/diff.c:6946\n#6  0x00005555556ee59c in run_diff_files (revs=<optimized out>, option=<optimized out>)\n    at /usr/src/debug/git/git-2.42.0/diff-lib.c:272\n#7  0x00005555555ba95b in builtin_diff_files (argv=<optimized out>, argc=<optimized out>, revs=<optimized out>)\n    at builtin/diff.c:293\n#8  cmd_diff (argc=1, argv=0x7fffffffe7a0, prefix=<optimized out>) at builtin/diff.c:586\n#9  0x00005555555767f4 in run_builtin (argv=0x7fffffffe7a0, argc=1, p=0x555555938db0 <commands.lto_priv+816>)\n    at /usr/src/debug/git/git-2.42.0/git.c:469\n#10 handle_builtin (argc=1, argv=0x7fffffffe7a0) at /usr/src/debug/git/git-2.42.0/git.c:723\n#11 0x0000555555576dab in run_argv (argcp=0x7fffffffe4ec, argv=0x7fffffffe510) at /usr/src/debug/git/git-2.42.0/git.c:787\n#12 0x00005555555727e3 in cmd_main (argv=<optimized out>, argc=<optimized out>) at /usr/src/debug/git/git-2.42.0/git.c:922\n#13 main (argc=<optimized out>, argv=<optimized out>) at /usr/src/debug/git/git-2.42.0/common-main.c:62\n(gdb) print filespec->oid_valid\nCannot access memory at address 0x555555975\n(gdb)\n-----\n\n\nWhat did you expect to happen?\n'git diff' should not crash/perform invalid memory accesses on a corrupted repository.\n\n\n[System Info]\ngit version:\ngit version 2.42.0\ncpu: x86_64\nno commit associated with this build\nsizeof-long: 8\nsizeof-size_t: 8\nshell-path: /bin/sh\nuname: Linux 6.5.5-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 23 Sep 2023 22:55:13 +0000 x86_64\ncompiler info: gnuc: 13.2\nlibc info: glibc: 2.38\n$SHELL (typically, interactive shell): /bin/bash\n\n\n[Enabled Hooks]\n\n\n\n[0] I'm aware that this is a bad idea in the first place and a red flag about\nthe underlying workflow :).\n"}]}