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

Document a further fuzzer-found limitation #515

Merged
merged 1 commit into from
Oct 11, 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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,6 @@ While data structures with any of these attributes should generally roundtrip th
- externally tagged tuple variants with just one field (that are not newtype variants)
- tuples or arrays with just one element are not supported inside newtype variants with `#[enable(unwrap_variant_newtypes)]`
- a `ron::value::RawValue`
- internally tagged newtype variants and `#[serde(flatten)]`ed fields must not contain:
- a unit or a unit struct inside an untagged newtype variant
- an untagged unit variant
- untagged tuple / struct variants with no fields are not supported
- untagged tuple variants with just one field (that are not newtype variants) are not supported when the `#![enable(unwrap_variant_newtypes)]` extension is enabled
- serializing a `ron::value::RawValue` using a `PrettyConfig` may add leading and trailing whitespace and comments, which the `ron::value::RawValue` absorbs upon deserialization
Expand All @@ -214,6 +211,11 @@ Furthermore, serde imposes the following restrictions for data to roundtrip:
- a flattened tagged struct
- internally (or adjacently) tagged or untagged enum variants or `#[serde(flatten)]`ed fields must not contain:
- `i128` or `u128` values
- internally tagged newtype variants and `#[serde(flatten)]`ed fields must not contain:
- a unit or a unit struct inside an untagged newtype variant
- an untagged unit variant
- internally tagged newtype variants, which are `#[serde(flatten)]`ed together with other fields, must not contain:
- a unit or unit struct or an untagged unit variant

Please file a [new issue](https://github.com/ron-rs/ron/issues/new) if you come across a use case which is not listed among the above restrictions but still breaks.

Expand Down
60 changes: 17 additions & 43 deletions fuzz/fuzz_targets/bench/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5191,7 +5191,7 @@ impl<'a> SerdeDataType<'a> {
{
return Err(arbitrary::Error::IncorrectFormat);
}
if matches!(representation, SerdeEnumRepresentation::InternallyTagged { tag: _ } if !inner.supported_inside_internally_tagged_newtype(&value, false))
if matches!(representation, SerdeEnumRepresentation::InternallyTagged { tag: _ } if !inner.supported_inside_internally_tagged_newtype(false))
{
return Err(arbitrary::Error::IncorrectFormat);
}
Expand Down Expand Up @@ -5472,7 +5472,6 @@ impl<'a> SerdeDataType<'a> {

fn supported_inside_internally_tagged_newtype(
&self,
value: &SerdeDataValue,
inside_untagged_newtype_variant: bool,
) -> bool {
// See https://github.com/serde-rs/serde/blob/ddc1ee564b33aa584e5a66817aafb27c3265b212/serde/src/private/ser.rs#L94-L336
Expand Down Expand Up @@ -5513,14 +5512,7 @@ impl<'a> SerdeDataType<'a> {
!inside_untagged_newtype_variant
}
SerdeDataType::Newtype { name: _, inner: ty } => {
if let SerdeDataValue::Newtype { inner: value } = value {
ty.supported_inside_internally_tagged_newtype(
value,
inside_untagged_newtype_variant,
)
} else {
false
}
ty.supported_inside_internally_tagged_newtype(inside_untagged_newtype_variant)
}
SerdeDataType::TupleStruct { name: _, fields: _ } => false,
SerdeDataType::Struct {
Expand All @@ -5533,53 +5525,27 @@ impl<'a> SerdeDataType<'a> {
variants,
representation,
} => {
let SerdeDataValue::Enum {
variant: variant_index,
value,
} = value
else {
return false;
};

let Some(ty) = variants.1.get(*variant_index as usize) else {
return false;
};

match (ty, value) {
(SerdeDataVariantType::Unit, SerdeDataVariantValue::Unit)
| (
SerdeDataVariantType::TaggedOther,
SerdeDataVariantValue::TaggedOther { .. },
) => {
variants.1.iter().all(|ty| match ty {
SerdeDataVariantType::Unit | SerdeDataVariantType::TaggedOther => {
// BUG: an untagged unit variant requires a unit,
// but it won't get one because it serialises itself as a unit,
// which is only serialised with the tag
!matches!(representation, SerdeEnumRepresentation::Untagged)
}
(
SerdeDataVariantType::Newtype { inner: ty },
SerdeDataVariantValue::Newtype { inner: value },
) => {
SerdeDataVariantType::Newtype { inner: ty } => {
if matches!(representation, SerdeEnumRepresentation::Untagged) {
ty.supported_inside_internally_tagged_newtype(value, true)
ty.supported_inside_internally_tagged_newtype(true)
} else {
true
}
}
(
SerdeDataVariantType::Tuple { fields: _ },
SerdeDataVariantValue::Struct { fields: _ },
) => !matches!(
SerdeDataVariantType::Tuple { fields: _ } => !matches!(
representation,
SerdeEnumRepresentation::Untagged
| SerdeEnumRepresentation::AdjacentlyTagged { .. }
),
(
SerdeDataVariantType::Struct { fields: _ },
SerdeDataVariantValue::Struct { fields: _ },
) => true,
_ => false,
}
SerdeDataVariantType::Struct { fields: _ } => true,
})
}
}
}
Expand Down Expand Up @@ -5802,6 +5768,14 @@ impl<'a> SerdeDataType<'a> {
has_unknown_key,
)
} else if is_flattened {
if matches!(representation, SerdeEnumRepresentation::InternallyTagged { tag: _ }) {
// BUG: an flattened internally tagged newtype alongside other flattened data
// must not contain a unit, unit struct, or untagged unit variant
if !inner.supported_inside_internally_tagged_newtype(true) {
return false;
}
}

if *has_flattened_map {
// BUG: a flattened map will also see the unknown key (serde)
return false;
Expand Down
87 changes: 87 additions & 0 deletions tests/502_known_bugs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2800,6 +2800,93 @@ fn untagged_unit_variant_inside_flatten_struct_variant() {
);
}

#[test]
fn unit_inside_internally_tagged_newtype_variant_inside_multi_flatten_struct() {
#[derive(PartialEq, Debug, Serialize, Deserialize)]
struct AnotherFlattenedStruct {
hi: i32,
}

#[derive(PartialEq, Debug, Serialize, Deserialize)]
#[serde(tag = "tag")]
enum InternallyTagged {
Newtype(()),
}

#[derive(PartialEq, Debug, Serialize, Deserialize)]
struct FlattenedStruct {
ho: i32,
#[serde(flatten)]
a: InternallyTagged,
#[serde(flatten)]
b: AnotherFlattenedStruct,
}

assert_eq!(
check_roundtrip(
&FlattenedStruct {
ho: 24,
a: InternallyTagged::Newtype(()),
b: AnotherFlattenedStruct { hi: 42 },
},
PrettyConfig::default()
),
Err(Err(SpannedError {
code: Error::InvalidValueForType {
expected: String::from("unit"),
found: String::from("a map")
},
position: Position { line: 5, col: 1 }
}))
);
}

#[test]
fn untagged_unit_variant_inside_internally_tagged_newtype_variant_inside_multi_flatten_struct() {
#[derive(PartialEq, Debug, Serialize, Deserialize)]
#[serde(untagged)]
enum Untagged {
Unit,
}

#[derive(PartialEq, Debug, Serialize, Deserialize)]
struct AnotherFlattenedStruct {
hi: i32,
}

#[derive(PartialEq, Debug, Serialize, Deserialize)]
#[serde(tag = "tag")]
enum InternallyTagged {
Newtype(Untagged),
}

#[derive(PartialEq, Debug, Serialize, Deserialize)]
struct FlattenedStruct {
ho: i32,
#[serde(flatten)]
a: InternallyTagged,
#[serde(flatten)]
b: AnotherFlattenedStruct,
}

assert_eq!(
check_roundtrip(
&FlattenedStruct {
ho: 24,
a: InternallyTagged::Newtype(Untagged::Unit),
b: AnotherFlattenedStruct { hi: 42 },
},
PrettyConfig::default()
),
Err(Err(SpannedError {
code: Error::Message(String::from(
"data did not match any variant of untagged enum Untagged"
)),
position: Position { line: 5, col: 1 }
}))
);
}

fn check_roundtrip<T: PartialEq + std::fmt::Debug + Serialize + serde::de::DeserializeOwned>(
val: &T,
config: PrettyConfig,
Expand Down
Loading