Skip to content
Prev Previous commit
Next Next commit
Use checked conversion for float nanoseconds
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
  • Loading branch information
Copilot and youknowone committed Dec 25, 2025
commit 56acf0ac7cf0897ddc0e2f0b2bea6ee84af702a7
7 changes: 4 additions & 3 deletions crates/vm/src/convert/try_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,13 @@ fn duration_from_f64_floor(f: f64, vm: &VirtualMachine) -> PyResult<std::time::D
}

let total_nanos = (f * NANOS_PER_SEC).floor();
if total_nanos > MAX_TOTAL_NANOS as f64 {
let total_nanos_u128 = total_nanos
.to_u128()
.ok_or_else(|| vm.new_value_error("value out of range"))?;
if total_nanos_u128 > MAX_TOTAL_NANOS {
return Err(vm.new_value_error("value out of range"));
}

// safe: total_nanos is finite, non-negative, and below MAX_TOTAL_NANOS
let total_nanos_u128 = total_nanos as u128;
let secs = (total_nanos_u128 / NANOS_PER_SEC_U128) as u64;
let nanos = (total_nanos_u128 % NANOS_PER_SEC_U128) as u32;

Expand Down