From e84de9b97f7ee4032837e3341aaf4cd04eec6f41 Mon Sep 17 00:00:00 2001 From: Nicolas Boichat Date: Mon, 21 Apr 2025 11:31:08 +0200 Subject: [PATCH] uucore: fast_inc: Add a debug_assert for developer convenience Suggested by our AI overlords. --- src/uucore/src/lib/features/fast_inc.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/uucore/src/lib/features/fast_inc.rs b/src/uucore/src/lib/features/fast_inc.rs index 1230cd2de..165cf273f 100644 --- a/src/uucore/src/lib/features/fast_inc.rs +++ b/src/uucore/src/lib/features/fast_inc.rs @@ -35,6 +35,11 @@ pub fn fast_inc(val: &mut [u8], start: &mut usize, end: usize, inc: &[u8]) { // First loop, add all digits of inc into val. for inc_pos in (0..inc.len()).rev() { + // The decrement operation would also panic in debug mode, print a message for developer convenience. + debug_assert!( + pos > 0, + "Buffer overflowed, make sure you allocate val with enough headroom." + ); pos -= 1; let mut new_val = inc[inc_pos] + carry; @@ -99,6 +104,11 @@ pub fn fast_inc_one(val: &mut [u8], start: &mut usize, end: usize) { } } + // The following decrement operation would also panic in debug mode, print a message for developer convenience. + debug_assert!( + *start > 0, + "Buffer overflowed, make sure you allocate val with enough headroom." + ); // The carry propagated so far that a new digit was added. val[*start - 1] = b'1'; *start -= 1;