From 36deaed0975bcca3a8ed4c88733caef91fb3c673 Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Sun, 22 Dec 2024 21:49:37 +0000 Subject: [PATCH] Fix a bug in `abs_diff` These implementations of `abs_diff` were added in c2ff1b3119 ("Completely overhaul fuzz testing"), but the signed implementation is wrong when |x| + |y| exceeds the integer's limits (e.g. `(-128).abs_diff(1)` should be 128 but currently these return 127. Resolve this by just using `std`'s implementation since that is stable now. This isn't used anywhere critical, we probably just weren't hitting the edge case. --- src/int/mod.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/int/mod.rs b/src/int/mod.rs index 0d3b0ce40..c0d5a6715 100644 --- a/src/int/mod.rs +++ b/src/int/mod.rs @@ -240,11 +240,7 @@ macro_rules! int_impl { } fn abs_diff(self, other: Self) -> Self { - if self < other { - other.wrapping_sub(self) - } else { - self.wrapping_sub(other) - } + self.abs_diff(other) } int_impl_common!($uty); @@ -277,7 +273,7 @@ macro_rules! int_impl { } fn abs_diff(self, other: Self) -> $uty { - self.wrapping_sub(other).wrapping_abs() as $uty + self.abs_diff(other) } int_impl_common!($ity);