diff --git a/sam.c b/sam.c index 878fcc270..dc8b9c183 100644 --- a/sam.c +++ b/sam.c @@ -3220,8 +3220,10 @@ int sam_format1(const bam_hdr_t *h, const bam1_t *b, kstring_t *str) // reference count the structure. int sam_write1(htsFile *fp, const sam_hdr_t *hdr, const bam1_t *b) { - const sam_hdr_t *h = hdr; - if (!h) h = fp->bam_header; + if (!fp || !b) { errno = EINVAL; return -1; } + const sam_hdr_t *h = hdr ? hdr : fp->bam_header; + if (!h) { hts_log_error("No header available for file \"%s\"", fp->fn); return -1; } + switch (fp->format.format) { case binary_format: fp->format.category = sequence_data; diff --git a/vcf.c b/vcf.c index 8416c5e80..1905c7311 100644 --- a/vcf.c +++ b/vcf.c @@ -3296,7 +3296,11 @@ static int vcf_idx_init(htsFile *fp, bcf_hdr_t *h, int min_shift, const char *fn return -1; } fp->fnidx = strdup(fnidx); - if (!fp->fnidx) return -1; + if (!fp->fnidx) { + hts_idx_destroy(fp->idx); + fp->idx = NULL; + return -1; + } return 0; } @@ -3317,7 +3321,11 @@ int bcf_idx_init(htsFile *fp, bcf_hdr_t *h, int min_shift, const char *fnidx) { fp->idx = hts_idx_init(nids, HTS_FMT_CSI, bgzf_tell(fp->fp.bgzf), min_shift, n_lvls); if (!fp->idx) return -1; fp->fnidx = strdup(fnidx); - if (!fp->fnidx) return -1; + if (!fp->fnidx) { + hts_idx_destroy(fp->idx); + fp->idx = NULL; + return -1; + } return 0; }