Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of struct/variant names starting in None, Some, true, or false #499

Merged
merged 3 commits into from
Sep 5, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_())
);
}
Loading