Fix towards invalid trailing characters.

This commit is contained in:
Ethiraric 2024-01-18 19:16:02 +01:00
parent 032efff867
commit bff3c4ccaf
2 changed files with 34 additions and 1 deletions

View file

@ -679,6 +679,10 @@ impl<T: Iterator<Item = char>> Scanner<T> {
&& is_blankz(self.buffer[3]) && is_blankz(self.buffer[3])
{ {
self.fetch_document_indicator(TokenType::DocumentEnd)?; self.fetch_document_indicator(TokenType::DocumentEnd)?;
self.skip_ws_to_eol(SkipTabs::Yes);
if !is_breakz(self.ch()) {
return Err(ScanError::new(self.mark, "invalid content after document end marker"));
}
return Ok(()); return Ok(());
} }
@ -1809,6 +1813,25 @@ impl<T: Iterator<Item = char>> Scanner<T> {
// Eat the right quote. // Eat the right quote.
self.skip(); self.skip();
// Ensure there is no invalid trailing content.
self.skip_ws_to_eol(SkipTabs::Yes);
match self.ch() {
// These can be encountered in flow sequences or mappings.
',' | '}' | ']' if self.flow_level > 0 => {}
// An end-of-line / end-of-stream is fine. No trailing content.
c if is_breakz(c) => {}
// ':' can be encountered if our scalar is a key.
// Outside of flow contexts, keys cannot span multiple lines
':' if self.flow_level == 0 && start_mark.line == self.mark.line => {}
// Inside a flow context, this is allowed.
':' if self.flow_level > 0 => {}
_ => {
return Err(ScanError::new(
self.mark,
"invalid trailing content after double-quoted scalar",
));
}
}
let style = if single { let style = if single {
TScalarStyle::SingleQuoted TScalarStyle::SingleQuoted

View file

@ -52,7 +52,9 @@ scalar
key: [1, 2]] key: [1, 2]]
key1:a2 key1:a2
"; ";
let Err(error) = YamlLoader::load_from_str(s) else { panic!() }; let Err(error) = YamlLoader::load_from_str(s) else {
panic!()
};
assert_eq!( assert_eq!(
error.info(), error.info(),
"mapping values are not allowed in this context" "mapping values are not allowed in this context"
@ -235,6 +237,14 @@ fn test_issue_65() {
assert!(YamlLoader::load_from_str(b).is_err()); assert!(YamlLoader::load_from_str(b).is_err());
} }
#[test]
fn test_issue_65_mwe() {
// A MWE for `test_issue_65`. The error over there is that there is invalid trailing content
// after a double quoted string.
let b = r#""foo" l"#;
assert!(YamlLoader::load_from_str(b).is_err());
}
#[test] #[test]
fn test_bad_docstart() { fn test_bad_docstart() {
assert!(YamlLoader::load_from_str("---This used to cause an infinite loop").is_ok()); assert!(YamlLoader::load_from_str("---This used to cause an infinite loop").is_ok());