Skip to content

Commit 4fa9499

Browse files
Copilotdoublegate
andcommitted
fix: improve Duration conversion safety and documentation
- Add clamping to u64::MAX to prevent overflow - Document that u64 can represent up to ~584 years - Add safety comment for Duration::from_nanos - Address code review feedback on potential data loss Co-authored-by: doublegate <6858123+doublegate@users.noreply.github.com>
1 parent 6509f41 commit 4fa9499

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

crates/prtip-core/src/types.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,9 @@ impl From<&ScanResult> for ScanResultRkyv {
649649
};
650650

651651
// Convert response time to u64 nanoseconds (avoid truncation issues)
652-
let response_time_nanos = result.response_time.as_nanos() as u64;
652+
// Note: u64 can represent up to ~584 years, which is more than sufficient
653+
// for network response times. We clamp to u64::MAX to avoid overflow.
654+
let response_time_nanos = result.response_time.as_nanos().min(u64::MAX as u128) as u64;
653655

654656
// Convert timestamp with proper error handling
655657
let timestamp_nanos = result
@@ -692,6 +694,7 @@ impl From<ScanResultRkyv> for ScanResult {
692694
};
693695

694696
// Convert u64 nanoseconds back to Duration
697+
// Safe: u64::MAX nanoseconds fits within Duration's range
695698
let response_time = Duration::from_nanos(rkyv.response_time_nanos);
696699

697700
// Convert i64 nanoseconds back to DateTime

0 commit comments

Comments
 (0)