Commit 62d59467 by lhchavez

Fix segfault when calling git_blame_buffer()

This change makes sure that the hunk is not null before trying to
dereference it. This avoids segfaults, especially when blaming against a
modified buffer (i.e. the index).

Fixes: #5443
parent dd435711
...@@ -415,7 +415,7 @@ on_error: ...@@ -415,7 +415,7 @@ on_error:
static bool hunk_is_bufferblame(git_blame_hunk *hunk) static bool hunk_is_bufferblame(git_blame_hunk *hunk)
{ {
return git_oid_is_zero(&hunk->final_commit_id); return hunk && git_oid_is_zero(&hunk->final_commit_id);
} }
static int buffer_hunk_cb( static int buffer_hunk_cb(
......
...@@ -17,6 +17,32 @@ void test_blame_buffer__cleanup(void) ...@@ -17,6 +17,32 @@ void test_blame_buffer__cleanup(void)
git_repository_free(g_repo); git_repository_free(g_repo);
} }
void test_blame_buffer__index(void)
{
const git_blame_hunk *hunk;
const char *buffer = "Hello\nWorld!";
/*
* We need to open a different file from the ones used in other tests. Close
* the one opened in test_blame_buffer__initialize() to avoid a leak.
*/
git_blame_free(g_fileblame);
g_fileblame = NULL;
cl_git_pass(git_blame_file(&g_fileblame, g_repo, "file.txt", NULL));
cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer)));
cl_assert_equal_i(2, git_blame_get_hunk_count(g_bufferblame));
check_blame_hunk_index(g_repo, g_bufferblame, 0, 1, 1, 0, "836bc00b", "file.txt");
hunk = git_blame_get_hunk_byline(g_bufferblame, 1);
cl_assert(hunk);
cl_assert_equal_s("lhchavez", hunk->final_signature->name);
check_blame_hunk_index(g_repo, g_bufferblame, 1, 2, 1, 0, "00000000", "file.txt");
hunk = git_blame_get_hunk_byline(g_bufferblame, 2);
cl_assert(hunk);
cl_assert(hunk->final_signature == NULL);
}
void test_blame_buffer__added_line(void) void test_blame_buffer__added_line(void)
{ {
const git_blame_hunk *hunk; const git_blame_hunk *hunk;
......
6653ff42313eb5c82806f145391b18a9699800c7 836bc00b06cb60eb0f629e237ad2b58adb2cfc7e
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment