Skip to content

Commit c8aaba2

Browse files
committed
libFuzzer: Fix missing trailer crash
This change fixes an invalid memory access when the trailer is missing / corrupt. Found using libFuzzer.
1 parent 1bf173c commit c8aaba2

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/indexer.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,10 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
951951
giterr_set(GITERR_INDEXER, "unexpected data at the end of the pack");
952952
return -1;
953953
}
954+
if (idx->off + 20 > idx->pack->mwf.size) {
955+
giterr_set(GITERR_INDEXER, "missing trailer at the end of the pack");
956+
return -1;
957+
}
954958

955959
packfile_trailer = git_mwindow_open(&idx->pack->mwf, &w, idx->pack->mwf.size - GIT_OID_RAWSZ, GIT_OID_RAWSZ, &left);
956960
if (packfile_trailer == NULL) {

tests/pack/indexer.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ void test_pack_indexer__leaky(void)
8787
git_indexer_free(idx);
8888
}
8989

90+
void test_pack_indexer__missing_trailer(void)
91+
{
92+
git_indexer *idx = 0;
93+
git_transfer_progress stats = { 0 };
94+
95+
cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL));
96+
/* Truncate a valid packfile */
97+
cl_git_pass(git_indexer_append(
98+
idx, out_of_order_pack, out_of_order_pack_len - 20, &stats));
99+
cl_git_fail(git_indexer_commit(idx, &stats));
100+
101+
cl_assert(giterr_last() != NULL);
102+
cl_assert_equal_i(giterr_last()->klass, GITERR_INDEXER);
103+
104+
git_indexer_free(idx);
105+
}
106+
90107
void test_pack_indexer__fix_thin(void)
91108
{
92109
git_indexer *idx = NULL;

0 commit comments

Comments
 (0)