diff --git a/src/uu/od/src/input_offset.rs b/src/uu/od/src/input_offset.rs index fa8e66c1a05..16bce18e631 100644 --- a/src/uu/od/src/input_offset.rs +++ b/src/uu/od/src/input_offset.rs @@ -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)); } } @@ -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)); +} + #[test] fn test_input_offset_with_label() { let mut sut = InputOffset::new(Radix::Hexadecimal, 10, Some(20));