Skip to content

Commit

Permalink
bam/reader/header: Handle optional trailing NUL in SAM header text
Browse files Browse the repository at this point in the history
This previously would transform the read into an empty line, but the
`NUL` should be signaling EOS instead.
  • Loading branch information
zaeleus committed Nov 2, 2023
1 parent a04da59 commit 66ee6e7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
6 changes: 6 additions & 0 deletions noodles-bam/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
28 changes: 24 additions & 4 deletions noodles-bam/src/reader/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -232,6 +232,26 @@ mod tests {
Ok(())
}

#[test]
fn test_read_header_with_trailing_nul_in_text() -> Result<(), Box<dyn std::error::Error>> {
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::<map::Header>::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<dyn std::error::Error>> {
Expand Down

0 comments on commit 66ee6e7

Please sign in to comment.