bam_index.c:
authorHenry Amrhein <hamrhein@caltech.edu>
Thu, 3 Jan 2013 06:16:32 +0000 (22:16 -0800)
committerHenry Amrhein <hamrhein@caltech.edu>
Thu, 3 Jan 2013 06:16:32 +0000 (22:16 -0800)
added error checking for malloc(), calloc() and realloc() calls.

bam_index.c

index 8dc7ce38171fcc830b22f3e642a13a165ee2f4a7..c7f33959bd99ddef3a9a628d885e450fc2e07acf 100644 (file)
@@ -80,11 +80,17 @@ static inline void insert_offset(khash_t(i) *h, int bin, uint64_t beg, uint64_t
        l = &kh_value(h, k);
        if (ret) { // not present
                l->m = 1; l->n = 0;
-               l->list = (pair64_t*)calloc(l->m, 16);
+               if ((l->list = (pair64_t*)calloc(l->m, 16)) == NULL) {
+                       fprintf(stderr, "[%s] failed to allocate bam_binlist node.\n", __func__);
+                       abort();
+               }
        }
        if (l->n == l->m) {
                l->m <<= 1;
-               l->list = (pair64_t*)realloc(l->list, l->m * 16);
+               if ((l->list = (pair64_t*)realloc(l->list, l->m * 16)) == NULL) {
+                       fprintf(stderr, "[%s] failed to resize bam_binlist node.\n", __func__);
+                       abort();
+               }
        }
        l->list[l->n].u = beg; l->list[l->n++].v = end;
 }
@@ -98,7 +104,10 @@ static inline void insert_offset2(bam_lidx_t *index2, bam1_t *b, uint64_t offset
                int old_m = index2->m;
                index2->m = end + 1;
                kroundup32(index2->m);
-               index2->offset = (uint64_t*)realloc(index2->offset, index2->m * 8);
+               if ((index2->offset = (uint64_t*)realloc(index2->offset, index2->m * 8)) == NULL) {
+                       fprintf(stderr, "[%s] failed to resize offset array.\n", __func__);
+                       abort();
+               }
                memset(index2->offset + old_m, 0, 8 * (index2->m - old_m));
        }
        if (beg == end) {
@@ -159,16 +168,34 @@ bam_index_t *bam_index_core(bamFile fp)
        bam1_core_t *c;
        uint64_t save_off, last_off, n_mapped, n_unmapped, off_beg, off_end, n_no_coor;
 
-       idx = (bam_index_t*)calloc(1, sizeof(bam_index_t));
-       b = (bam1_t*)calloc(1, sizeof(bam1_t));
+       if ((idx = (bam_index_t*)calloc(1, sizeof(bam_index_t))) == NULL) {
+               fprintf(stderr, "[%s] failed to allocate bam_index structure.\n", __func__);
+               return NULL;
+       }
+       if ((b = (bam1_t*)calloc(1, sizeof(bam1_t))) == NULL) {
+               fprintf(stderr, "[%s] failed to allocate bam1_t buffer.\n", __func__);
+               free(idx);
+               return NULL;
+       }
        h = bam_header_read(fp);
        c = &b->core;
 
        idx->n = h->n_targets;
        bam_header_destroy(h);
-       idx->index = (khash_t(i)**)calloc(idx->n, sizeof(void*));
+       if ((idx->index = (khash_t(i)**)calloc(idx->n, sizeof(void*))) == NULL) {
+               fprintf(stderr, "[%s] failed to allocate bam_index bin hash.\n", __func__);
+               free(b);
+               free(idx);
+               return NULL;
+       }
        for (i = 0; i < idx->n; ++i) idx->index[i] = kh_init(i);
-       idx->index2 = (bam_lidx_t*)calloc(idx->n, sizeof(bam_lidx_t));
+       if ((idx->index2 = (bam_lidx_t*)calloc(idx->n, sizeof(bam_lidx_t))) == NULL) {
+               fprintf(stderr, "[%s] failed to allocate bam_index linear index.\n", __func__);
+               free(idx->index);
+               free(b);
+               free(idx);
+               return NULL;
+       }
 
        save_bin = save_tid = last_tid = last_bin = 0xffffffffu;
        save_off = last_off = bam_tell(fp); last_coor = 0xffffffffu;
@@ -400,15 +427,32 @@ static bam_index_t *bam_index_load_core(FILE *fp)
                fclose(fp);
                return 0;
        }
-       idx = (bam_index_t*)calloc(1, sizeof(bam_index_t));     
+       if ((idx = (bam_index_t*)calloc(1, sizeof(bam_index_t))) == NULL) {
+               fprintf(stderr, "[%s] failed to allocate bam_index structure.\n", __func__);
+               fclose(fp);
+               return NULL;
+       }
        if (fread(&idx->n, 4, 1, fp) < 1) {
                fprintf(stderr, "[%s] failed to read n_ref.\n", __func__);
+               free(idx);
                fclose(fp);
                return 0;
        }
        if (bam_is_be) bam_swap_endian_4p(&idx->n);
-       idx->index = (khash_t(i)**)calloc(idx->n, sizeof(void*));
-       idx->index2 = (bam_lidx_t*)calloc(idx->n, sizeof(bam_lidx_t));
+       if ((idx->index = (khash_t(i)**)calloc(idx->n, sizeof(void*))) == NULL) {
+               fprintf(stderr, "[%s] failed to allocate bam_index bin hash.\n", __func__);
+               free(idx);
+               fclose(fp);
+               return NULL;
+       }
+       if ((idx->index2 = (bam_lidx_t*)calloc(idx->n, sizeof(bam_lidx_t))) == NULL) {
+               fprintf(stderr, "[%s] failed to allocate bam_index linear index.\n", __func__);
+               free(idx->index);
+               free(idx);
+               fclose(fp);
+               return NULL;
+       }
+
        for (i = 0; i < idx->n; ++i) {
                khash_t(i) *index;
                bam_lidx_t *index2 = idx->index2 + i;
@@ -420,6 +464,8 @@ static bam_index_t *bam_index_load_core(FILE *fp)
                // load binning index
                if (fread(&size, 4, 1, fp) < 1) {
                        fprintf(stderr, "[%s] failed to read n_bin.\n", __func__);
+                       free(idx->index);
+                       free(idx);
                        fclose(fp);
                        return 0;
                }
@@ -427,6 +473,8 @@ static bam_index_t *bam_index_load_core(FILE *fp)
                for (j = 0; j < (int)size; ++j) {
                        if (fread(&key, 4, 1, fp) < 1) {
                                fprintf(stderr, "[%s] failed to read bin.\n", __func__);
+                               free(idx->index);
+                               free(idx);
                                fclose(fp);
                                return 0;
                        }
@@ -435,14 +483,24 @@ static bam_index_t *bam_index_load_core(FILE *fp)
                        p = &kh_value(index, k);
                        if (fread(&p->n, 4, 1, fp) < 1) {
                                fprintf(stderr, "[%s] failed to read n_chunk.\n", __func__);
+                               free(idx->index);
+                               free(idx);
                                fclose(fp);
                                return 0;
                        }
                        if (bam_is_be) bam_swap_endian_4p(&p->n);
                        p->m = p->n;
-                       p->list = (pair64_t*)malloc(p->m * 16);
+                       if ((p->list = (pair64_t*)malloc(p->m * 16)) == NULL) {
+                               fprintf(stderr, "[%s] failed to allocate chunk array.\n", __func__);
+                               free(idx->index);
+                               free(idx);
+                               fclose(fp);
+                               return 0;
+                       }
                        if (fread(p->list, 16, p->n, fp) < p->n) {
                                fprintf(stderr, "[%s] failed to read %d chunks.\n", __func__, p->n);
+                               free(idx->index);
+                               free(idx);
                                fclose(fp);
                                return 0;
                        }
@@ -457,15 +515,26 @@ static bam_index_t *bam_index_load_core(FILE *fp)
                // load linear index
                if (fread(&index2->n, 4, 1, fp) < 1) {
                        fprintf(stderr, "[%s] failed to read n_intv", __func__);
+                       free(idx->index);
+                       free(idx);
                        fclose(fp);
                        return 0;
                }
                if (bam_is_be) bam_swap_endian_4p(&index2->n);
                if (index2->n > 0) {
                        index2->m = index2->n;
-                       index2->offset = (uint64_t*)calloc(index2->m, 8);
+                       if ((index2->offset = (uint64_t*)calloc(index2->m, 8)) == NULL) {
+                               fprintf(stderr, "[%s] falied to allocate interval array.\n", __func__);
+                               free(idx->index);
+                               free(idx);
+                               fclose(fp);
+                               return 0;
+                       }
                        if (fread(index2->offset, index2->n, 8, fp) < index2->n) {
                                fprintf(stderr, "[%s] failed to read %d intervals.", __func__, index2->n);
+                               free(index2->offset);
+                               free(idx->index);
+                               free(idx);
                                fclose(fp);
                                return 0;
                        }
@@ -493,7 +562,11 @@ bam_index_t *bam_index_load_local(const char *_fn)
                        if (*p == '/') break;
                fn = strdup(p + 1);
        } else fn = strdup(_fn);
-       fnidx = (char*)calloc(strlen(fn) + 5, 1);
+       if ((fnidx = (char*)calloc(strlen(fn) + 5, 1)) == NULL) {
+               fprintf(stderr, "[%s] failed to allocate space for index file name.\n", __func__);
+               free(fn);
+               return NULL;
+       }
        strcpy(fnidx, fn); strcat(fnidx, ".bai");
        fp = fopen(fnidx, "rb");
        if (fp == 0) { // try "{base}.bai"
@@ -509,7 +582,11 @@ bam_index_t *bam_index_load_local(const char *_fn)
                bam_index_t *idx = bam_index_load_core(fp);
                fclose(fp);
                return idx;
-       } else return 0;
+       }
+       else {
+               fprintf(stderr, "[%s] failed to open index file.\n", __func__);
+               return 0;
+       }
 }
 
 #ifdef _USE_KNETFILE
@@ -528,18 +605,22 @@ static void download_from_remote(const char *url)
        ++fn; // fn now points to the file name
        fp_remote = knet_open(url, "r");
        if (fp_remote == 0) {
-               fprintf(stderr, "[download_from_remote] fail to open remote file.\n");
+               fprintf(stderr, "[%s] failed to open remote file.\n", __func__);
                return;
        }
        if ((fp = fopen(fn, "wb")) == 0) {
-               fprintf(stderr, "[download_from_remote] fail to create file in the working directory.\n");
+               fprintf(stderr, "[%s] failed to create file in the working directory.\n", __func__);
                knet_close(fp_remote);
                return;
        }
-       buf = (uint8_t*)calloc(buf_size, 1);
+       if ((buf = (uint8_t*)calloc(buf_size, 1)) == NULL) {
+               fprintf(stderr, "[%s] failed to allocate buffer.\n", __func__);
+               fclose(fp);
+               return;
+       }
        while ((l = knet_read(fp_remote, buf, buf_size)) != 0) {
                if (fwrite(buf, 1, l, fp) < l) {
-                       fprintf(stderr, "[%s] fail to write to destination file.\n", __func__);
+                       fprintf(stderr, "[%s] failed to write to destination file.\n", __func__);
                        break;
                }
        }
@@ -560,12 +641,16 @@ bam_index_t *bam_index_load(const char *fn)
        idx = bam_index_load_local(fn);
        if (idx == 0 && (strstr(fn, "ftp://") == fn || strstr(fn, "http://") == fn)) {
                char *fnidx = calloc(strlen(fn) + 5, 1);
+               if (fnidx == NULL) {
+                       fprintf(stderr, "[%s] failed to allocate space for index filename.\n", __func__);
+                       return NULL;
+               }
                strcat(strcpy(fnidx, fn), ".bai");
-               fprintf(stderr, "[bam_index_load] attempting to download the remote index file.\n");
+               fprintf(stderr, "[%s] attempting to download the remote index file.\n", __func__);
                download_from_remote(fnidx);
                idx = bam_index_load_local(fn);
        }
-       if (idx == 0) fprintf(stderr, "[bam_index_load] fail to load BAM index.\n");
+       if (idx == 0) fprintf(stderr, "[%s] fail to load BAM index.\n", __func__);
        return idx;
 }
 
@@ -576,13 +661,13 @@ int bam_index_build2(const char *fn, const char *_fnidx)
        bamFile fp;
        bam_index_t *idx;
        if ((fp = bam_open(fn, "r")) == 0) {
-               fprintf(stderr, "[bam_index_build2] fail to open the BAM file.\n");
+               fprintf(stderr, "[%s] failure to open the BAM file.\n", __func__);
                return -1;
        }
        idx = bam_index_core(fp);
        bam_close(fp);
        if(idx == 0) {
-               fprintf(stderr, "[bam_index_build2] fail to index the BAM file.\n");
+               fprintf(stderr, "[%s] fail to index the BAM file.\n", __func__);
                return -1;
        }
        if (_fnidx == 0) {
@@ -591,7 +676,7 @@ int bam_index_build2(const char *fn, const char *_fnidx)
        } else fnidx = strdup(_fnidx);
        fpidx = fopen(fnidx, "wb");
        if (fpidx == 0) {
-               fprintf(stderr, "[bam_index_build2] fail to create the index file.\n");
+               fprintf(stderr, "[%s] fail to create the index file.\n", __func__);
                free(fnidx);
                return -1;
        }
@@ -693,17 +778,23 @@ bam_iter_t bam_iter_query(const bam_index_t *idx, int tid, int beg, int end)
        if (beg < 0) beg = 0;
        if (end < beg) return 0;
        // initialize iter
-       iter = calloc(1, sizeof(struct __bam_iter_t));
+       if ((iter = calloc(1, sizeof(struct __bam_iter_t))) == NULL) {
+               fprintf(stderr, "[%s] failed to allocate bam iterator.\n", __func__);
+               abort();
+       }
        iter->tid = tid, iter->beg = beg, iter->end = end; iter->i = -1;
        //
-       bins = (uint16_t*)calloc(BAM_MAX_BIN, 2);
+       if ((bins = (uint16_t*)calloc(BAM_MAX_BIN, 2)) == NULL) {
+               fprintf(stderr, "[%s] failed to allocate bin array.\n", __func__);
+               abort();
+       }
        n_bins = reg2bins(beg, end, bins);
        index = idx->index[tid];
        if (idx->index2[tid].n > 0) {
                min_off = (beg>>BAM_LIDX_SHIFT >= idx->index2[tid].n)? idx->index2[tid].offset[idx->index2[tid].n-1]
                        : idx->index2[tid].offset[beg>>BAM_LIDX_SHIFT];
                if (min_off == 0) { // improvement for index files built by tabix prior to 0.1.4
-                       int n = beg>>BAM_LIDX_SHIFT;
+                       int n = beg >> BAM_LIDX_SHIFT;
                        if (n > idx->index2[tid].n) n = idx->index2[tid].n;
                        for (i = n - 1; i >= 0; --i)
                                if (idx->index2[tid].offset[i] != 0) break;
@@ -717,7 +808,10 @@ bam_iter_t bam_iter_query(const bam_index_t *idx, int tid, int beg, int end)
        if (n_off == 0) {
                free(bins); return iter;
        }
-       off = (pair64_t*)calloc(n_off, 16);
+       if ((off = (pair64_t*)calloc(n_off, 16)) == NULL) {
+               fprintf(stderr, "[%s] failed to allocate offset pair array.\n", __func__);
+               abort();
+       }
        for (i = n_off = 0; i < n_bins; ++i) {
                if ((k = kh_get(i, index, bins[i])) != kh_end(index)) {
                        int j;
@@ -732,6 +826,10 @@ bam_iter_t bam_iter_query(const bam_index_t *idx, int tid, int beg, int end)
        }
        {
                bam1_t *b = (bam1_t*)calloc(1, sizeof(bam1_t));
+               if (b == NULL) {
+                       fprintf(stderr, "[%s] failure to allocate bam1_t buffer.\n", __func__);
+                       abort();
+               }
                int l;
                ks_introsort(off, n_off, off);
                // resolve completely contained adjacent blocks