Skip to content

Fix out-of-bounds access possibility in safe code. #18

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

Merged
merged 2 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ego-tree"
version = "0.5.0"
version = "0.6.0"
description = "Vec-backed ID-tree"
keywords = ["tree", "vec", "id", "index"]
authors = ["Curtis McEnroe <[email protected]>"]
Expand Down
28 changes: 24 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ impl<T> Node<T> {
#[derive(Debug)]
pub struct NodeRef<'a, T: 'a> {
/// Node ID.
pub id: NodeId,
id: NodeId,

/// Tree containing the node.
pub tree: &'a Tree<T>,
tree: &'a Tree<T>,

node: &'a Node<T>,
}
Expand All @@ -88,10 +88,10 @@ pub struct NodeRef<'a, T: 'a> {
#[derive(Debug)]
pub struct NodeMut<'a, T: 'a> {
/// Node ID.
pub id: NodeId,
id: NodeId,

/// Tree containing the node.
pub tree: &'a mut Tree<T>,
tree: &'a mut Tree<T>,
}

// Trait implementations regardless of T.
Expand Down Expand Up @@ -171,6 +171,16 @@ impl<T> Tree<T> {
}

impl<'a, T: 'a> NodeRef<'a, T> {
/// Returns the ID of this node.
pub fn id(&self) -> NodeId {
self.id
}

/// Returns the tree owning this node.
pub fn tree(&self) -> &'a Tree<T> {
self.tree
}

/// Returns the value of this node.
pub fn value(&self) -> &'a T {
&self.node.value
Expand Down Expand Up @@ -213,6 +223,16 @@ impl<'a, T: 'a> NodeRef<'a, T> {
}

impl<'a, T: 'a> NodeMut<'a, T> {
/// Returns the ID of this node.
pub fn id(&self) -> NodeId {
self.id
}

/// Returns the tree owning this node.
pub fn tree(&mut self) -> &mut Tree<T> {
self.tree
}

fn node(&mut self) -> &mut Node<T> {
unsafe { self.tree.node_mut(self.id) }
}
Expand Down
4 changes: 2 additions & 2 deletions tests/node_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ fn reparent_from_id_append() {
'e' => { 'f', 'g' },
}
};
let e_id = tree.root().last_child().unwrap().id;
let e_id = tree.root().last_child().unwrap().id();
tree.root_mut().first_child().unwrap().reparent_from_id_append(e_id);

let b = tree.root().first_child().unwrap();
Expand All @@ -322,7 +322,7 @@ fn reparent_from_id_prepend() {
'e' => { 'c', 'd' },
}
};
let e_id = tree.root().last_child().unwrap().id;
let e_id = tree.root().last_child().unwrap().id();
tree.root_mut().first_child().unwrap().reparent_from_id_prepend(e_id);

let b = tree.root().first_child().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions tests/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ fn orphan() {
#[test]
fn get() {
let tree = Tree::new('a');
let id = tree.root().id;
let id = tree.root().id();
assert_eq!(Some(tree.root()), tree.get(id));
}

#[test]
fn get_mut() {
let mut tree = Tree::new('a');
let id = tree.root().id;
let id = tree.root().id();
assert_eq!(Some('a'), tree.get_mut(id).map(|mut n| *n.value()));
}

Expand Down