Skip to content

Commit

Permalink
Fix issue with deeply indented block scalars.
Browse files Browse the repository at this point in the history
Fixes #29.
  • Loading branch information
Ethiraric committed May 31, 2024
1 parent 0eb7936 commit 9f56534
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,10 @@ impl<T: Iterator<Item = char>> Scanner<T> {
while !self.buffer.is_empty() && self.mark.col < indent && self.ch() == ' ' {
self.skip_blank();
}
if !(!self.buffer.is_empty() && self.mark.col < indent && self.ch() == ' ') {
// If we reached our indent, we can break. We must also break if we have
// reached content or EOF; that is, the buffer is not empty and the next
// character is not a space.
if self.mark.col == indent || (!self.buffer.is_empty() && self.ch() != ' ') {
break;
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,30 @@ foobar";
);
}

#[test]
fn test_large_block_scalar_indent() {
// https://github.com/Ethiraric/yaml-rust2/issues/29
// Tests the `loop` fallback of `skip_block_scalar_indent`. The indent in the YAML string must
// be greater than `BUFFER_LEN - 2`. The second line is further indented with spaces, and the
// resulting string should be "a\n b".
let s = "
a: |-
a
b
";

let doc = &YamlLoader::load_from_str(s).unwrap()[0];
let Yaml::Hash(map) = doc else {
dbg!(doc);
panic!()
};
assert_eq!(map.len(), 1);
assert_eq!(
map.get(&Yaml::String("a".to_string())),
Some(&Yaml::String(String::from("a\n b")))
);
}

#[test]
fn test_bad_docstart() {
assert!(YamlLoader::load_from_str("---This used to cause an infinite loop").is_ok());
Expand Down

0 comments on commit 9f56534

Please sign in to comment.