From d26daeb6723b5e43c1c8fb48f836d18e5317f42e Mon Sep 17 00:00:00 2001 From: Robert Bastian <4706271+robertbastian@users.noreply.github.com> Date: Tue, 23 Jul 2024 00:58:22 +0200 Subject: [PATCH 1/3] Fix overflow --- src/lib.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 57e9390..512f0ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -619,9 +619,7 @@ fn roundtrip_calculate( } fn offset_calculate(t1: u64, t2: u64, t3: u64, t4: u64, units: Units) -> i64 { - let theta = (t2.wrapping_sub(t1) as i64) - .wrapping_add(t3.wrapping_sub(t4) as i64) - / 2; + let theta = (t2.wrapping_sub(t1) / 2) as i64 + (t3.wrapping_sub(t4) / 2) as i64; let theta_sec = (theta.unsigned_abs() & SECONDS_MASK) >> 32; let theta_sec_fraction = theta.unsigned_abs() & SECONDS_FRAC_MASK; @@ -639,6 +637,19 @@ fn offset_calculate(t1: u64, t2: u64, t3: u64, t4: u64, units: Units) -> i64 { } } +#[test] +fn test_offset_calculate() { + let t1 = 9487534663484046772u64; + let t2 = 16882120099581835046u64; + let t3 = 16882120099583884144u64; + let t4 = 9487534663651464597u64; + + assert_eq!( + offset_calculate(t1, t2, t3, t4, Units::Microseconds), + 1721686086620926 + ); +} + #[cfg(feature = "log")] fn debug_ntp_packet(packet: &NtpPacket, _recv_timestamp: u64) { let mode = shifter(packet.li_vn_mode, MODE_MASK, MODE_SHIFT); From 609f8059d340325ee052c05f8b40c03ea15e6873 Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Fri, 9 Aug 2024 16:20:54 +0200 Subject: [PATCH 2/3] fmt --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 512f0ef..7760779 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -619,7 +619,8 @@ fn roundtrip_calculate( } fn offset_calculate(t1: u64, t2: u64, t3: u64, t4: u64, units: Units) -> i64 { - let theta = (t2.wrapping_sub(t1) / 2) as i64 + (t3.wrapping_sub(t4) / 2) as i64; + let theta = + (t2.wrapping_sub(t1) / 2) as i64 + (t3.wrapping_sub(t4) / 2) as i64; let theta_sec = (theta.unsigned_abs() & SECONDS_MASK) >> 32; let theta_sec_fraction = theta.unsigned_abs() & SECONDS_FRAC_MASK; From b6f4a475ead1d1f789c712cb2f360c87074f7aba Mon Sep 17 00:00:00 2001 From: Vladimir Petrigo Date: Sat, 17 Aug 2024 14:19:11 +0200 Subject: [PATCH 3/3] lib.rs: Switch from wrapping to saturating add --- src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7760779..a262906 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,9 +84,9 @@ //! socket //! .set_read_timeout(Some(Duration::from_secs(2))) //! .expect("Unable to set UDP socket read timeout"); -//! # #[cfg(all(feature = "std", feature = "sup"))] +//! # #[cfg(all(feature = "std"))] //! let result = sntpc::simple_get_time("time.google.com:123", socket); -//! # #[cfg(all(feature = "std", feature = "sup"))] +//! # #[cfg(all(feature = "std"))] //! match result { //! Ok(time) => { //! println!("Got time: {}.{}", time.sec(), sntpc::fraction_to_milliseconds(time.sec_fraction())); @@ -604,7 +604,7 @@ fn roundtrip_calculate( t4: u64, units: Units, ) -> u64 { - let delta = t4.wrapping_sub(t1).wrapping_sub(t3.wrapping_sub(t2)); + let delta = t4.wrapping_sub(t1).saturating_sub(t3.wrapping_sub(t2)); let delta_sec = (delta & SECONDS_MASK) >> 32; let delta_sec_fraction = delta & SECONDS_FRAC_MASK; @@ -619,8 +619,8 @@ fn roundtrip_calculate( } fn offset_calculate(t1: u64, t2: u64, t3: u64, t4: u64, units: Units) -> i64 { - let theta = - (t2.wrapping_sub(t1) / 2) as i64 + (t3.wrapping_sub(t4) / 2) as i64; + let theta = ((t2.wrapping_sub(t1) / 2) as i64) + .saturating_add((t3.wrapping_sub(t4) / 2) as i64); let theta_sec = (theta.unsigned_abs() & SECONDS_MASK) >> 32; let theta_sec_fraction = theta.unsigned_abs() & SECONDS_FRAC_MASK;