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
12 changes: 10 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,9 @@ impl InputOffset {

/// Increase `byte_pos` and `label` if a label is used.
pub fn increase_position(&mut self, n: u64) {
self.byte_pos += n;
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 +95,14 @@ fn test_input_offset() {
assert_eq!("0000036", &sut.format_byte_offset());
}

#[test]
fn test_increase_position_wraps_on_overflow() {
// A label of u64::MAX must not panic — it wraps, matching C unsigned semantics.
let mut sut = InputOffset::new(Radix::Hexadecimal, 0, Some(u64::MAX));
sut.increase_position(1); // would panic in debug builds before the fix
assert_eq!(sut.label, Some(0));
}
Comment on lines +100 to +104

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