From 66ee6e74da3e79dbbd70e516ef73c34a1cf1c8fa Mon Sep 17 00:00:00 2001 From: Michael Macias Date: Wed, 1 Nov 2023 14:55:29 -0500 Subject: [PATCH] bam/reader/header: Handle optional trailing NUL in SAM header text This previously would transform the read into an empty line, but the `NUL` should be signaling EOS instead. --- noodles-bam/CHANGELOG.md | 6 ++++++ noodles-bam/src/reader/header.rs | 28 ++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/noodles-bam/CHANGELOG.md b/noodles-bam/CHANGELOG.md index f5ca021f1..6f6f6e822 100644 --- a/noodles-bam/CHANGELOG.md +++ b/noodles-bam/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixed + + * bam/reader/header: Handle optional trailing `NUL` in SAM header text. + ## 0.49.0 - 2023-10-26 ### Changed diff --git a/noodles-bam/src/reader/header.rs b/noodles-bam/src/reader/header.rs index efb1913b0..3a889bd71 100644 --- a/noodles-bam/src/reader/header.rs +++ b/noodles-bam/src/reader/header.rs @@ -70,10 +70,10 @@ where let mut buf = Vec::new(); while read_line(&mut header_reader, &mut buf)? != 0 { - // § 4.2 The BAM format (2021-06-03): "Plain header text in SAM; not necessarily - // NUL-terminated". - if buf.ends_with(&[NUL]) { - buf.pop(); + // § 4.2 "The BAM format" (2023-05-24): "Plain header text in SAM; not necessarily + // `NUL`-terminated". + if buf == [NUL] { + break; } parser @@ -232,6 +232,26 @@ mod tests { Ok(()) } + #[test] + fn test_read_header_with_trailing_nul_in_text() -> Result<(), Box> { + let mut data = Vec::new(); + data.put_slice(MAGIC_NUMBER); // magic + data.put_u32_le(12); // l_text + data.put_slice(b"@HD\tVN:1.6\n\x00"); // text + data.put_u32_le(0); // n_ref + + let mut reader = &data[..]; + let actual = read_header(&mut reader)?; + + let expected = sam::Header::builder() + .set_header(Map::::new(Version::new(1, 6))) + .build(); + + assert_eq!(actual, expected); + + Ok(()) + } + #[test] fn test_read_header_with_missing_sam_header_reference_sequence_dictionary( ) -> Result<(), Box> {