Skip to content
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 freeze/crash when logging large times #2588

Merged
merged 5 commits into from
Jul 4, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions crates/re_log_types/src/time_real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ use crate::TimeInt;
pub struct TimeReal(FixedI128<typenum::U64>);

impl TimeReal {
pub const MIN: Self = Self(FixedI128::MIN);
pub const MAX: Self = Self(FixedI128::MAX);

#[inline]
pub fn floor(&self) -> TimeInt {
let int: i64 = self.0.saturating_floor().lossy_into();
Expand Down Expand Up @@ -64,16 +67,38 @@ impl From<i64> for TimeReal {
}

impl From<f32> for TimeReal {
/// Saturating cast
#[inline]
fn from(value: f32) -> Self {
Self(FixedI128::from_num(value))
assert!(!value.is_nan());
emilk marked this conversation as resolved.
Show resolved Hide resolved
if let Some(num) = FixedI128::checked_from_num(value) {
Self(num)
} else {
// underflow or overflow
if value < 0.0 {
Self::MIN
} else {
Self::MAX
}
}
}
}

impl From<f64> for TimeReal {
/// Saturating cast
#[inline]
fn from(value: f64) -> Self {
Self(FixedI128::from_num(value))
assert!(!value.is_nan());
if let Some(num) = FixedI128::checked_from_num(value) {
Self(num)
} else {
// unde
if value < 0.0 {
Self::MIN
} else {
Self::MAX
}
}
}
}

Expand Down Expand Up @@ -146,7 +171,7 @@ impl std::ops::Mul<f64> for TimeReal {

#[inline]
fn mul(self, rhs: f64) -> Self::Output {
Self(self.0.saturating_mul(FixedI128::from_num(rhs)))
Self(self.0.saturating_mul(Self::from(rhs).0))
}
}

Expand Down Expand Up @@ -259,4 +284,9 @@ fn test_time_value_f() {
assert_eq!(T::from(f) - T::from(g), T::from(f - g));
}
}

assert_eq!(TimeReal::from(f32::NEG_INFINITY), TimeReal::MIN);
assert_eq!(TimeReal::from(f32::MIN), TimeReal::MIN);
assert_eq!(TimeReal::from(f32::INFINITY), TimeReal::MAX);
assert_eq!(TimeReal::from(f32::MAX), TimeReal::MAX);
}