Skip to content

Commit

Permalink
Prevent too deep recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
KamilaBorowska committed Sep 15, 2018
1 parent acdd53a commit d61b49c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub struct Scanner<T> {
simple_keys: Vec<SimpleKey>,
indent: isize,
indents: Vec<isize>,
flow_level: usize,
flow_level: u8,
tokens_parsed: usize,
token_available: bool,
}
Expand Down Expand Up @@ -906,7 +906,7 @@ impl<T: Iterator<Item=char>> Scanner<T> {
// The indicators '[' and '{' may start a simple key.
try!(self.save_simple_key());

self.increase_flow_level();
self.increase_flow_level()?;

self.allow_simple_key();

Expand Down Expand Up @@ -941,9 +941,11 @@ impl<T: Iterator<Item=char>> Scanner<T> {
Ok(())
}

fn increase_flow_level(&mut self) {
fn increase_flow_level(&mut self) -> ScanResult {
self.simple_keys.push(SimpleKey::new(Marker::new(0,0,0)));
self.flow_level += 1;
self.flow_level = self.flow_level.checked_add(1)
.ok_or_else(|| ScanError::new(self.mark, "Recursion limit exceeded"))?;
Ok(())
}
fn decrease_flow_level(&mut self) {
if self.flow_level > 0 {
Expand Down
12 changes: 12 additions & 0 deletions src/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,4 +638,16 @@ c: ~
let first = out.into_iter().next().unwrap();
assert_eq!(first[0]["important"].as_bool().unwrap(), true);
}

#[test]
fn test_recursion_depth_check_objects() {
let s = "{a:".repeat(10_000) + &"}".repeat(10_000);
assert!(YamlLoader::load_from_str(&s).is_err());
}

#[test]
fn test_recursion_depth_check_arrays() {
let s = "[".repeat(10_000) + &"]".repeat(10_000);
assert!(YamlLoader::load_from_str(&s).is_err());
}
}

0 comments on commit d61b49c

Please sign in to comment.