From: Lee Baker Date: Sat, 9 Mar 2013 19:27:56 +0000 (-0600) Subject: Fix memory leaks: X-Git-Url: https://git.donarmstrong.com/?p=samtools.git;a=commitdiff_plain;h=2fa4cde9b8806faceffb8677440b54ca4c90b6f7 Fix memory leaks: * When downloading a remote FASTA index * When indexing a BAM, we can't open the FAI file * When indexing a BCF, we can't open the index file * Parsing a VCF header with no sample line * Encountering multiple adjacent tabs in a SAM header --- diff --git a/bam_index.c b/bam_index.c index d6b94e2..f916e04 100644 --- a/bam_index.c +++ b/bam_index.c @@ -464,6 +464,7 @@ bam_index_t *bam_index_load(const char *fn) strcat(strcpy(fnidx, fn), ".bai"); fprintf(stderr, "[bam_index_load] attempting to download the remote index file.\n"); download_from_remote(fnidx); + free(fnidx); idx = bam_index_load_local(fn); } if (idx == 0) fprintf(stderr, "[bam_index_load] fail to load BAM index.\n"); @@ -494,6 +495,7 @@ int bam_index_build2(const char *fn, const char *_fnidx) if (fpidx == 0) { fprintf(stderr, "[bam_index_build2] fail to create the index file.\n"); free(fnidx); + bam_index_destroy(idx); return -1; } bam_index_save(idx, fpidx); diff --git a/bcftools/index.c b/bcftools/index.c index 014856d..a7db24f 100644 --- a/bcftools/index.c +++ b/bcftools/index.c @@ -259,6 +259,7 @@ int bcf_idx_build2(const char *fn, const char *_fnidx) if (fpidx == 0) { fprintf(stderr, "[bcf_idx_build2] fail to create the index file.\n"); free(fnidx); + bcf_idx_destroy(idx); return -1; } bcf_idx_save(idx, fpidx); diff --git a/bcftools/vcf.c b/bcftools/vcf.c index bc11084..e8526a3 100644 --- a/bcftools/vcf.c +++ b/bcftools/vcf.c @@ -30,7 +30,12 @@ bcf_hdr_t *vcf_hdr_read(bcf_t *bp) memset(&smpl, 0, sizeof(kstring_t)); while (ks_getuntil(v->ks, '\n', &v->line, &dret) >= 0) { if (v->line.l < 2) continue; - if (v->line.s[0] != '#') return 0; // no sample line + if (v->line.s[0] != '#') { + free(meta.s); + free(smpl.s); + free(h); + return 0; // no sample line + } if (v->line.s[0] == '#' && v->line.s[1] == '#') { kputsn(v->line.s, v->line.l, &meta); kputc('\n', &meta); } else if (v->line.s[0] == '#') { diff --git a/sam_header.c b/sam_header.c index a1b5181..ddc2c38 100644 --- a/sam_header.c +++ b/sam_header.c @@ -366,6 +366,7 @@ static HeaderLine *sam_header_line_parse(const char *headerLine) while (*to && *to=='\t') to++; if ( to-from != 1 ) { debug("[sam_header_line_parse] multiple tabs on line [%s] (%d)\n", headerLine,(int)(to-from)); + free(hline); return 0; } from = to;