Skip to content

Commit 6dd0d7d

Browse files
daviesrobwhitwham
authored andcommitted
Add overflow check in bgzf_index_load_hfile()
This function reads an item count from the input file and uses it to allocate an array of file offsets. It's possible for this to overflow, resulting in an attempt to malloc(0) and, if that returns a valid pointer, an attempt to zero 16 bytes in a zero-length allocation. Fix by checking that the allocation will not overflow. Thanks to Harrison Green for reporting this.
1 parent d4c747b commit 6dd0d7d

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

bgzf.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2471,6 +2471,9 @@ int bgzf_index_load_hfile(BGZF *fp, struct hFILE *idx, const char *name)
24712471
if (fp->idx == NULL) goto fail;
24722472
uint64_t x;
24732473
if (hread_uint64(&x, idx) < 0) goto fail;
2474+
if (x >= ((SIZE_MAX < UINT64_MAX ? SIZE_MAX : UINT64_MAX)
2475+
/ sizeof(bgzidx1_t) / 2))
2476+
goto fail;
24742477

24752478
fp->idx->noffs = fp->idx->moffs = x + 1;
24762479
fp->idx->offs = (bgzidx1_t*) malloc(fp->idx->moffs*sizeof(bgzidx1_t));

0 commit comments

Comments
 (0)