Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Fix unreachable code panic on scan error #295

Merged
merged 3 commits into from Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/de.rs
Expand Up @@ -101,17 +101,20 @@ impl<'de> Deserializer<'de> {
let mut pos = 0;
let mut jumpcount = 0;

match &self.progress {
match self.progress {
Progress::Iterable(_) => return Err(error::new(ErrorImpl::MoreThanOneDocument)),
Progress::Document(document) => {
let t = f(&mut DeserializerFromEvents {
document,
document: &document,
pos: &mut pos,
jumpcount: &mut jumpcount,
path: Path::Root,
remaining_depth: 128,
current_enum: None,
})?;
if let Some(parse_error) = document.error {
return Err(error::shared(parse_error));
}
return Ok(t);
}
_ => {}
Expand All @@ -130,6 +133,9 @@ impl<'de> Deserializer<'de> {
remaining_depth: 128,
current_enum: None,
})?;
if let Some(parse_error) = document.error {
return Err(error::shared(parse_error));
}
if loader.next_document().is_none() {
Ok(t)
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/libyaml/parser.rs
Expand Up @@ -84,6 +84,9 @@ impl<'input> Parser<'input> {
let mut event = MaybeUninit::<sys::yaml_event_t>::uninit();
unsafe {
let parser = addr_of_mut!((*self.pin.ptr).sys);
if (*parser).error != sys::YAML_NO_ERROR {
return Err(Error::parse_error(parser));
}
let event = event.as_mut_ptr();
if sys::yaml_parser_parse(parser, event).fail {
return Err(Error::parse_error(parser));
Expand Down
9 changes: 8 additions & 1 deletion tests/test_error.rs
Expand Up @@ -3,7 +3,7 @@
use indoc::indoc;
use serde::de::{Deserialize, SeqAccess, Visitor};
use serde_derive::{Deserialize, Serialize};
use serde_yaml::Deserializer;
use serde_yaml::{Deserializer, Value};
use std::collections::BTreeMap;
use std::fmt::{self, Debug};

Expand All @@ -15,6 +15,13 @@ where
assert_eq!(expected, result.unwrap_err().to_string());
}

#[test]
fn test_scan_error() {
let yaml = ">\n@";
let expected = "found character that cannot start any token at line 2 column 1, while scanning for the next token";
test_error::<Value>(yaml, expected);
}

#[test]
fn test_incorrect_type() {
let yaml = indoc! {"
Expand Down