Skip to content

Commit f15595b

Browse files
authored
Require the index to be valid in NodeIndexable::from_index (#443)
* Require the index to be valid in NodeIndexable (and EdgeIndexable) * Add bounds-check assertion for GraphMap
1 parent d1168b0 commit f15595b

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

‎src/graphmap.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,11 @@ where
10291029
i
10301030
}
10311031
fn from_index(&self, ix: usize) -> Self::NodeId {
1032+
assert!(
1033+
ix < self.nodes.len(),
1034+
"The requested index {} is out-of-bounds.",
1035+
ix
1036+
);
10321037
let (&key, _) = self.nodes.get_index(ix).unwrap();
10331038
key
10341039
}
@@ -1056,6 +1061,11 @@ where
10561061
}
10571062

10581063
fn from_index(&self, ix: usize) -> Self::EdgeId {
1064+
assert!(
1065+
ix < self.edges.len(),
1066+
"The requested index {} is out-of-bounds.",
1067+
ix
1068+
);
10591069
let (&key, _) = self.edges.get_index(ix).unwrap();
10601070
key
10611071
}

‎src/visit/mod.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ trait_template! {
508508
fn node_bound(self: &Self) -> usize;
509509
/// Convert `a` to an integer index.
510510
fn to_index(self: &Self, a: Self::NodeId) -> usize;
511-
/// Convert `i` to a node index
511+
/// Convert `i` to a node index. `i` must be a valid value in the graph.
512512
fn from_index(self: &Self, i: usize) -> Self::NodeId;
513513
}
514514
}
@@ -524,7 +524,7 @@ trait_template! {
524524
fn edge_bound(self: &Self) -> usize;
525525
/// Convert `a` to an integer index.
526526
fn to_index(self: &Self, a: Self::EdgeId) -> usize;
527-
/// Convert `i` to a node index
527+
/// Convert `i` to an edge index. `i` must be a valid value in the graph.
528528
fn from_index(self: &Self, i: usize) -> Self::EdgeId;
529529
}
530530
}

0 commit comments

Comments
 (0)