Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/uu/od/src/input_offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ impl InputOffset {

/// Increase `byte_pos` and `label` if a label is used.
pub fn increase_position(&mut self, n: u64) {
self.byte_pos += n;
// Byte offset and label are fixed-width addresses, so they wrap around
// like GNU od instead of aborting on overflow (a `--traditional` label
// near u64::MAX overflowed the unchecked add). See #13225.
self.byte_pos = self.byte_pos.wrapping_add(n);
if let Some(l) = self.label {
self.label = Some(l + n);
self.label = Some(l.wrapping_add(n));
}
}

Expand Down Expand Up @@ -95,6 +98,14 @@ fn test_input_offset() {
assert_eq!("0000036", &sut.format_byte_offset());
}

#[test]
fn test_increase_position_wraps_instead_of_overflowing() {
// A --traditional label near u64::MAX must wrap, not abort (#13225).
let mut sut = InputOffset::new(Radix::Hexadecimal, u64::MAX, Some(u64::MAX));
sut.increase_position(16);
assert_eq!("00000f (00000f)", &sut.format_byte_offset());
}

#[test]
fn test_input_offset_with_label() {
let mut sut = InputOffset::new(Radix::Hexadecimal, 10, Some(20));
Expand Down
Loading