Skip to content

Commit

Permalink
fix: Panic on trailing omitted FORMAT records (#417)
Browse files Browse the repository at this point in the history
* Handle trailing omitted FORMAT records

* Remove newline

* fix: Wrong if statement

* fmt

---------

Co-authored-by: Fenner Macrae <[email protected]>
Co-authored-by: Johannes Koester <[email protected]>
  • Loading branch information
3 people authored Mar 27, 2024
1 parent b6aeba4 commit 9f575ee
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/bcf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,24 @@ mod tests {
);
}

#[test]
fn test_trailing_omitted_format_fields() {
let mut reader = Reader::from_path("test/test_trailing_omitted_format.vcf").unwrap();
let first_record = reader
.records()
.next()
.unwrap()
.expect("Fail to read record");

let expected: Vec<&[u8]> = Vec::new();
assert_eq!(*first_record.format(b"STR").string().unwrap(), expected,);
assert_eq!(
*first_record.format(b"INT").integer().unwrap(),
vec![&[i32::missing()]],
);
assert!(first_record.format(b"FLT").float().unwrap()[0][0].is_nan(),);
}

// #[test]
// fn test_buffer_lifetime() {
// let mut reader = Reader::from_path("test/obs-cornercase.vcf").unwrap();
Expand Down
3 changes: 3 additions & 0 deletions src/bcf/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,9 @@ impl<'a, 'b, B: BorrowMut<Buffer> + Borrow<Buffer> + 'b> Format<'a, B> {
/// memory.
pub fn string(mut self) -> Result<BufferBacked<'b, Vec<&'b [u8]>, B>> {
self.data(htslib::BCF_HT_STR).map(|ret| {
if ret == 0 {
return BufferBacked::new(Vec::new(), self.buffer);
}
BufferBacked::new(
unsafe {
slice::from_raw_parts(self.buffer.borrow_mut().inner as *const u8, ret as usize)
Expand Down
9 changes: 9 additions & 0 deletions test/test_trailing_omitted_format.vcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
##fileformat=VCFv4.3
##contig=<ID=chr1,length=10000>
##INFO=<ID=FOO,Number=1,Type=Integer,Description="Some field">
##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
##FORMAT=<ID=STR,Number=1,Type=String,Description="Some string field">
##FORMAT=<ID=INT,Number=1,Type=Integer,Description="Some integer field">
##FORMAT=<ID=FLT,Number=1,Type=Float,Description="Some float field">
#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT SAMPLE1
chr1 1234 . t a . . FOO=1 GT:STR:FLT:INT .

0 comments on commit 9f575ee

Please sign in to comment.