Skip to content

Commit

Permalink
lib: Add delta calculation with wrapping operations to avoid overflows
Browse files Browse the repository at this point in the history
  • Loading branch information
vpetrigo committed Nov 9, 2022
1 parent 58fa630 commit e140d06
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sntpc"
version = "0.3.1"
version = "0.3.2"
description = "Library for making SNTP requests"
homepage = "https://github.com/vpetrigo/sntpc"
repository = "https://github.com/vpetrigo/sntpc"
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ fn roundtrip_calculate(
t4: u64,
units: Units,
) -> u64 {
let delta = (t4 - t1) - (t3 - t2);
let delta = t4.wrapping_sub(t1).wrapping_sub(t3.wrapping_sub(t2));
let delta_sec = (delta & SECONDS_MASK) >> 32;
let delta_sec_fraction = delta & SECONDS_FRAC_MASK;

Expand All @@ -1018,7 +1018,9 @@ 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) as i64)
.wrapping_add(t3.wrapping_sub(t4) as i64)
/ 2;
let theta_sec = (theta.abs() as u64 & SECONDS_MASK) >> 32;
let theta_sec_fraction = theta.abs() as u64 & SECONDS_FRAC_MASK;

Expand Down

0 comments on commit e140d06

Please sign in to comment.