{"thread":{"id":"66913","subject":"[PATCH] difftool: handle invalid paths without crashing","startedAt":"2026-11-22T13:39:25Z","lastAt":"2026-11-22T13:39:25Z","messageCount":1,"participants":["Ophir Bleicher via GitGitGadget"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"559094","messageId":"pull.2503.git.git.1795354765127.gitgitgadget@gmail.com","threadId":"66913","inReplyTo":null,"subject":"[PATCH] difftool: handle invalid paths without crashing","fromName":"Ophir Bleicher via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-22T13:39:25Z","receivedAt":"2026-11-22T13:39:25Z","isPatch":true,"sender":{"key":"name:Ophir Bleicher","avatar":null},"body":"From: Ophir Bleicher <ophirble@gmail.com>\n\nCheck the cache entry returned by make_transient_cache_entry() in\nbuiltin/difftool.c. An invalid path can cause the function to\ndereference a NULL pointer, resulting in a segmentation fault:\n\n$ blob=$(printf '%s\\n' DEADBLOB | git hash-object -w --stdin)\n$ tree=$(printf \"100644 blob %s\\t.git\\n\" \"$blob\" | git mktree)\n$ commit=$(git commit-tree -m BADCOMMIT \"$tree\")\n$ git difftool --dir-diff --no-symlinks --extcmd true \"$commit\" HEAD\nerror: invalid path '.git'\nSegmentation fault (core dumped) git difftool ...\n\nAdd a regression test in t/t7800-difftool.sh for a tree containing\nan invalid .git path to ensure git difftool --dir-diff exits\ncorrectly.\n\nSigned-off-by: Ophir Bleicher <ophirble@gmail.com>\n---\n    difftool: handle invalid paths without crashing\n    \n    Check the cache entry returned by make_transient_cache_entry() in\n    builtin/difftool.c. An invalid path can cause the function to\n    dereference a NULL pointer, resulting in a segmentation fault:\n    \n    $ blob=$(printf '%s\\n' DEADBLOB | git hash-object -w --stdin) $\n    tree=$(printf \"100644 blob %s\\t.git\\n\" \"$blob\" | git mktree) $\n    commit=$(git commit-tree -m BADCOMMIT \"$tree\") $ git difftool --dir-diff\n    --no-symlinks --extcmd true \"$commit\" HEAD error: invalid path '.git'\n    Segmentation fault (core dumped) git difftool ...\n    \n    Add a regression test in t/t7800-difftool.sh for a tree containing an\n    invalid .git path to ensure git difftool --dir-diff exits correctly.\n    \n    Signed-off-by: Ophir Bleicher ophirble@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2503%2FOphirBleicher%2Fbug-difftool-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2503/OphirBleicher/bug-difftool-v1\nPull-Request: https://github.com/git/git/pull/2503\n\n builtin/difftool.c  | 2 ++\n t/t7800-difftool.sh | 8 ++++++++\n 2 files changed, 10 insertions(+)\n\ndiff --git a/builtin/difftool.c b/builtin/difftool.c\nindex bc7b2ea443..8208ade472 100644\n--- a/builtin/difftool.c\n+++ b/builtin/difftool.c\n@@ -336,6 +336,8 @@ static int checkout_path(unsigned mode, struct object_id *oid,\n \tint ret;\n \n \tce = make_transient_cache_entry(mode, oid, path, 0, NULL);\n+\tif (!ce)\n+\t\treturn -1;\n \tret = checkout_entry(ce, state, NULL, NULL);\n \n \tdiscard_cache_entry(ce);\ndiff --git a/t/t7800-difftool.sh b/t/t7800-difftool.sh\nindex 0079aa9da4..9c79b2dec0 100755\n--- a/t/t7800-difftool.sh\n+++ b/t/t7800-difftool.sh\n@@ -929,4 +929,12 @@ test_expect_success 'difftool --rotate/skip-to error condition' '\n \ttest_must_fail git difftool --no-prompt --extcmd=cat --rotate-to=\"3\" HEAD^ &&\n \ttest_must_fail git difftool --no-prompt --extcmd=cat --skip-to=\"3\" HEAD^\n '\n+\n+test_expect_success 'difftool handles invalid .git path' '\n+\tblob=$(printf '%s\\n' DEADBLOB | git hash-object -w --stdin) &&\n+\ttree=$(printf \"100644 blob %s\\t.git\\n\" \"$blob\" | git mktree) &&\n+\tcommit=$(git commit-tree -m BADCOMMIT \"$tree\") &&\n+\ttest_expect_code 1 git difftool --dir-diff --no-symlinks --extcmd true \"$commit\" HEAD\n+'\n+\n test_done\n\nbase-commit: f7de81456940dd6f3274e461149bba12965de609\n-- \ngitgitgadget\n\n"}]}