Skip to content

Commit

Permalink
vcf/io/writer/record/samples/sample/value/integer: Validate value
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Sep 24, 2024
1 parent 9eb0faa commit f5777ce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion noodles-vcf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Changed

* vcf/io/writer/record/info/field/value/integer: Validate value.
* vcf/io/writer/record: Validate integer value.

Integer values must now be > -2^31 + 7. See _The Variant Call Format
Specification: VCFv4.5 and BCFv2.2_ (2024-06-28) § 1.3 "Data types".
Expand Down
24 changes: 22 additions & 2 deletions noodles-vcf/src/io/writer/record/samples/sample/value/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@ pub(super) fn write_integer<W>(writer: &mut W, n: i32) -> io::Result<()>
where
W: Write,
{
write!(writer, "{n}")
if is_valid(n) {
write!(writer, "{n}")
} else {
Err(io::Error::new(
io::ErrorKind::InvalidInput,
"invalid integer",
))
}
}

// § 1.3 "Data types" (2024-06-28): "For the Integer type, the values from -2^31 to -2^31 + 7
// cannot be stored in the binary version and therefore are disallowed in both VCF and BCF..."
fn is_valid(n: i32) -> bool {
const MIN: i32 = i32::MIN + 7;
n > MIN
}

#[cfg(test)]
Expand All @@ -22,10 +36,16 @@ mod tests {

let mut buf = Vec::new();

t(&mut buf, i32::MIN, b"-2147483648")?;
t(&mut buf, i32::MIN + 8, b"-2147483640")?;
t(&mut buf, 0, b"0")?;
t(&mut buf, i32::MAX, b"2147483647")?;

buf.clear();
assert!(matches!(
write_integer(&mut buf, i32::MIN),
Err(e) if e.kind() == io::ErrorKind::InvalidInput
));

Ok(())
}
}

0 comments on commit f5777ce

Please sign in to comment.