Fix infinite loop on {....

This commit is contained in:
Ethiraric 2024-10-19 04:14:50 +02:00
parent a704716f86
commit 3358629dd1
3 changed files with 28 additions and 6 deletions

View file

@ -2177,7 +2177,7 @@ impl<T: Input> Scanner<T> {
loop {
self.input.lookahead(4);
if self.input.next_is_document_end()
|| (self.input.next_is_document_start() && self.leading_whitespace)
|| (self.leading_whitespace && self.input.next_is_document_start())
|| self.input.peek() == '#'
{
break;
@ -2293,10 +2293,20 @@ impl<T: Input> Scanner<T> {
self.allow_simple_key();
}
Ok(Token(
Span::new(start_mark, end_mark),
TokenType::Scalar(TScalarStyle::Plain, string),
))
if string.is_empty() {
// `fetch_plain_scalar` must absolutely consume at least one byte. Otherwise,
// `fetch_next_token` will never stop calling it. An empty plain scalar may happen with
// erroneous inputs such as "{...".
Err(ScanError::new_str(
start_mark,
"unexpected end of plain scalar",
))
} else {
Ok(Token(
Span::new(start_mark, end_mark),
TokenType::Scalar(TScalarStyle::Plain, string),
))
}
}
fn fetch_key(&mut self) -> ScanResult {

View file

@ -214,6 +214,17 @@ fn test_issue14() {
);
}
#[test]
fn test_issue14_v2() {
let s = "{...";
let Err(error) = run_parser(s) else { panic!() };
assert_eq!(error.info(), "unexpected end of plain scalar");
assert_eq!(
error.to_string(),
"unexpected end of plain scalar at byte 1 line 1 column 2"
);
}
#[test]
fn test_issue13() {
// The following input creates an infinite loop.

View file

@ -94,7 +94,8 @@ fn load_tests_from_file(entry: &DirEntry) -> Result<Vec<Test<YamlTest>>> {
let test_name = file_name
.strip_suffix(".yaml")
.ok_or("unexpected filename")?;
let tests = Yaml::load_from_str(&fs::read_to_string(entry.path())?)?;
let tests = Yaml::load_from_str(&fs::read_to_string(entry.path())?)
.map_err(|e| format!("While reading {file_name}: {e}"))?;
let tests = tests[0].as_vec().ok_or("no test list found in file")?;
let mut result = vec![];