Clean up numeric overflow using u128 for product of u64 and 1_000 - #1113
Clean up numeric overflow using u128 for product of u64 and 1_000#1113worikgh wants to merge 1 commit into
Conversation
|
|
||
| fn micros_to_stream_instant(micros: u64) -> crate::StreamInstant { | ||
| let nanos = micros * 1000; | ||
| let nanos = micros as u128 * 1000; |
There was a problem hiding this comment.
Good catch. This can be even simpler:
fn micros_to_stream_instant(micros: u64) -> crate::StreamInstant {
crate::StreamInstant::from_nanos_i128(micros as i128 * 1_000)
.expect("`micros` out of range of `StreamInstant` representation")
}And it seems the same issue is present in the ASIO and CoreAudio hosts:
src/host/coreaudio/mod.rs:
- let nanos = m_host_time * info.numer as u64 / info.denom as u64;
- let secs = nanos / 1_000_000_000;
- let subsec_nanos = nanos - secs * 1_000_000_000;
- Ok(crate::StreamInstant::new(secs as i64, subsec_nanos as u32))
+ let nanos = m_host_time as u128 * info.numer as u128 / info.denom as u128;
+ crate::StreamInstant::from_nanos_i128(nanos as i128).ok_or(BackendSpecificError {
+ description: "host time out of range of `StreamInstant` representation".to_string(),
+ })src/host/asio/stream.rs:
- let systime_ns = asio_ns_to_double(system_time);
- let secs = systime_ns as i64 / 1_000_000_000;
- let nanos = (systime_ns as i64 - secs * 1_000_000_000) as u32;
- crate::StreamInstant::new(secs, nanos)
+ let nanos = (system_time.hi as u64) << 32 | system_time.lo as u64;
+ crate::StreamInstant::from_nanos_i128(nanos as i128)
+ .expect("`system_time` out of range of `StreamInstant` representation")Would you be so kind to update those too and add appropriate changelog entries?
There was a problem hiding this comment.
Would you be so kind to update those too and add appropriate changelog entries?
Forgive me: Are you asking me to update my pull request? I will. I am inexperienced at doing so, I will do my best.
Will not do any harm if you were not addressing me - you can ignore it.
Soon....
There was a problem hiding this comment.
Indeed if you would want to? No worries about the experience, good that you call that out. Let me know where I can help out.
I some cases I observed in
host/jack/stream.rsthe functionmicros_to_stream_instantthe parametermicroswhen multiplied by 1_000 overflowed u64