From 0f54a1a0df5045aee4a2d2f8656c365d835095e5 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 19 Oct 2024 09:22:33 -0700 Subject: [PATCH] Handle early return sooner on eof in seq or map This matches how peeking is done within deserialize_any and other Deserializer methods --- src/de.rs | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/de.rs b/src/de.rs index 88ca3e66c..a22fcf0de 100644 --- a/src/de.rs +++ b/src/de.rs @@ -1929,9 +1929,16 @@ impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> { fn has_next_element<'de, 'a, R: Read<'de> + 'a>( seq: &mut SeqAccess<'a, R>, ) -> Result { - match tri!(seq.de.parse_whitespace()) { - Some(b']') => Ok(false), - Some(b',') if !seq.first => { + let peek = match tri!(seq.de.parse_whitespace()) { + Some(b) => b, + None => { + return Err(seq.de.peek_error(ErrorCode::EofWhileParsingList)); + } + }; + + match peek { + b']' => Ok(false), + b',' if !seq.first => { seq.de.eat_char(); match tri!(seq.de.parse_whitespace()) { Some(b']') => Err(seq.de.peek_error(ErrorCode::TrailingComma)), @@ -1939,7 +1946,7 @@ impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> { None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingValue)), } } - Some(_) => { + _ => { if seq.first { seq.first = false; Ok(true) @@ -1947,7 +1954,6 @@ impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> { Err(seq.de.peek_error(ErrorCode::ExpectedListCommaOrEnd)) } } - None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingList)), } } @@ -1978,9 +1984,16 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> { K: de::DeserializeSeed<'de>, { fn has_next_key<'de, 'a, R: Read<'de> + 'a>(map: &mut MapAccess<'a, R>) -> Result { - match tri!(map.de.parse_whitespace()) { - Some(b'}') => Ok(false), - Some(b',') if !map.first => { + let peek = match tri!(map.de.parse_whitespace()) { + Some(b) => b, + None => { + return Err(map.de.peek_error(ErrorCode::EofWhileParsingObject)); + } + }; + + match peek { + b'}' => Ok(false), + b',' if !map.first => { map.de.eat_char(); match tri!(map.de.parse_whitespace()) { Some(b'"') => Ok(true), @@ -1989,10 +2002,10 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> { None => Err(map.de.peek_error(ErrorCode::EofWhileParsingValue)), } } - Some(b) => { + _ => { if map.first { map.first = false; - if b == b'"' { + if peek == b'"' { Ok(true) } else { Err(map.de.peek_error(ErrorCode::KeyMustBeAString)) @@ -2001,7 +2014,6 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> { Err(map.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd)) } } - None => Err(map.de.peek_error(ErrorCode::EofWhileParsingObject)), } }