Skip to content

Commit b3100fd

Browse files
committed
worktree: don't segfault with an absolute pathspec without a work tree
If a command is run with an absolute path as a pathspec inside a bare repository, e.g. "rev-list HEAD -- /home", the code tried to run strlen() on NULL, which is the result of get_git_work_tree(), and segfaulted. It should just fail instead. Currently the function returns NULL even inside .git/ in a repository with a work tree, but that is a separate issue. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8b8e862 commit b3100fd

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

setup.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ const char *prefix_path(const char *prefix, int len, const char *path)
1818
if (normalize_path_copy(sanitized, sanitized))
1919
goto error_out;
2020
if (is_absolute_path(orig)) {
21+
size_t len, total;
2122
const char *work_tree = get_git_work_tree();
22-
size_t len = strlen(work_tree);
23-
size_t total = strlen(sanitized) + 1;
23+
if (!work_tree)
24+
goto error_out;
25+
len = strlen(work_tree);
26+
total = strlen(sanitized) + 1;
2427
if (strncmp(sanitized, work_tree, len) ||
2528
(sanitized[len] != '\0' && sanitized[len] != '/')) {
2629
error_out:

t/t1501-worktree.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,19 @@ test_expect_success 'git grep' '
174174
GIT_DIR=../.. GIT_WORK_TREE=.. git grep -l changed | grep dir/tracked)
175175
'
176176

177+
test_expect_success 'git commit' '
178+
(
179+
cd repo.git &&
180+
GIT_DIR=. GIT_WORK_TREE=work git commit -a -m done
181+
)
182+
'
183+
184+
test_expect_success 'absolute pathspec should fail gracefully' '
185+
(
186+
cd repo.git || exit 1
187+
git config --unset core.worktree
188+
test_must_fail git log HEAD -- /home
189+
)
190+
'
191+
177192
test_done

0 commit comments

Comments
 (0)