Skip to content

Commit

Permalink
Fix overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian authored Jul 22, 2024
1 parent b6fc4af commit d26daeb
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down

0 comments on commit d26daeb

Please sign in to comment.