Skip to content

Commit

Permalink
Avoid undefined behaviour around f64/u64 casts (#10374)
Browse files Browse the repository at this point in the history
Rust 1.45 will define float-to-int casts in a way which breaks the
current code. Instead, avoid round-tripping f64::INFINITY ->
f64::NEGATIVE_INFINITY -> u64 and just filter out f64::INFINITY earlier.
  • Loading branch information
illicitonion committed Jul 16, 2020
1 parent 145ad80 commit d9cf068
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/rust/engine/graph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,16 @@ impl<N: Node> InnerGraph<N> {
let (weights, paths) =
petgraph::algo::bellman_ford(&graph, src).expect("The graph must be acyclic");
if let Some((index, total_duration)) = weights
.iter()
// INFINITY is used for missing entries. We don't want for this to interfere with our max_by.
// Use NEG_INFINITY instead, which has to be the minimum duration.
.map(|weight| {
if *weight == std::f64::INFINITY {
std::f64::NEG_INFINITY
.into_iter()
.enumerate()
.filter_map(|(i, weight)| {
// INFINITY is used for missing entries.
if weight == std::f64::INFINITY {
None
} else {
*weight
Some((i, Duration::from_nanos(-weight as u64)))
}
})
.map(|weight| Duration::from_nanos(-weight as u64))
.enumerate()
.max_by(|(_, left_duration), (_, right_duration)| left_duration.cmp(&right_duration))
{
let critical_path = {
Expand Down

0 comments on commit d9cf068

Please sign in to comment.