@@ -501,7 +501,10 @@ impl<T: Encoder<E>, E> Encodable<T, E> for Uuid {
501
501
impl < T : Decoder < E > , E > Decodable < T , E > for Uuid {
502
502
/// Decode a UUID from a string
503
503
fn decode ( d : & mut T ) -> Result < Uuid , E > {
504
- Ok ( from_str ( try!( d. read_str ( ) ) . as_slice ( ) ) . unwrap ( ) )
504
+ match from_str ( try!( d. read_str ( ) ) . as_slice ( ) ) {
505
+ Some ( decode) => Ok ( decode) ,
506
+ None => Err ( d. error ( "Unable to decode UUID" ) )
507
+ }
505
508
}
506
509
}
507
510
@@ -802,6 +805,23 @@ mod test {
802
805
assert_eq ! ( u, u2) ;
803
806
}
804
807
808
+ #[ test]
809
+ fn test_bad_decode ( ) {
810
+ use serialize:: json;
811
+ use serialize:: { Encodable , Decodable } ;
812
+
813
+ let js_good = json:: String ( "a1a2a3a4a5a6a7a8a1a2a3a4a5a6a7a8" . to_string ( ) ) ;
814
+ let js_bad1 = json:: String ( "a1a2a3a4a5a6a7a8a1a2a3a4a5a6a7ah" . to_string ( ) ) ;
815
+ let js_bad2 = json:: String ( "a1a2a3a4a5a6a7a8a1a2a3a4a5a6a7a" . to_string ( ) ) ;
816
+
817
+ let u_good: Result < Uuid , _ > = Decodable :: decode ( & mut json:: Decoder :: new ( js_good) ) ;
818
+ let u_bad1: Result < Uuid , _ > = Decodable :: decode ( & mut json:: Decoder :: new ( js_bad1) ) ;
819
+ let u_bad2: Result < Uuid , _ > = Decodable :: decode ( & mut json:: Decoder :: new ( js_bad2) ) ;
820
+ assert ! ( u_good. is_ok( ) ) ;
821
+ assert ! ( u_bad1. is_err( ) ) ;
822
+ assert ! ( u_bad2. is_err( ) ) ;
823
+ }
824
+
805
825
#[ test]
806
826
fn test_iterbytes_impl_for_uuid ( ) {
807
827
use std:: collections:: HashSet ;
0 commit comments