Skip to content

Commit

Permalink
fix(sentry-types): Switch to checked version of from_secs_f64 (#554)
Browse files Browse the repository at this point in the history
This change make use of `try_from_secs_f64` (the checked version of `from_secs_f64`),
avoiding panics if the incoming seconds is negative, overflows Duration or not finite.
  • Loading branch information
olksdr authored Feb 24, 2023
1 parent 569b0d9 commit 5294125
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion sentry-types/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn datetime_to_timestamp(st: &SystemTime) -> f64 {
}

pub fn timestamp_to_datetime(ts: f64) -> Option<SystemTime> {
let duration = Duration::from_secs_f64(ts);
let duration = Duration::try_from_secs_f64(ts).ok()?;
SystemTime::UNIX_EPOCH.checked_add(duration)
}

Expand Down Expand Up @@ -188,3 +188,16 @@ pub mod ts_rfc3339_opt {
}
}
}

#[cfg(test)]
mod tests {
use super::timestamp_to_datetime;

#[test]
fn test_timestamp_to_datetime() {
assert!(timestamp_to_datetime(-10000.0).is_none());
assert!(timestamp_to_datetime(f64::INFINITY).is_none());
assert!(timestamp_to_datetime(f64::MAX).is_none());
assert!(timestamp_to_datetime(123123123.0).is_some());
}
}

0 comments on commit 5294125

Please sign in to comment.