-
-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix overflow in calculate_offset
#25
Conversation
@robertbastian thank you for the PR! Would you please tell if you can take a look for formatting issues reported? |
I fixed the formatting issue, but cannot reproduce the test failure locally 🤷 |
Hm, I am able to do that. There is the test log:
And here are the patch that prevent that behavior: Subject: [PATCH] sntpc: Update library crate
---
Index: src/lib.rs
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs (revision 609f8059d340325ee052c05f8b40c03ea15e6873)
+++ b/src/lib.rs (date 1723294272616)
@@ -619,8 +619,8 @@
}
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)
+ .wrapping_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; |
A wrapping add would avoid the panic, but I don't think it produces a correct value. It should either be a checked add, a saturating add, or use |
I am not sure extending the result to
Let's switch to |
Works for me, thanks! |
If client and server clock are very far out of sync (i.e. clients just booted and has clock 0), the
theta
calculation would overflow in thewrapping_add
step, which is not correct. Pushing the/ 2
onto the subterms avoids the overflow.