Skip to content

Commit bc6b13a

Browse files
tgummerergitster
authored andcommitted
blame: fix segfault on untracked files
Since 3b75ee9 ("blame: allow to blame paths freshly added to the index", 2016-07-16) git blame also looks at the index to determine if there is a file that was freshly added to the index. cache_name_pos returns -pos - 1 in case there is no match is found, or if the name matches, but the entry has a stage other than 0. As git blame should work for unmerged files, it uses strcmp to determine whether the name of the returned position matches, in which case the file exists, but is merely unmerged, or if the file actually doesn't exist in the index. If the repository is empty, or if the file would lexicographically be sorted as the last file in the repository, -cache_name_pos - 1 is outside of the length of the active_cache array, causing git blame to segfault. Guard against that, and die() normally to restore the old behaviour. Reported-by: Simon Ruderich <[email protected]> Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c66b470 commit bc6b13a

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

builtin/blame.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2245,7 +2245,8 @@ static void verify_working_tree_path(struct commit *work_tree, const char *path)
22452245
pos = cache_name_pos(path, strlen(path));
22462246
if (pos >= 0)
22472247
; /* path is in the index */
2248-
else if (!strcmp(active_cache[-1 - pos]->name, path))
2248+
else if (-1 - pos < active_nr &&
2249+
!strcmp(active_cache[-1 - pos]->name, path))
22492250
; /* path is in the index, unmerged */
22502251
else
22512252
die("no such path '%s' in HEAD", path);

t/t8002-blame.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ test_description='git blame'
66
PROG='git blame -c'
77
. "$TEST_DIRECTORY"/annotate-tests.sh
88

9+
test_expect_success 'blame untracked file in empty repo' '
10+
>untracked &&
11+
test_must_fail git blame untracked
12+
'
13+
914
PROG='git blame -c -e'
1015
test_expect_success 'blame --show-email' '
1116
check_count \

0 commit comments

Comments
 (0)