Fix unwrap being hit in macro when malicious input is given

This commit is contained in:
Ephraim Kunz 2022-01-08 07:52:13 -08:00
parent 19b51168c9
commit c4d4828c56
2 changed files with 28 additions and 1 deletions

View File

@ -107,3 +107,30 @@ fn option_struct() {
);
test_for_type!(Option::<Test>, None, None);
}
#[test]
fn option_bool_no_crash_on_bad_input() {
init();
#[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
struct Test {
field: SubTest,
}
#[derive(Debug, PartialEq, YaDeserialize, YaSerialize)]
struct SubTest {
#[yaserde(attribute)]
content: Option<bool>,
}
impl Default for SubTest {
fn default() -> Self {
SubTest { content: None }
}
}
let content = "<field><content>/<R/";
let result: Result<Test, String> = yaserde::de::from_str(content);
assert!(result.is_err());
}

View File

@ -116,7 +116,7 @@ pub fn parse(
self,
v: &str,
) -> ::std::result::Result<Self::Value, ::std::string::String> {
::std::result::Result::Ok(#field_type::from_str(#map_if_bool).unwrap())
#field_type::from_str(#map_if_bool).map_err(|e| e.to_string())
}
}
})