deeptools / pyBigWig

A python extension for quick access to bigWig and bigBed files
MIT License
218 stars 49 forks source link

Reading bigWig files sometimes fails: Uninitialized bwRTree_t->root variable #85

Closed pbenner closed 5 years ago

pbenner commented 5 years ago

https://github.com/deeptools/pyBigWig/blob/339ebc8d1765006cf736a6b5dd644b75aec63b14/libBigWig/bwValues.c#L36

A new root node is allocated here using malloc (not calloc), which leaves node->root uninitialized.

https://github.com/deeptools/pyBigWig/blob/339ebc8d1765006cf736a6b5dd644b75aec63b14/libBigWig/bwValues.c#L302

Here the index is read.

https://github.com/deeptools/pyBigWig/blob/339ebc8d1765006cf736a6b5dd644b75aec63b14/libBigWig/bwValues.c#L308

And here the RTree is parsed only if idx->root is not zero, assuming that it was initialized to zero by readRTreeIdx, which is not the case.

Possible fix:

--- a/libBigWig/bwValues.c
+++ b/libBigWig/bwValues.c
@@ -35,6 +35,7 @@ static bwRTree_t *readRTreeIdx(bigWigFile_t *fp, uint64_t offset) {

     node = malloc(sizeof(bwRTree_t));
     if(!node) return NULL;
+    node->root = 0;

     if(bwRead(&(node->blockSize), sizeof(uint32_t), 1, fp) != 1) goto error;
     if(bwRead(&(node->nItems), sizeof(uint64_t), 1, fp) != 1) goto error;
dpryan79 commented 5 years ago

Good catch, I'll change that to calloc.

dpryan79 commented 5 years ago

I'll push out a version with this fixed now.