Skip to content

Commit 8c491a5

Browse files
committed
recur ignore branch nodes with no children and add some limit checks
1 parent 801b61b commit 8c491a5

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/algorithm/recur.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ pub fn recur(is_leaf: SigNode, children: SigNode, combine: SigNode, env: &mut Ui
7171
scalar_child,
7272
}) = stack.pop()
7373
{
74+
env.respect_execution_limit()?;
75+
if stack.len() > 1_000_000 {
76+
return Err(env.error(
77+
"recur reached more than 1 million nodes at once. The base case may be incorrect.",
78+
));
79+
}
7480
// println!("value: {value:?}, parent: {parent:?}, child_nodes: {child_nodes:?}");
7581
if let Some(child_nodes) = child_nodes {
7682
env.push_all(
@@ -144,21 +150,23 @@ pub fn recur(is_leaf: SigNode, children: SigNode, combine: SigNode, env: &mut Ui
144150
env.push(value.clone());
145151
env.exec(children.clone())?;
146152
let children = env.pop("child nodes")?;
147-
let index = stack.len();
148-
// println!("{value:?} has children {children:?}");
149-
stack.push(RecNode {
150-
parent,
151-
value,
152-
child_nodes: Some(Vec::new()),
153-
scalar_child: children.rank() == 0,
154-
});
155-
for value in children.into_rows() {
153+
if children.row_count() > 0 {
154+
let index = stack.len();
155+
// println!("{value:?} has children {children:?}");
156156
stack.push(RecNode {
157-
parent: Some(index),
157+
parent,
158158
value,
159-
child_nodes: None,
160-
scalar_child: false,
159+
child_nodes: Some(Vec::new()),
160+
scalar_child: children.rank() == 0,
161161
});
162+
for value in children.into_rows() {
163+
stack.push(RecNode {
164+
parent: Some(index),
165+
value,
166+
child_nodes: None,
167+
scalar_child: false,
168+
});
169+
}
162170
}
163171
}
164172
}

0 commit comments

Comments
 (0)