Skip to content

Commit

Permalink
Fix type mismatches between types in sizeof() in malloc/calloc/reallo…
Browse files Browse the repository at this point in the history
…c calls. This generates a warning in the clang analyzer, designed to catch incorrectly sized memory allocations.

The allocation at bam_plcmd.c:208 is twice as big as it needs to be, and has been fixed.
  • Loading branch information
leecbaker committed Mar 10, 2013
1 parent 51467d0 commit 4ce9296
Show file tree
Hide file tree
Showing 20 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion bam2bcf_indel.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ int bcf_call_gap_prep(int n, int *n_plp, bam_pileup1_t **plp, int pos, bcf_calla
int L = right - left + 1, max_i, max2_i;
uint32_t *cns, max, max2;
char *ref0, *r;
ref_sample = calloc(n, sizeof(void*));
ref_sample = calloc(n, sizeof(char*));
cns = calloc(L, 4);
ref0 = calloc(L, 1);
for (i = 0; i < right - left; ++i)
Expand Down
4 changes: 2 additions & 2 deletions bam2depth.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ int main_depth(int argc, char *argv[])
}
else
n = argc - optind; // the number of BAMs on the command line
data = calloc(n, sizeof(void*)); // data[i] for the i-th input
data = calloc(n, sizeof(aux_t*)); // data[i] for the i-th input
beg = 0; end = 1<<30; tid = -1; // set the default region
for (i = 0; i < n; ++i) {
bam_header_t *htmp;
Expand All @@ -107,7 +107,7 @@ int main_depth(int argc, char *argv[])
// the core multi-pileup loop
mplp = bam_mplp_init(n, read_bam, (void**)data); // initialization
n_plp = calloc(n, sizeof(int)); // n_plp[i] is the number of covering reads from the i-th BAM
plp = calloc(n, sizeof(void*)); // plp[i] points to the array of covering reads (internal in mplp)
plp = calloc(n, sizeof(bam_pileup1_t*)); // plp[i] points to the array of covering reads (internal in mplp)
while (bam_mplp_auto(mplp, &tid, &pos, n_plp, plp) > 0) { // come to the next covered position
if (pos < beg || pos >= end) continue; // out of range; skip
if (bed && bed_overlap(bed, h->target_name[tid], pos, pos + 1) == 0) continue; // not in BED; skip
Expand Down
2 changes: 1 addition & 1 deletion bam_import.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ int sam_header_parse(bam_header_t *h)
if (h->dict == 0) h->dict = sam_header_parse2(h->text);
tmp = sam_header2list(h->dict, "SQ", "SN", &h->n_targets);
if (h->n_targets == 0) return 0;
h->target_name = calloc(h->n_targets, sizeof(void*));
h->target_name = calloc(h->n_targets, sizeof(char*));
for (i = 0; i < h->n_targets; ++i)
h->target_name[i] = strdup(tmp[i]);
free(tmp);
Expand Down
4 changes: 2 additions & 2 deletions bam_index.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ bam_index_t *bam_index_core(bamFile fp)

idx->n = h->n_targets;
bam_header_destroy(h);
idx->index = (khash_t(i)**)calloc(idx->n, sizeof(void*));
idx->index = (khash_t(i)**)calloc(idx->n, sizeof(khash_t(i)*));
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));

Expand Down Expand Up @@ -341,7 +341,7 @@ static bam_index_t *bam_index_load_core(FILE *fp)
idx = (bam_index_t*)calloc(1, sizeof(bam_index_t));
fread(&idx->n, 4, 1, fp);
if (bam_is_be) bam_swap_endian_4p(&idx->n);
idx->index = (khash_t(i)**)calloc(idx->n, sizeof(void*));
idx->index = (khash_t(i)**)calloc(idx->n, sizeof(khash_t(i)*));
idx->index2 = (bam_lidx_t*)calloc(idx->n, sizeof(bam_lidx_t));
for (i = 0; i < idx->n; ++i) {
khash_t(i) *index;
Expand Down
2 changes: 1 addition & 1 deletion bam_lpileup.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ static int tview_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pl
if (tv->n_nodes + 1 > tv->m_aux) { // enlarge
tv->m_aux = tv->n_nodes + 1;
kroundup32(tv->m_aux);
tv->aux = (freenode_t**)realloc(tv->aux, sizeof(void*) * tv->m_aux);
tv->aux = (freenode_t**)realloc(tv->aux, sizeof(freenode_t*) * tv->m_aux);
}
for (p = tv->head, i = l = 0; p->next;) {
if (p->level > max_level) { // then discard this entry
Expand Down
4 changes: 2 additions & 2 deletions bam_pileup.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ bam_mplp_t bam_mplp_init(int n, bam_plp_auto_f func, void **data)
iter = calloc(1, sizeof(struct __bam_mplp_t));
iter->pos = calloc(n, 8);
iter->n_plp = calloc(n, sizeof(int));
iter->plp = calloc(n, sizeof(void*));
iter->iter = calloc(n, sizeof(void*));
iter->plp = calloc(n, sizeof(bam_pileup1_t*));
iter->iter = calloc(n, sizeof(bam_plp_t));
iter->n = n;
iter->min = (uint64_t)-1;
for (i = 0; i < n; ++i) {
Expand Down
8 changes: 4 additions & 4 deletions bam_plcmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ static int mpileup(mplp_conf_t *conf, int n, char **fn)
memset(&gplp, 0, sizeof(mplp_pileup_t));
memset(&buf, 0, sizeof(kstring_t));
memset(&bc, 0, sizeof(bcf_call_t));
data = calloc(n, sizeof(void*));
plp = calloc(n, sizeof(void*));
n_plp = calloc(n, sizeof(int*));
data = calloc(n, sizeof(mplp_aux_t*));
plp = calloc(n, sizeof(bam_pileup1_t*));
n_plp = calloc(n, sizeof(int));
sm = bam_smpl_init();

// read the header and initialize data
Expand Down Expand Up @@ -248,7 +248,7 @@ static int mpileup(mplp_conf_t *conf, int n, char **fn)
gplp.n = sm->n;
gplp.n_plp = calloc(sm->n, sizeof(int));
gplp.m_plp = calloc(sm->n, sizeof(int));
gplp.plp = calloc(sm->n, sizeof(void*));
gplp.plp = calloc(sm->n, sizeof(bam_pileup1_t*));

fprintf(stderr, "[%s] %d samples in %d input files\n", __func__, sm->n, n);
// write the VCF header
Expand Down
4 changes: 2 additions & 2 deletions bam_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ int bam_merge_core2(int by_qname, const char *out, const char *headers, int n, c
iter = (bam_iter_t*)calloc(n, sizeof(bam_iter_t));
// prepare RG tag
if (flag & MERGE_RG) {
RG = (char**)calloc(n, sizeof(void*));
RG = (char**)calloc(n, sizeof(char*));
RG_len = (int*)calloc(n, sizeof(int));
for (i = 0; i != n; ++i) {
int l = strlen(fn[i]);
Expand Down Expand Up @@ -469,7 +469,7 @@ void bam_sort_core_ext(int is_by_qname, const char *fn, const char *prefix, size
if (k == max_k) {
size_t old_max = max_k;
max_k = max_k? max_k<<1 : 0x10000;
buf = realloc(buf, max_k * sizeof(void*));
buf = realloc(buf, max_k * sizeof(bam1_t*));
memset(buf + old_max, 0, sizeof(void*) * (max_k - old_max));
}
if (buf[k] == 0) buf[k] = (bam1_t*)calloc(1, sizeof(bam1_t));
Expand Down
4 changes: 2 additions & 2 deletions bamshuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ static void bamshuf(const char *fn, int n_files, const char *pre, int clevel, in
fp = strcmp(fn, "-")? bgzf_open(fn, "r") : bgzf_dopen(fileno(stdin), "r");
assert(fp);
h = bam_hdr_read(fp);
fnt = (char**)calloc(n_files, sizeof(void*));
fpt = (BGZF**)calloc(n_files, sizeof(void*));
fnt = (char**)calloc(n_files, sizeof(char*));
fpt = (BGZF**)calloc(n_files, sizeof(BGZF*));
cnt = (int64_t*)calloc(n_files, 8);
l = strlen(pre);
for (i = 0; i < n_files; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion bcftools/bcf.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static inline char **cnt_null(int l, char *str, int *_n)
for (p = str; p != str + l; ++p)
if (*p == 0) ++n;
*_n = n;
list = calloc(n, sizeof(void*));
list = calloc(n, sizeof(char*));
list[0] = str;
for (p = str, n = 1; p < str + l - 1; ++p)
if (*p == 0) list[n++] = p + 1;
Expand Down
2 changes: 1 addition & 1 deletion bcftools/call1.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ static char **read_samples(const char *fn, int *_n)
int l;
if (max == n) {
max = max? max<<1 : 4;
sam = realloc(sam, sizeof(void*)*max);
sam = realloc(sam, sizeof(char*)*max);
}
l = s.l;
sam[n] = malloc(s.l + 2);
Expand Down
2 changes: 1 addition & 1 deletion bcftools/prob1.c
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ static double contrast2(bcf_p1aux_t *p1, double ret[3])
computation in this block can be accelerated with a similar strategy, but perhaps this
is not a serious concern for now. */
double tmp = lgamma(2*(n1+n2)+1) - (lgamma(2*n1+1) + lgamma(2*n2+1));
p1->hg = calloc(2*n1+1, sizeof(void*));
p1->hg = calloc(2*n1+1, sizeof(double*));
for (k1 = 0; k1 <= 2*n1; ++k1) {
p1->hg[k1] = calloc(2*n2+1, sizeof(double));
for (k2 = 0; k2 <= 2*n2; ++k2)
Expand Down
6 changes: 3 additions & 3 deletions bedcov.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ int main_bedcov(int argc, char *argv[])
}
memset(&str, 0, sizeof(kstring_t));
n = argc - optind - 1;
aux = calloc(n, sizeof(void*));
idx = calloc(n, sizeof(void*));
aux = calloc(n, sizeof(aux_t*));
idx = calloc(n, sizeof(bam_index_t*));
for (i = 0; i < n; ++i) {
aux[i] = calloc(1, sizeof(aux_t));
aux[i]->min_mapQ = min_mapQ;
Expand All @@ -69,7 +69,7 @@ int main_bedcov(int argc, char *argv[])
fp = gzopen(argv[optind], "rb");
ks = ks_init(fp);
n_plp = calloc(n, sizeof(int));
plp = calloc(n, sizeof(void*));
plp = calloc(n, sizeof(bam_pileup1_t*));
while (ks_getuntil(ks, KS_SEP_LINE, &str, &dret) >= 0) {
char *p, *q;
int tid, beg, end, pos;
Expand Down
2 changes: 1 addition & 1 deletion faidx.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static inline void fai_insert_index(faidx_t *idx, const char *name, int len, int
faidx1_t t;
if (idx->n == idx->m) {
idx->m = idx->m? idx->m<<1 : 16;
idx->name = (char**)realloc(idx->name, sizeof(void*) * idx->m);
idx->name = (char**)realloc(idx->name, sizeof(char*) * idx->m);
}
idx->name[idx->n] = strdup(name);
k = kh_put(s, idx->hash, idx->name[idx->n], &ret);
Expand Down
4 changes: 2 additions & 2 deletions kprobaln.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ int kpa_glocal(const uint8_t *_ref, int l_ref, const uint8_t *_query, int l_quer
if (bw < abs(l_ref - l_query)) bw = abs(l_ref - l_query);
bw2 = bw * 2 + 1;
// allocate the forward and backward matrices f[][] and b[][] and the scaling array s[]
f = calloc(l_query+1, sizeof(void*));
if (is_backward) b = calloc(l_query+1, sizeof(void*));
f = calloc(l_query+1, sizeof(double*));
if (is_backward) b = calloc(l_query+1, sizeof(double*));
for (i = 0; i <= l_query; ++i) { // FIXME: this will lead in segfault for l_query==0
f[i] = calloc(bw2 * 3 + 6, sizeof(double)); // FIXME: this is over-allocated for very short seqs
if (is_backward) b[i] = calloc(bw2 * 3 + 6, sizeof(double));
Expand Down
8 changes: 4 additions & 4 deletions phase.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static int **count_all(int l, int vpos, nseq_t *hash)
int i, j, **cnt;
uint8_t *seq;
seq = calloc(l, 1);
cnt = calloc(vpos, sizeof(void*));
cnt = calloc(vpos, sizeof(int*));
for (i = 0; i < vpos; ++i) cnt[i] = calloc(1<<l, sizeof(int));
for (k = 0; k < kh_end(hash); ++k) {
if (kh_exist(hash, k)) {
Expand Down Expand Up @@ -116,7 +116,7 @@ static int8_t *dynaprog(int l, int vpos, int **w)
uint32_t x, z = 1u<<(l-1), mask = (1u<<l) - 1;
f[0] = calloc(z, sizeof(int));
f[1] = calloc(z, sizeof(int));
b = calloc(vpos, sizeof(void*));
b = calloc(vpos, sizeof(int8_t*));
prev = f[0]; curr = f[1];
// fill the backtrack matrix
for (i = 0; i < vpos; ++i) {
Expand Down Expand Up @@ -405,7 +405,7 @@ static int phase(phaseg_t *g, const char *chr, int vpos, uint64_t *cns, nseq_t *
i + g->vpos_shift + 1, (int)(x&0xffff), (int)(x>>16&0xffff), (int)(x>>32&0xffff), (int)(x>>48&0xffff));
}
free(path); free(pcnt); free(regmask); free(sitemask);
seqs = calloc(n_seqs, sizeof(void*));
seqs = calloc(n_seqs, sizeof(frag_t*));
for (k = 0, i = 0; k < kh_end(hash); ++k)
if (kh_exist(hash, k) && kh_val(hash, k).vpos < vpos && !kh_val(hash, k).single)
seqs[i++] = &kh_val(hash, k);
Expand Down Expand Up @@ -455,7 +455,7 @@ static int readaln(void *data, bam1_t *b)
if (!(b->core.flag & (BAM_FUNMAP|BAM_FSECONDARY|BAM_FQCFAIL|BAM_FDUP)) && g->pre) {
if (g->n == g->m) {
g->m = g->m? g->m<<1 : 16;
g->b = realloc(g->b, g->m * sizeof(void*));
g->b = realloc(g->b, g->m * sizeof(bam1_t*));
}
g->b[g->n++] = bam_dup1(b);
}
Expand Down
4 changes: 2 additions & 2 deletions razf.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static inline int is_big_endian(){
static void add_zindex(RAZF *rz, int64_t in, int64_t out){
if(rz->index->size == rz->index->cap){
rz->index->cap = rz->index->cap * 1.5 + 2;
rz->index->cell_offsets = realloc(rz->index->cell_offsets, sizeof(int) * rz->index->cap);
rz->index->cell_offsets = realloc(rz->index->cell_offsets, sizeof(uint32_t) * rz->index->cap);
rz->index->bin_offsets = realloc(rz->index->bin_offsets, sizeof(int64_t) * (rz->index->cap/RZ_BIN_SIZE + 1));
}
if(rz->index->size % RZ_BIN_SIZE == 0) rz->index->bin_offsets[rz->index->size / RZ_BIN_SIZE] = out;
Expand Down Expand Up @@ -132,7 +132,7 @@ static void load_zindex(RAZF *rz, int fd){
#else
read(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
#endif
rz->index->cell_offsets = malloc(sizeof(int) * rz->index->size);
rz->index->cell_offsets = malloc(sizeof(uint32_t) * rz->index->size);
#ifdef _USE_KNETFILE
knet_read(fp, rz->index->cell_offsets, sizeof(int) * rz->index->size);
#else
Expand Down
2 changes: 1 addition & 1 deletion sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bam_header_t *bam_header_dup(const bam_header_t *h0)
h->text = (char*)calloc(h->l_text + 1, 1);
memcpy(h->text, h0->text, h->l_text);
h->target_len = (uint32_t*)calloc(h->n_targets, 4);
h->target_name = (char**)calloc(h->n_targets, sizeof(void*));
h->target_name = (char**)calloc(h->n_targets, sizeof(char*));
for (i = 0; i < h->n_targets; ++i) {
h->target_len[i] = h0->target_len[i];
h->target_name[i] = strdup(h0->target_name[i]);
Expand Down
2 changes: 1 addition & 1 deletion sam_header.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ char **sam_header2list(const void *_dict, char type[2], char key_tag[2], int *_n

if (n == max) {
max = max? max<<1 : 4;
ret = realloc(ret, max * sizeof(void*));
ret = realloc(ret, max * sizeof(char*));
}
ret[n++] = key->value;

Expand Down
2 changes: 1 addition & 1 deletion sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static void add_pair(bam_sample_t *sm, khash_t(sm) *sm2id, const char *key, cons
if (k_sm == kh_end(sm2id)) { // absent
if (sm->n == sm->m) {
sm->m = sm->m? sm->m<<1 : 1;
sm->smpl = realloc(sm->smpl, sizeof(void*) * sm->m);
sm->smpl = realloc(sm->smpl, sizeof(char*) * sm->m);
}
sm->smpl[sm->n] = strdup(val);
k_sm = kh_put(sm, sm2id, sm->smpl[sm->n], &ret);
Expand Down

0 comments on commit 4ce9296

Please sign in to comment.