Skip to content

Commit

Permalink
Fix parsing of struct/variant names starting in None, Some, `true…
Browse files Browse the repository at this point in the history
…`, or `false` (#499)

* Fix parsing of struct/variant names starting in `None` or `Some`

* Add CHANGELOG entry

* Also fix for struct/variant names starting in `true` or `false`
  • Loading branch information
juntyr authored Sep 5, 2023
1 parent 10021d1 commit 9ecb1e5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add support for byte literals as strongly typed unsigned 8-bit integers ([#438](https://github.com/ron-rs/ron/pull/438))
- Fix issue [#321](https://github.com/ron-rs/ron/issues/321) and allow parsing UTF-8 identifiers ([#488](https://github.com/ron-rs/ron/pull/488))
- Breaking: Enforce that ron always writes valid UTF-8 ([#488](https://github.com/ron-rs/ron/pull/488))
- Fix parsing of struct/variant names starting in `None`, `Some`, `true`, or `false` ([#499](https://github.com/ron-rs/ron/pull/499))

## [0.8.1] - 2023-08-17

Expand Down
4 changes: 2 additions & 2 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,9 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
where
V: Visitor<'de>,
{
if self.parser.consume_str("None") {
if self.parser.consume_ident("None") {
visitor.visit_none()
} else if self.parser.consume_str("Some") && {
} else if self.parser.consume_ident("Some") && {
self.parser.skip_ws()?;
self.parser.consume_char('(')
} {
Expand Down
18 changes: 18 additions & 0 deletions src/de/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,21 @@ fn test_remainder() {
assert_eq!(deserializer.remainder(), " 37 ");
assert_eq!(deserializer.end(), Err(Error::TrailingCharacters));
}

#[test]
fn boolean_struct_name() {
check_from_str_bytes_reader::<bool>(
"true_",
Err(SpannedError {
code: Error::ExpectedBoolean,
position: Position { line: 1, col: 1 },
}),
);
check_from_str_bytes_reader::<bool>(
"false_",
Err(SpannedError {
code: Error::ExpectedBoolean,
position: Position { line: 1, col: 1 },
}),
);
}
4 changes: 2 additions & 2 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,9 @@ impl<'a> Parser<'a> {
}

pub fn bool(&mut self) -> Result<bool> {
if self.consume_str("true") {
if self.consume_ident("true") {
Ok(true)
} else if self.consume_str("false") {
} else if self.consume_ident("false") {
Ok(false)
} else {
Err(Error::ExpectedBoolean)
Expand Down
36 changes: 36 additions & 0 deletions tests/367_implicit_some.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,39 @@ fn test_nested_implicit_some() {
Ok(Some(Some(Some(5))))
);
}

#[test]
fn fuzzer_found_issues() {
#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
struct Some_();

#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
struct None_();

let ser_some = ron::ser::to_string_pretty(
&Some(Some_()),
ron::ser::PrettyConfig::default()
.struct_names(true)
.extensions(ron::extensions::Extensions::IMPLICIT_SOME),
)
.unwrap();
assert_eq!(ser_some, "#![enable(implicit_some)]\nSome_()");

let ser_none = ron::ser::to_string_pretty(
&Some(None_()),
ron::ser::PrettyConfig::default()
.struct_names(true)
.extensions(ron::extensions::Extensions::IMPLICIT_SOME),
)
.unwrap();
assert_eq!(ser_none, "#![enable(implicit_some)]\nNone_()");

assert_eq!(
ron::from_str::<Option<Some_>>(&ser_some).unwrap(),
Some(Some_())
);
assert_eq!(
ron::from_str::<Option<None_>>(&ser_none).unwrap(),
Some(None_())
);
}

0 comments on commit 9ecb1e5

Please sign in to comment.