Skip to content

Commit

Permalink
sam/io/writer/header/record/value/map: Add value validation
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Mar 14, 2024
1 parent 3b3e750 commit c3f9878
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
6 changes: 6 additions & 0 deletions noodles-sam/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Changed

* sam/io/writer/header/record/value/map: Add value validation.

## 0.54.0 - 2024-03-12

### Deprecated
Expand Down
30 changes: 29 additions & 1 deletion noodles-sam/src/io/writer/header/record/value/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where
write_delimiter(writer)?;
write_tag(writer, *tag.as_ref())?;
write_separator(writer)?;
writer.write_all(value)?;
write_value(writer, value)?;

Ok(())
}
Expand All @@ -47,3 +47,31 @@ where
{
writer.write_all(&[SEPARATOR])
}

fn write_value<W>(writer: &mut W, value: &[u8]) -> io::Result<()>
where
W: Write,
{
if is_valid_value(value) {
writer.write_all(value)
} else {
Err(io::Error::new(io::ErrorKind::InvalidInput, "invalid value"))
}
}

fn is_valid_value(buf: &[u8]) -> bool {
// § 1.3 "The header section" (2023-05-24): `[ -~]+`.
!buf.is_empty() && buf.iter().all(|&b| matches!(b, b' '..=b'~'))
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_is_valid_value() {
assert!(is_valid_value(b"sq0"));
assert!(!is_valid_value(&[0x00]));
assert!(!is_valid_value(b"sq\t0"));
}
}

0 comments on commit c3f9878

Please sign in to comment.