{"thread":{"id":"56144","subject":"Bug: Commit a merge resolution with non-closed conflict markers leads to errs, segfault","startedAt":"2021-07-22T09:31:06Z","lastAt":"2021-07-22T14:17:48Z","messageCount":4,"participants":["BENTZ Sylvain","Felipe Contreras","Ævar Arnfjörð Bjarmason"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"430901","messageId":"bdb6c03f290c49bb96e6c0a3d9742a52@md6.fr","threadId":"56144","inReplyTo":null,"subject":"Bug: Commit a merge resolution with non-closed conflict markers leads to errs, segfault","fromName":"BENTZ Sylvain","fromEmail":"sylvain.bentz@md6.fr","sentAt":"2021-07-22T09:28:13Z","receivedAt":"2021-07-22T09:31:06Z","isPatch":false,"sender":{"key":"sylvain.bentz@md6.fr","avatar":null},"body":"Hello,\n\nI've stumbled on a segmentation fault while resolving merge conflicts.\nBasically, I missed to remove some conflict markers, tried to commit the\n(faulty) merge resolution, and Git mishandles that.\n\nThe original issue happened applying a patch using 3-way merge.  The minimal\nworking example below creates a conflict with a simple `git merge` command.\n\n\nThank you for filling out a Git bug report!\nPlease answer the following questions to help us understand your issue.\n\nWhat did you do before the bug happened? (Steps to reproduce your issue)\n\n1. Create a merge conflict\n2. Edit the conflicted file, keep some conflict markers (more on that below [1])\n3. Commit (works but errors are reported!)\n4. Fix the file, by removing all conflict markers\n5. Commit (works but segmentation fault!)\n6. Commit again, with some changes or with an empty commit (fatal error!)\n\nSee shell script (MWE) below [2].\n\n\nWhat did you expect to happen? (Expected behavior)\n\nAt step 3, Git should return an error without committing.\n\n\nWhat happened instead? (Actual behavior)\n\nGit works and breaks at the same time.\nStep 3 : It throws error messages without returning an error, and it does commit.\nStep 5 : It breaks (segfault), manages to commit, but fails to remove a lockfile.\n\nSee terminal output below [3].\n\nThen, if I reset hard to the commit named \"master\", delete the lockfile, and\nrepeat the process, I still get errors in step 3, but no segfault in step 5.\n\n\nWhat's different between what you expected and what actually happened?\n\nGit shouldn't segfault obviously. See gdb output below [4].\n\n\nAnything else you want to add:\n\nTested versions: 2.20 (debian stable), 2.32 (alpine 3.14), next (alpine 3.14)\n\n[1] Some configurations of conflict markers triggers that bug, others don't.\n    The minimal working example shows the case (A).\n\nFailure cases:\n(A)        (B)        (C)\n<<<<<<<<<  <<<<<<<<<  <<<<<<<<<\n           >>>>>>>>>  =========\n\nNot failure cases:\n(D)        (E)        (F)\n=========  =========  >>>>>>>>>\n>>>>>>>>>                      \n\n[2] Shell script (MWE)\n\n# Setup conflicting branches `master' and `conflict'\n\nFOLDER=${1:-git-break}\nmkdir \"$FOLDER\"\ncd \"$FOLDER\"\ngit init\necho '' > README\ngit add README\ngit commit -m \"First commit\"\necho master > README\ngit commit -i README -m \"master\"\ngit checkout -b conflict HEAD~\necho conflict > README\ngit commit -i README -m \"conflict\"\ngit checkout master\n\n# Convenience functions\n\nmer() { set -x; git merge conflict; }\ncom() { set -x; git add README; git commit -m \"$1\"; }\ndel() { set -x; for arg; do sed -i \"/$arg/ d\" README; done; cat README; }\nsee() { set -x; git log -p -n2; }\n\n# Script\n\nmer                  # Create a merge conflict\ndel = '>'            # Keep the conflict marker `<<<<<<<<<<<'\ncom \"Merge commit\"   # Commit (errors reported)\ndel '<'              # Fix by deleting the remaining marker\ncom \"Fix the merge\"  # Commit (segfault)\necho next >> README  # Do some changes...\ncom \"next\"           # Commit (fatal error)\n\n[3] Terminal output\n\n####### START TERMINAL OUTPUT ########\n\n/git-min # see\n+ see\n+ set -x\n+ git log -p -n2\ncommit 9a9dd03509b8cf45c8037c60c256d6ef67f9e431 (HEAD -> master)\nAuthor: sylvain.bentz <sylvain.bentz@md6.fr>\nDate:   Wed Jul 21 14:50:37 2021 +0200\n\n    master\n\ndiff --git a/README b/README\nindex 8b13789..1f7391f 100644\n--- a/README\n+++ b/README\n@@ -1 +1 @@\n-\n+master\n\ncommit cfb5d5cd9f28e1dcca7a1d1088405d8ec4ebf713\nAuthor: sylvain.bentz <sylvain.bentz@md6.fr>\nDate:   Wed Jul 21 14:49:36 2021 +0200\n\n    First commit\n\ndiff --git a/README b/README\nnew file mode 100644\nindex 0000000..8b13789\n--- /dev/null\n+++ b/README\n@@ -0,0 +1 @@\n+\n\n/git-min # mer\n+ mer\n+ set -x\n+ git merge conflict\nAuto-merging README\nCONFLICT (content): Merge conflict in README\nRecorded preimage for 'README'\nAutomatic merge failed; fix conflicts and then commit the result.\n\n/git-min # del = '>'\n+ del '=' '>'\n+ set -x\n+ sed -i '/=/ d' README\n+ sed -i '/>/ d' README\n+ cat README\n<<<<<<< HEAD\nmaster\nconflict\n\n/git-min # com merge\n+ com merge\n+ set -x\n+ git add README\n+ git commit -m merge\nerror: could not parse conflict hunks in 'README'\nerror: could not parse conflict hunks in 'README'\nRecorded preimage for 'README'\n[master ab6c525] merge\n\n/git-min # del '<'\n+ del '<'\n+ set -x\n+ sed -i '/</ d' README\n+ cat README\nmaster\nconflict\n\n/git-min # com mergefix\n+ com mergefix\n+ set -x\n+ git add README\n+ git commit -m mergefix\nSegmentation fault\n\n/git-min # see\n+ see\n+ set -x\n+ git log -p -n2\ncommit 8ea1a55602ba5ae505b43f2ae3a49b3a9a26b2fe (HEAD -> master)\nAuthor: Your Name <you@example.com>\nDate:   Wed Jul 21 15:38:35 2021 +0000\n\n    mergefix\n\ndiff --git a/README b/README\nindex 59c1bc9..6ecc231 100644\n--- a/README\n+++ b/README\n@@ -1,3 +1,2 @@\n-<<<<<<< HEAD\n master\n conflict\n\ncommit ab6c52591506ac2e75745814527f062d767b4e11\nMerge: 9a9dd03 42a166a\nAuthor: Your Name <you@example.com>\nDate:   Wed Jul 21 15:38:18 2021 +0000\n\n    merge\n\n/git-min # echo 'next' >> README\n+ echo next\n\n/git-min # com next\n+ com next\n+ set -x\n+ git add README\n+ git commit -m next\nfatal: Unable to create '/git-min/.git/MERGE_RR.lock': File exists.\n\nAnother git process seems to be running in this repository, e.g.\nan editor opened by 'git commit'. Please make sure all processes\nare terminated then try again. If it still fails, a git process\nmay have crashed in this repository earlier:\nremove the file manually to continue.\n\n/git-min # git --version\n+ git --version\ngit version 2.32.0.473.g1bb01aad6f\n\n/git-min # git config --list\n+ git config --list\nuser.email=you@example.com\nuser.name=Your Name\ncore.repositoryformatversion=0\ncore.filemode=true\ncore.bare=false\ncore.logallrefupdates=true\ncore.pager=cat\ncolor.ui=never\n\n####### END TERMINAL OUTPUT ########\n\n[4] GDB Output\n\n(gdb) run\nStarting program: /usr/local/bin/git commit -m Fix\\ the\\ merge\nwarning: Error disabling address space randomization: Operation not permitted\n\nProgram received signal SIGSEGV, Segmentation fault.\n0x000055dbdff61748 in do_rerere_one_path (update=0x7ffffce68720, rr_item=0x7f5ed8e20340,\n    istate=0x55dbe00ef520 <the_index>) at rerere.c:736\n736\n\n\nPlease review the rest of the bug report below.\nYou can delete any lines you don't wish to share.\n\n[System Info]\ngit version:\ngit version 2.32.0.473.g1bb01aad6f\ncpu: x86_64\nbuilt from commit: 1bb01aad6fdafaac7a11bed18a67384f28ab735b\nsizeof-long: 8\nsizeof-size_t: 8\nshell-path: /bin/sh\nuname: Linux 5.4.72-microsoft-standard-WSL2 #1 SMP Wed Oct 28 23:40:43 UTC 2020 x86_64\ncompiler info: gnuc: 10.3\nlibc info: no libc information available\n$SHELL (typically, interactive shell): <unset>\n\n[Enabled Hooks]\n"},{"id":"430904","messageId":"60f9477d9cd3a_3fba2082@natae.notmuch","threadId":"56144","inReplyTo":"bdb6c03f290c49bb96e6c0a3d9742a52@md6.fr","subject":"RE: Bug: Commit a merge resolution with non-closed conflict markers leads to errs, segfault","fromName":"Felipe Contreras","fromEmail":"felipe.contreras@gmail.com","sentAt":"2021-07-22T10:25:01Z","receivedAt":"2021-07-22T10:26:52Z","isPatch":false,"sender":{"key":"felipe.contreras@gmail.com","avatar":"https://avatars.githubusercontent.com/u/8358?v=4"},"body":"BENTZ Sylvain wrote:\n> I've stumbled on a segmentation fault while resolving merge conflicts.\n> Basically, I missed to remove some conflict markers, tried to commit the\n> (faulty) merge resolution, and Git mishandles that.\n> \n> The original issue happened applying a patch using 3-way merge.  The minimal\n> working example below creates a conflict with a simple `git merge` command.\n\nA quick workaround:\n\n  git config --global rerere.enabled false\n\nBut yeah, there seems to be a bug in `git rerere`.\n\n-- \nFelipe Contreras\n"},{"id":"430918","messageId":"66af2662ae884fff81f51a4eb5ac1dd1@md6.fr","threadId":"56144","inReplyTo":"60f9477d9cd3a_3fba2082@natae.notmuch","subject":"RE: Bug: Commit a merge resolution with non-closed conflict markers leads to errs, segfault","fromName":"BENTZ Sylvain","fromEmail":"sylvain.bentz@md6.fr","sentAt":"2021-07-22T12:53:47Z","receivedAt":"2021-07-22T12:53:56Z","isPatch":false,"sender":{"key":"sylvain.bentz@md6.fr","avatar":null},"body":"Oh I wasn't aware of this when I initialized my MWE repo, from git-config[1]:\n\n  By default, git-rerere[1] is enabled if there is an rr-cache directory under\n  the $GIT_DIR, e.g. if \"rerere\" was previously used in the repository.\n\nThe correct MWE requires to enable rerere:\n\n  git config rerere.enabled true\n\n--\nSylvain Bentz\n"},{"id":"430936","messageId":"87pmvatoti.fsf@evledraar.gmail.com","threadId":"56144","inReplyTo":"66af2662ae884fff81f51a4eb5ac1dd1@md6.fr","subject":"Re: Bug: Commit a merge resolution with non-closed conflict markers leads to errs, segfault","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-07-22T14:17:28Z","receivedAt":"2021-07-22T14:17:48Z","isPatch":false,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Thu, Jul 22 2021, BENTZ Sylvain wrote:\n\n> Oh I wasn't aware of this when I initialized my MWE repo, from git-config[1]:\n>\n>   By default, git-rerere[1] is enabled if there is an rr-cache directory under\n>   the $GIT_DIR, e.g. if \"rerere\" was previously used in the repository.\n>\n> The correct MWE requires to enable rerere:\n>\n>   git config rerere.enabled true\n\nI think the patch I posted at the end of\nhttps://lore.kernel.org/git/87v96p4w3f.fsf@evledraar.gmail.com/ should\nmight catch this, i.e. the \"could not copy\" error.\n"}]}