Skip to content

Commit

Permalink
Prevent invalid enum deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
killercup committed Jan 29, 2017
1 parent 9e4f254 commit 8e90a97
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
12 changes: 6 additions & 6 deletions json/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,26 +847,26 @@ impl<'a, R: Read + 'a> de::VariantVisitor for UnitVariantVisitor<'a, R> {
Ok(())
}

fn visit_newtype_seed<T>(self, seed: T) -> Result<T::Value>
fn visit_newtype_seed<T>(self, _seed: T) -> Result<T::Value>
where T: de::DeserializeSeed,
{
seed.deserialize(self.de)
Err(self.de.error(ErrorCode::EOFWhileParsingValue))
}

fn visit_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
fn visit_tuple<V>(self, _len: usize, _visitor: V) -> Result<V::Value>
where V: de::Visitor,
{
de::Deserializer::deserialize(self.de, visitor)
Err(self.de.error(ErrorCode::EOFWhileParsingValue))
}

fn visit_struct<V>(
self,
_fields: &'static [&'static str],
visitor: V
_visitor: V
) -> Result<V::Value>
where V: de::Visitor,
{
de::Deserializer::deserialize(self.de, visitor)
Err(self.de.error(ErrorCode::EOFWhileParsingValue))
}
}

Expand Down
14 changes: 14 additions & 0 deletions json_tests/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,8 @@ fn test_parse_enum_errors() {
"trailing characters at line 1 column 9"),
("\"Frog\"",
"EOF while parsing a value at line 1 column 6"),
("\"Frog\" 0 ",
"EOF while parsing a value at line 1 column 6"),
("{\"Frog\":{}}",
"invalid type: map, expected tuple variant Animal::Frog at line 1 column 10"),
("{\"Cat\":[]}",
Expand Down Expand Up @@ -1772,3 +1774,15 @@ fn test_json_macro() {
(<Result<(), &str> as Clone>::clone(&Err("")).unwrap_err()): "err"
});
}

#[test]
fn issue_220() {
#[derive(Debug, PartialEq, Eq, Deserialize)]
enum E {
V(u8),
}

assert!(from_str::<E>(r#" "V"0 "#).is_err());

assert_eq!(from_str::<E>(r#"{"V": 0}"#).unwrap(), E::V(0));
}

0 comments on commit 8e90a97

Please sign in to comment.