Skip to content

Commit

Permalink
Fix fuzzer found issue with br# being parsed as the identifier br
Browse files Browse the repository at this point in the history
  • Loading branch information
juntyr committed Aug 21, 2023
1 parent e366705 commit 5a4b2c7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ impl<'a> Bytes<'a> {
match self.bytes.get(1) {
Some(b'"') => return Err(Error::ExpectedIdentifier),
Some(b'r') => match self.bytes.get(2) {
Some(b'"') => return Err(Error::ExpectedIdentifier),
Some(b'#' | b'"') => return Err(Error::ExpectedIdentifier),
Some(_) | None => (),
},
Some(_) | None => (),
Expand Down
63 changes: 63 additions & 0 deletions tests/438_rusty_byte_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,69 @@ fn fuzzer_failures() {
.unwrap(),
"b\"{\x00\x00\x00\x00\""
);

// `br#` should be parsed as the start of a byte string, not the identifier `br` and a `#`
assert_eq!(
ron::from_str(r##"br#"""#"##),
Ok(ron::Value::Bytes(vec![34]))
);
assert_eq!(
ron::from_str(r##"{"error": br#"""#}"##),
Ok(ron::Value::Map(
[(
ron::Value::String(String::from("error")),
ron::Value::Bytes(vec![34])
)]
.into_iter()
.collect()
))
);
assert_eq!(
ron::from_str(
r##"#![enable(unwrap_newtypes)]
#![enable(unwrap_variant_newtypes)]
Some({"error": br#"""#})
"##
),
Ok(ron::Value::Option(Some(Box::new(ron::Value::Map(
[(
ron::Value::String(String::from("error")),
ron::Value::Bytes(vec![34])
)]
.into_iter()
.collect()
)))))
);

// `br"` should be parsed as the start of a byte string, not the identifier `br` and a `"`
assert_eq!(ron::from_str(r#"br"""#), Ok(ron::Value::Bytes(vec![])));
assert_eq!(
ron::from_str(r#"{"error": br""}"#),
Ok(ron::Value::Map(
[(
ron::Value::String(String::from("error")),
ron::Value::Bytes(vec![])
)]
.into_iter()
.collect()
))
);
assert_eq!(
ron::from_str(
r#"#![enable(unwrap_newtypes)]
#![enable(unwrap_variant_newtypes)]
Some({"error": br""})
"#
),
Ok(ron::Value::Option(Some(Box::new(ron::Value::Map(
[(
ron::Value::String(String::from("error")),
ron::Value::Bytes(vec![])
)]
.into_iter()
.collect()
)))))
);
}

#[test]
Expand Down

0 comments on commit 5a4b2c7

Please sign in to comment.