Skip to content

Commit dac9a1c

Browse files
committed
libuuid: use Decoder::error() rather than failing on bad decode
1 parent 5bd8edc commit dac9a1c

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/libuuid/lib.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,10 @@ impl<T: Encoder<E>, E> Encodable<T, E> for Uuid {
501501
impl<T: Decoder<E>, E> Decodable<T, E> for Uuid {
502502
/// Decode a UUID from a string
503503
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+
}
505508
}
506509
}
507510

@@ -802,6 +805,23 @@ mod test {
802805
assert_eq!(u, u2);
803806
}
804807

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+
805825
#[test]
806826
fn test_iterbytes_impl_for_uuid() {
807827
use std::collections::HashSet;

0 commit comments

Comments
 (0)