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

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

let secs = (total_nanos_u128 / NANOS_PER_SEC_U128) as u64;
let nanos = (total_nanos_u128 % NANOS_PER_SEC_U128) as u32;
Expand Down
Loading