Skip to content

Commit aba138c

Browse files
committed
Fix out-of-bounds access possibility in safe code.
With the `id` and `tree` fields of `NodeRef` and `NodeMut` being public, it was possible to assign to them. For example, it was possible to build a `NodeMut` for large ID/index in a small tree/Vec. Since some APIs use unchecked indexing, this would let users of this library cause out-of-bounds access in a `Vec` without writing `unsafe` code themselves. This commit fixes that issue by making the fields private and instead providing read-only access via accessor methods. Now the fields can only be set by the `ego-tree` crate, which can make sure to only ever use an ID that is in-bounds for a given tree.
1 parent 3efaff6 commit aba138c

File tree

2 files changed

+5
-14
lines changed

2 files changed

+5
-14
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.1"
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: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,10 @@ impl<T> Node<T> {
7676
#[derive(Debug)]
7777
pub struct NodeRef<'a, T: 'a> {
7878
/// Node ID.
79-
#[deprecated(since = "0.5.1", note = "use the id() method instead and upgrade to 0.6")]
80-
pub id: NodeId,
79+
id: NodeId,
8180

8281
/// Tree containing the node.
83-
#[deprecated(since = "0.5.1", note = "use the tree() method instead and upgrade to 0.6")]
84-
pub tree: &'a Tree<T>,
82+
tree: &'a Tree<T>,
8583

8684
node: &'a Node<T>,
8785
}
@@ -90,12 +88,10 @@ pub struct NodeRef<'a, T: 'a> {
9088
#[derive(Debug)]
9189
pub struct NodeMut<'a, T: 'a> {
9290
/// Node ID.
93-
#[deprecated(since = "0.5.1", note = "use the id() method instead and upgrade to 0.6")]
94-
pub id: NodeId,
91+
id: NodeId,
9592

9693
/// Tree containing the node.
97-
#[deprecated(since = "0.5.1", note = "use the tree() method instead and upgrade to 0.6")]
98-
pub tree: &'a mut Tree<T>,
94+
tree: &'a mut Tree<T>,
9995
}
10096

10197
// Trait implementations regardless of T.
@@ -107,15 +103,13 @@ impl<'a, T: 'a> Clone for NodeRef<'a, T> {
107103

108104
impl<'a, T: 'a> Eq for NodeRef<'a, T> { }
109105
impl<'a, T: 'a> PartialEq for NodeRef<'a, T> {
110-
#[allow(deprecated)]
111106
fn eq(&self, other: &Self) -> bool {
112107
self.id == other.id
113108
&& self.tree as *const _ == other.tree as *const _
114109
&& self.node as *const _ == other.node as *const _
115110
}
116111
}
117112

118-
#[allow(deprecated)]
119113
impl<T> Tree<T> {
120114
/// Creates a tree with a root node.
121115
pub fn new(root: T) -> Self {
@@ -176,7 +170,6 @@ impl<T> Tree<T> {
176170
}
177171
}
178172

179-
#[allow(deprecated)]
180173
impl<'a, T: 'a> NodeRef<'a, T> {
181174
/// Returns the ID of this node.
182175
pub fn id(&self) -> NodeId {
@@ -229,7 +222,6 @@ impl<'a, T: 'a> NodeRef<'a, T> {
229222
}
230223
}
231224

232-
#[allow(deprecated)]
233225
impl<'a, T: 'a> NodeMut<'a, T> {
234226
/// Returns the ID of this node.
235227
pub fn id(&self) -> NodeId {
@@ -548,7 +540,6 @@ impl<'a, T: 'a> NodeMut<'a, T> {
548540
}
549541
}
550542

551-
#[allow(deprecated)]
552543
impl<'a, T: 'a> From<NodeMut<'a, T>> for NodeRef<'a, T> {
553544
fn from(node: NodeMut<'a, T>) -> Self {
554545
unsafe { node.tree.get_unchecked(node.id) }

0 commit comments

Comments
 (0)