Skip to content

Commit e164eec

Browse files
authored
Merge pull request #18 from SimonSapin/out-of-bounds
Fix out-of-bounds access possibility in safe code.
2 parents 3f6b678 + aba138c commit e164eec

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ego-tree"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
description = "Vec-backed ID-tree"
55
keywords = ["tree", "vec", "id", "index"]
66
authors = ["Curtis McEnroe <[email protected]>"]

src/lib.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ impl<T> Node<T> {
7676
#[derive(Debug)]
7777
pub struct NodeRef<'a, T: 'a> {
7878
/// Node ID.
79-
pub id: NodeId,
79+
id: NodeId,
8080

8181
/// Tree containing the node.
82-
pub tree: &'a Tree<T>,
82+
tree: &'a Tree<T>,
8383

8484
node: &'a Node<T>,
8585
}
@@ -88,10 +88,10 @@ pub struct NodeRef<'a, T: 'a> {
8888
#[derive(Debug)]
8989
pub struct NodeMut<'a, T: 'a> {
9090
/// Node ID.
91-
pub id: NodeId,
91+
id: NodeId,
9292

9393
/// Tree containing the node.
94-
pub tree: &'a mut Tree<T>,
94+
tree: &'a mut Tree<T>,
9595
}
9696

9797
// Trait implementations regardless of T.
@@ -171,6 +171,16 @@ impl<T> Tree<T> {
171171
}
172172

173173
impl<'a, T: 'a> NodeRef<'a, T> {
174+
/// Returns the ID of this node.
175+
pub fn id(&self) -> NodeId {
176+
self.id
177+
}
178+
179+
/// Returns the tree owning this node.
180+
pub fn tree(&self) -> &'a Tree<T> {
181+
self.tree
182+
}
183+
174184
/// Returns the value of this node.
175185
pub fn value(&self) -> &'a T {
176186
&self.node.value
@@ -213,6 +223,16 @@ impl<'a, T: 'a> NodeRef<'a, T> {
213223
}
214224

215225
impl<'a, T: 'a> NodeMut<'a, T> {
226+
/// Returns the ID of this node.
227+
pub fn id(&self) -> NodeId {
228+
self.id
229+
}
230+
231+
/// Returns the tree owning this node.
232+
pub fn tree(&mut self) -> &mut Tree<T> {
233+
self.tree
234+
}
235+
216236
fn node(&mut self) -> &mut Node<T> {
217237
unsafe { self.tree.node_mut(self.id) }
218238
}

tests/node_mut.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ fn reparent_from_id_append() {
298298
'e' => { 'f', 'g' },
299299
}
300300
};
301-
let e_id = tree.root().last_child().unwrap().id;
301+
let e_id = tree.root().last_child().unwrap().id();
302302
tree.root_mut().first_child().unwrap().reparent_from_id_append(e_id);
303303

304304
let b = tree.root().first_child().unwrap();
@@ -322,7 +322,7 @@ fn reparent_from_id_prepend() {
322322
'e' => { 'c', 'd' },
323323
}
324324
};
325-
let e_id = tree.root().last_child().unwrap().id;
325+
let e_id = tree.root().last_child().unwrap().id();
326326
tree.root_mut().first_child().unwrap().reparent_from_id_prepend(e_id);
327327

328328
let b = tree.root().first_child().unwrap();

tests/tree.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ fn orphan() {
3737
#[test]
3838
fn get() {
3939
let tree = Tree::new('a');
40-
let id = tree.root().id;
40+
let id = tree.root().id();
4141
assert_eq!(Some(tree.root()), tree.get(id));
4242
}
4343

4444
#[test]
4545
fn get_mut() {
4646
let mut tree = Tree::new('a');
47-
let id = tree.root().id;
47+
let id = tree.root().id();
4848
assert_eq!(Some('a'), tree.get_mut(id).map(|mut n| *n.value()));
4949
}
5050

0 commit comments

Comments
 (0)