blame: fix segfault on untracked files
commitbc6b13a7d2300e982dd3a3aeef2f3ad4d39cf149
authorThomas Gummerer <t.gummerer@gmail.com>
Sat, 27 Aug 2016 20:01:50 +0000 (27 21:01 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 29 Aug 2016 18:57:33 +0000 (29 11:57 -0700)
treefbb0ea83fa6363d4709f022441caa4381d3d152d
parentc66b470082144471fc95e669eb398c8f320d67c4
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 <simon@ruderich.org>
Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/blame.c
t/t8002-blame.sh