From 1a449bb3d0216c6a2a0fb14cec0b2504016895f5 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 5 Jul 2016 00:52:19 -0700 Subject: [PATCH] Invalid-length when enum seq is too short --- serde_codegen/src/de.rs | 9 ++++++--- testing/tests/test_annotations.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/serde_codegen/src/de.rs b/serde_codegen/src/de.rs index 69d70bc3..98c4aaf6 100644 --- a/serde_codegen/src/de.rs +++ b/serde_codegen/src/de.rs @@ -395,6 +395,7 @@ fn deserialize_seq( fields: &[Field], is_struct: bool, ) -> P { + let mut index_in_seq = 0usize; let let_values: Vec<_> = fields.iter() .enumerate() .map(|(i, field)| { @@ -420,14 +421,16 @@ fn deserialize_seq( }) } }; - quote_stmt!(cx, + let assign = quote_stmt!(cx, let $name = match $visit { Some(value) => { value }, 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(); diff --git a/testing/tests/test_annotations.rs b/testing/tests/test_annotations.rs index 0a795725..a18a4b82 100644 --- a/testing/tests/test_annotations.rs +++ b/testing/tests/test_annotations.rs @@ -946,3 +946,31 @@ fn test_missing_renamed_field_enum() { 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::( + &[ + Token::EnumSeqStart("InvalidLengthEnum", "A", Some(3)), + Token::EnumSeqSep, + Token::I32(1), + Token::EnumSeqEnd, + ], + Error::InvalidLength(1), + ); + assert_de_tokens_error::( + &[ + Token::EnumSeqStart("InvalidLengthEnum", "B", Some(3)), + Token::EnumSeqSep, + Token::I32(1), + Token::EnumSeqEnd, + ], + Error::InvalidLength(1), + ); +}