@matheusbsbs / matheus-bagliano / commits / b3100fd555
Fork 0

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 <gitster@pobox.com>

Junio C Hamano committed Dec 16, 2009 at 10:50 UTC b3100fd5557dbaad4eeb5690336758ef21bb08bb
2 files changed +20 -2
setup.c
+5 -2
@@ -18,9 +18,12 @@ const char *prefix_path(const char *prefix, int len, const char *path)
18 if (normalize_path_copy(sanitized, sanitized))
19 goto error_out;
20 if (is_absolute_path(orig)) {
21 + size_t len, total;
22 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;
27 if (strncmp(sanitized, work_tree, len) ||
28 (sanitized[len] != '\0' && sanitized[len] != '/')) {
29 error_out:
t/t1501-worktree.sh
+15
@@ -174,4 +174,19 @@ test_expect_success 'git grep' '
174 GIT_DIR=../.. GIT_WORK_TREE=.. git grep -l changed | grep dir/tracked)
175 '
176
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 +
192 test_done