Skip to content

Commit

Permalink
bcf/record/codec/decoder/position: Fix reading position at max position
Browse files Browse the repository at this point in the history
This would previously overflow and error instead of returning a properly
normalized `Position`.
  • Loading branch information
zaeleus committed Aug 16, 2024
1 parent 3547177 commit 8d1ca90
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 9 additions & 0 deletions noodles-bcf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## Unreleased

### Fixed

* bcf/record/codec/decoder/position: Fix reading position at max position.

This would previously overflow and error instead of returning a properly
normalized `Position`.

## 0.59.0 - 2024-08-04

### Changed
Expand Down
18 changes: 14 additions & 4 deletions noodles-bcf/src/record/codec/decoder/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ pub fn read_pos(src: &mut &[u8]) -> io::Result<Option<Position>> {

match src.read_i32::<LittleEndian>()? {
TELOMERE_START => Ok(None),
n => usize::try_from(n + 1)
.and_then(Position::try_from)
.map(Some)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)),
n => usize::try_from(n)
.map(|m| m + 1)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
.map(Position::new),
}
}

Expand All @@ -38,4 +38,14 @@ mod tests {

Ok(())
}

#[cfg(not(target_pointer_width = "16"))]
#[test]
fn test_read_pos_with_max_position() -> Result<(), Box<dyn std::error::Error>> {
let data = i32::MAX.to_le_bytes();
let mut src = &data[..];
let expected = Position::try_from(1 << 31)?;
assert_eq!(read_pos(&mut src)?, Some(expected));
Ok(())
}
}

0 comments on commit 8d1ca90

Please sign in to comment.