From 0cf39f1d9abc929f722272358355df2018473add Mon Sep 17 00:00:00 2001 From: Kanishk Sachan Date: Wed, 1 Jul 2026 14:06:42 +0100 Subject: [PATCH] od: fix overflow panic in --traditional label accumulator `InputOffset::increase_position` used plain `+=` / `+` for the running byte position and user-supplied label, which panics in debug builds (or any build with `-C overflow-checks=on`) when the label is near `u64::MAX`. C unsigned arithmetic wraps on overflow, and GNU od silently wraps in the same situation. Switch to `wrapping_add` for both fields to match GNU's behaviour and eliminate the debug-mode panic reported in #13225. Adds a unit test that exercises the wrap-around path directly. Fixes #13225 --- src/uu/od/src/input_offset.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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));