@@ -316,7 +316,8 @@ pub enum DecoderError {
316
316
ExpectedError ( string:: String , string:: String ) ,
317
317
MissingFieldError ( string:: String ) ,
318
318
UnknownVariantError ( string:: String ) ,
319
- ApplicationError ( string:: String )
319
+ ApplicationError ( string:: String ) ,
320
+ EOF ,
320
321
}
321
322
322
323
#[ derive( Copy , Debug ) ]
@@ -1995,21 +1996,21 @@ impl Decoder {
1995
1996
}
1996
1997
1997
1998
impl Decoder {
1998
- fn pop ( & mut self ) -> Json {
1999
- self . stack . pop ( ) . unwrap ( )
1999
+ fn pop ( & mut self ) -> DecodeResult < Json > {
2000
+ self . stack . pop ( ) . ok_or ( EOF )
2000
2001
}
2001
2002
}
2002
2003
2003
2004
macro_rules! expect {
2004
2005
( $e: expr, Null ) => ( {
2005
- match $e {
2006
+ match try! ( $e ) {
2006
2007
Json :: Null => Ok ( ( ) ) ,
2007
2008
other => Err ( ExpectedError ( "Null" . to_string( ) ,
2008
2009
format!( "{}" , other) ) )
2009
2010
}
2010
2011
} ) ;
2011
2012
( $e: expr, $t: ident) => ( {
2012
- match $e {
2013
+ match try! ( $e ) {
2013
2014
Json :: $t( v) => Ok ( v) ,
2014
2015
other => {
2015
2016
Err ( ExpectedError ( stringify!( $t) . to_string( ) ,
@@ -2022,7 +2023,7 @@ macro_rules! expect {
2022
2023
macro_rules! read_primitive {
2023
2024
( $name: ident, $ty: ty) => {
2024
2025
fn $name( & mut self ) -> DecodeResult <$ty> {
2025
- match self . pop( ) {
2026
+ match try! ( self . pop( ) ) {
2026
2027
Json :: I64 ( f) => match num:: cast( f) {
2027
2028
Some ( f) => Ok ( f) ,
2028
2029
None => Err ( ExpectedError ( "Number" . to_string( ) , format!( "{}" , f) ) ) ,
@@ -2065,7 +2066,7 @@ impl ::Decoder for Decoder {
2065
2066
fn read_f32 ( & mut self ) -> DecodeResult < f32 > { self . read_f64 ( ) . map ( |x| x as f32 ) }
2066
2067
2067
2068
fn read_f64 ( & mut self ) -> DecodeResult < f64 > {
2068
- match self . pop ( ) {
2069
+ match try! ( self . pop ( ) ) {
2069
2070
Json :: I64 ( f) => Ok ( f as f64 ) ,
2070
2071
Json :: U64 ( f) => Ok ( f as f64 ) ,
2071
2072
Json :: F64 ( f) => Ok ( f) ,
@@ -2113,7 +2114,7 @@ impl ::Decoder for Decoder {
2113
2114
mut f : F ) -> DecodeResult < T >
2114
2115
where F : FnMut ( & mut Decoder , usize ) -> DecodeResult < T > ,
2115
2116
{
2116
- let name = match self . pop ( ) {
2117
+ let name = match try! ( self . pop ( ) ) {
2117
2118
Json :: String ( s) => s,
2118
2119
Json :: Object ( mut o) => {
2119
2120
let n = match o. remove ( & "variant" . to_string ( ) ) {
@@ -2178,7 +2179,7 @@ impl ::Decoder for Decoder {
2178
2179
F : FnOnce ( & mut Decoder ) -> DecodeResult < T > ,
2179
2180
{
2180
2181
let value = try!( f ( self ) ) ;
2181
- self . pop ( ) ;
2182
+ try! ( self . pop ( ) ) ;
2182
2183
Ok ( value)
2183
2184
}
2184
2185
@@ -2250,7 +2251,7 @@ impl ::Decoder for Decoder {
2250
2251
fn read_option < T , F > ( & mut self , mut f : F ) -> DecodeResult < T > where
2251
2252
F : FnMut ( & mut Decoder , bool ) -> DecodeResult < T > ,
2252
2253
{
2253
- match self . pop ( ) {
2254
+ match try! ( self . pop ( ) ) {
2254
2255
Json :: Null => f ( self , false ) ,
2255
2256
value => { self . stack . push ( value) ; f ( self , true ) }
2256
2257
}
@@ -3847,6 +3848,18 @@ mod tests {
3847
3848
}
3848
3849
}
3849
3850
3851
+ #[ test]
3852
+ fn test_bad_json_stack_depleted ( ) {
3853
+ use json;
3854
+ #[ derive( Debug , RustcDecodable ) ]
3855
+ enum ChatEvent {
3856
+ Variant ( i32 )
3857
+ }
3858
+ let serialized = "{\" variant\" : \" Variant\" , \" fields\" : []}" ;
3859
+ let r: Result < ChatEvent , _ > = json:: decode ( serialized) ;
3860
+ assert ! ( r. unwrap_err( ) == EOF ) ;
3861
+ }
3862
+
3850
3863
#[ bench]
3851
3864
fn bench_streaming_small ( b : & mut Bencher ) {
3852
3865
b. iter ( || {
0 commit comments