Skip to content

Commit

Permalink
vcf/io/writer/record/info/field/value/integer: Validate value
Browse files Browse the repository at this point in the history
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".
  • Loading branch information
zaeleus committed Sep 24, 2024
1 parent 91f9ede commit ff39c54
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
9 changes: 9 additions & 0 deletions noodles-vcf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## Unreleased

### Changed

* vcf/io/writer/record/info/field/value/integer: Validate 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".

## 0.65.0 - 2024-09-12

### Changed
Expand Down
24 changes: 22 additions & 2 deletions noodles-vcf/src/io/writer/record/info/field/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 ff39c54

Please sign in to comment.