Auto merge of #421 - serde-rs:length, r=oli-obk

Invalid-length when enum seq is too short

Fixes https://github.com/serde-rs/json/issues/96.
This commit is contained in:
Homu
2016-07-05 18:27:33 +09:00
2 changed files with 34 additions and 3 deletions
+6 -3
View File
@@ -395,6 +395,7 @@ fn deserialize_seq(
fields: &[Field], fields: &[Field],
is_struct: bool, is_struct: bool,
) -> P<ast::Expr> { ) -> P<ast::Expr> {
let mut index_in_seq = 0usize;
let let_values: Vec<_> = fields.iter() let let_values: Vec<_> = fields.iter()
.enumerate() .enumerate()
.map(|(i, field)| { .map(|(i, field)| {
@@ -420,14 +421,16 @@ fn deserialize_seq(
}) })
} }
}; };
quote_stmt!(cx, let assign = quote_stmt!(cx,
let $name = match $visit { let $name = match $visit {
Some(value) => { value }, Some(value) => { value },
None => { None => {
return Err(_serde::de::Error::end_of_stream()); return Err(_serde::de::Error::invalid_length($index_in_seq));
} }
}; };
).unwrap() ).unwrap();
index_in_seq += 1;
assign
} }
}) })
.collect(); .collect();
+28
View File
@@ -946,3 +946,31 @@ fn test_missing_renamed_field_enum() {
Error::MissingField("d"), Error::MissingField("d"),
); );
} }
#[derive(Debug, PartialEq, Deserialize)]
enum InvalidLengthEnum {
A(i32, i32, i32),
B(#[serde(skip_deserializing)] i32, i32, i32),
}
#[test]
fn test_invalid_length_enum() {
assert_de_tokens_error::<InvalidLengthEnum>(
&[
Token::EnumSeqStart("InvalidLengthEnum", "A", Some(3)),
Token::EnumSeqSep,
Token::I32(1),
Token::EnumSeqEnd,
],
Error::InvalidLength(1),
);
assert_de_tokens_error::<InvalidLengthEnum>(
&[
Token::EnumSeqStart("InvalidLengthEnum", "B", Some(3)),
Token::EnumSeqSep,
Token::I32(1),
Token::EnumSeqEnd,
],
Error::InvalidLength(1),
);
}