Skip to content

Commit 68dbce0

Browse files
committed
Fix on-the-fly indexing of VCF w.r.t virtual offsets.
When using bcftools view --write-index -o out.vcf.gz the virtual file offsets can differ depending on whether we do a bgzf_tell before or after a flush. Specifically it could point to the last byte in the current BGZF block or the first byte in the next BGZF block. Ultimately both of these resolve to the same physical location, but in some situations the former may mean attempting to read zero bytes (the remainder of the bgzf block). This has been known in the past to be misinterpreted as an EOF. (See samtools/samtools#1861) It also means the contents of the index produced by --write-index and a separate bcftools index command can yield different results, albeit both representing the same data. The fix for the samtools / bcftools issue above (#1672) when multi-threading inadvertently recreated the bug when not multi-threading.
1 parent bf11805 commit 68dbce0

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ Bug fixes
8989
* Fix small OSS-Fuzz reported issues with CRAM encoding and long
9090
CIGARS and/or illegal positions. (PR #1775, PR #1801, PR #1817)
9191

92+
* Fix issues with on-the-fly indexing of VCF/BCF (bcftools --write-index)
93+
when not using multiple threads. (PR #1837. Fixes samtools/bcftools#2267,
94+
reported by Giulio Genovese)
95+
9296
* Stricter limits on POS / MPOS / TLEN in sam_parse1(). This fixes
9397
a signed overflow reported by OSS-Fuzz and should help prevent other
9498
as-yet undetected bugs. (PR #1812)

vcf.c

+2
Original file line numberDiff line numberDiff line change
@@ -4238,6 +4238,8 @@ int vcf_write(htsFile *fp, const bcf_hdr_t *h, bcf1_t *v)
42384238
if ( fp->format.compression!=no_compression ) {
42394239
if (bgzf_flush_try(fp->fp.bgzf, fp->line.l) < 0)
42404240
return -1;
4241+
if (fp->idx && !fp->fp.bgzf->mt)
4242+
hts_idx_amend_last(fp->idx, bgzf_tell(fp->fp.bgzf));
42414243
ret = bgzf_write(fp->fp.bgzf, fp->line.s, fp->line.l);
42424244
} else {
42434245
ret = hwrite(fp->fp.hfile, fp->line.s, fp->line.l);

0 commit comments

Comments
 (0)