Skip to content

Commit bba9599

Browse files
authored
Merge pull request libgit2#5445 from lhchavez/fix-5443
Fix segfault when calling git_blame_buffer()
2 parents 9d5016d + 62d5946 commit bba9599

File tree

6 files changed

+32
-2
lines changed

6 files changed

+32
-2
lines changed

src/blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ int git_blame_file(
415415

416416
static bool hunk_is_bufferblame(git_blame_hunk *hunk)
417417
{
418-
return git_oid_is_zero(&hunk->final_commit_id);
418+
return hunk && git_oid_is_zero(&hunk->final_commit_id);
419419
}
420420

421421
static int buffer_hunk_cb(

tests/blame/buffer.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,32 @@ void test_blame_buffer__cleanup(void)
1717
git_repository_free(g_repo);
1818
}
1919

20+
void test_blame_buffer__index(void)
21+
{
22+
const git_blame_hunk *hunk;
23+
const char *buffer = "Hello\nWorld!";
24+
25+
/*
26+
* We need to open a different file from the ones used in other tests. Close
27+
* the one opened in test_blame_buffer__initialize() to avoid a leak.
28+
*/
29+
git_blame_free(g_fileblame);
30+
g_fileblame = NULL;
31+
cl_git_pass(git_blame_file(&g_fileblame, g_repo, "file.txt", NULL));
32+
33+
cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer)));
34+
cl_assert_equal_i(2, git_blame_get_hunk_count(g_bufferblame));
35+
36+
check_blame_hunk_index(g_repo, g_bufferblame, 0, 1, 1, 0, "836bc00b", "file.txt");
37+
hunk = git_blame_get_hunk_byline(g_bufferblame, 1);
38+
cl_assert(hunk);
39+
cl_assert_equal_s("lhchavez", hunk->final_signature->name);
40+
check_blame_hunk_index(g_repo, g_bufferblame, 1, 2, 1, 0, "00000000", "file.txt");
41+
hunk = git_blame_get_hunk_byline(g_bufferblame, 2);
42+
cl_assert(hunk);
43+
cl_assert(hunk->final_signature == NULL);
44+
}
45+
2046
void test_blame_buffer__added_line(void)
2147
{
2248
const git_blame_hunk *hunk;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
x��K
2+
1D]���B�$�OD�{�{:��� O���գ�A�y�u���"� RA�ˆ�4�1����f/���]Uϲȣ���X
3+
�M��Ȗ,�sFc8���S[�}⩼壏�78p�O��.%��f�Z�ua�\u������JF�
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x+)JMU04�`040031QH�+�(a�ٵ�J�5�Ѕ'3����P%�0�� ���}_ՉΓ�E�{�4�٩�r� -3'�槚�^�uU�'�ݘ��a�n��P5��5��?����p����9���_$*��\9�
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6653ff42313eb5c82806f145391b18a9699800c7
1+
836bc00b06cb60eb0f629e237ad2b58adb2cfc7e

0 commit comments

Comments
 (0)