Skip to content

Commit c538147

Browse files
author
bors-servo
authored
Auto merge of #384 - servo:dropfix, r=Manishearth
Ensure all RcDom nodes drop children iteratively This is a better solution for the problem solved by 8bf65ef which doesn't require destroying all nodes when the RcDom is dropped. Fixes #383.
2 parents dd6210a + 10c0e85 commit c538147

File tree

4 files changed

+9
-43
lines changed

4 files changed

+9
-43
lines changed

html5ever/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "html5ever"
4-
version = "0.23.0"
4+
version = "0.24.0"
55
authors = [ "The html5ever Project Developers" ]
66
license = "MIT / Apache-2.0"
77
repository = "https://github.com/servo/html5ever"
@@ -29,7 +29,7 @@ name = "serializer"
2929
[dependencies]
3030
log = "0.4"
3131
mac = "0.1"
32-
markup5ever = { version = "0.8", path = "../markup5ever" }
32+
markup5ever = { version = "0.9", path = "../markup5ever" }
3333

3434
[dev-dependencies]
3535
serde_json = "1.0"

markup5ever/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "markup5ever"
3-
version = "0.8.1"
3+
version = "0.9.0"
44
authors = [ "The html5ever Project Developers" ]
55
license = "MIT / Apache-2.0"
66
repository = "https://github.com/servo/html5ever"

markup5ever/rcdom.rs

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ pub struct Node {
109109
pub children: RefCell<Vec<Handle>>,
110110
/// Represents this node's data.
111111
pub data: NodeData,
112-
/// Flag to control whether to free any children on destruction.
113-
leak_children_on_drop: Cell<bool>,
114112
}
115113

116114
impl Node {
@@ -120,36 +118,16 @@ impl Node {
120118
data: data,
121119
parent: Cell::new(None),
122120
children: RefCell::new(Vec::new()),
123-
leak_children_on_drop: Cell::new(true),
124121
})
125122
}
126-
127-
/// Drop any child nodes remaining in this node at destruction.
128-
///
129-
/// RcDom's destructor automatically drops any nodes and children that are
130-
/// present in the document. This setting only affects nodes that are dropped
131-
/// by manipulating the tree before RcDom's destructor runs (such as manually
132-
/// removing children from a node after parsing is complete).
133-
///
134-
/// Unsafety: due to the representation of children, this can trigger
135-
/// stack overflow if dropping a node with a very deep tree of children.
136-
/// This is not a recommended configuration to use when interacting with
137-
/// arbitrary HTML content.
138-
pub unsafe fn free_child_nodes_on_drop(&self) {
139-
self.leak_children_on_drop.set(false);
140-
}
141123
}
142124

143125
impl Drop for Node {
144126
fn drop(&mut self) {
145-
if !self.children.borrow().is_empty() {
146-
if self.leak_children_on_drop.get() {
147-
warn!("Dropping node with children outside of RcDom's destructor. \
148-
Leaking memory for {} children.", self.children.borrow().len());
149-
for child in mem::replace(&mut *self.children.borrow_mut(), vec![]) {
150-
mem::forget(child);
151-
}
152-
}
127+
let mut nodes = mem::replace(&mut *self.children.borrow_mut(), vec![]);
128+
while let Some(node) = nodes.pop() {
129+
let children = mem::replace(&mut *node.children.borrow_mut(), vec![]);
130+
nodes.extend(children.into_iter());
153131
}
154132
}
155133
}
@@ -227,18 +205,6 @@ pub struct RcDom {
227205
pub quirks_mode: QuirksMode,
228206
}
229207

230-
impl Drop for RcDom {
231-
fn drop(&mut self) {
232-
// Ensure that node destructors execute linearly, rather
233-
// than recursing through a tree of arbitrary depth.
234-
let mut to_be_processed = vec![self.document.clone()];
235-
while let Some(node) = to_be_processed.pop() {
236-
to_be_processed.extend_from_slice(&*node.children.borrow());
237-
node.children.borrow_mut().clear();
238-
}
239-
}
240-
}
241-
242208
impl TreeSink for RcDom {
243209
type Output = Self;
244210
fn finish(self) -> Self {

xml5ever/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "xml5ever"
4-
version = "0.14.0"
4+
version = "0.15.0"
55
authors = ["The xml5ever project developers"]
66
license = "MIT / Apache-2.0"
77
repository = "https://github.com/servo/html5ever"
@@ -23,7 +23,7 @@ doctest = true
2323
time = "0.1"
2424
log = "0.4"
2525
mac = "0.1"
26-
markup5ever = {version = "0.8", path = "../markup5ever" }
26+
markup5ever = {version = "0.9", path = "../markup5ever" }
2727

2828
[dev-dependencies]
2929
serde_json = "1.0"

0 commit comments

Comments
 (0)