Skip to content

Commit

Permalink
libuuid: use Decoder::error() rather than failing on bad decode
Browse files Browse the repository at this point in the history
  • Loading branch information
apoelstra committed Aug 1, 2014
1 parent 5bd8edc commit dac9a1c
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/libuuid/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,10 @@ impl<T: Encoder<E>, E> Encodable<T, E> for Uuid {
impl<T: Decoder<E>, E> Decodable<T, E> for Uuid {
/// Decode a UUID from a string
fn decode(d: &mut T) -> Result<Uuid, E> {
Ok(from_str(try!(d.read_str()).as_slice()).unwrap())
match from_str(try!(d.read_str()).as_slice()) {
Some(decode) => Ok(decode),
None => Err(d.error("Unable to decode UUID"))
}
}
}

Expand Down Expand Up @@ -802,6 +805,23 @@ mod test {
assert_eq!(u, u2);
}

#[test]
fn test_bad_decode() {
use serialize::json;
use serialize::{Encodable, Decodable};

let js_good = json::String("a1a2a3a4a5a6a7a8a1a2a3a4a5a6a7a8".to_string());
let js_bad1 = json::String("a1a2a3a4a5a6a7a8a1a2a3a4a5a6a7ah".to_string());
let js_bad2 = json::String("a1a2a3a4a5a6a7a8a1a2a3a4a5a6a7a".to_string());

let u_good: Result<Uuid, _> = Decodable::decode(&mut json::Decoder::new(js_good));
let u_bad1: Result<Uuid, _> = Decodable::decode(&mut json::Decoder::new(js_bad1));
let u_bad2: Result<Uuid, _> = Decodable::decode(&mut json::Decoder::new(js_bad2));
assert!(u_good.is_ok());
assert!(u_bad1.is_err());
assert!(u_bad2.is_err());
}

#[test]
fn test_iterbytes_impl_for_uuid() {
use std::collections::HashSet;
Expand Down

5 comments on commit dac9a1c

@bors
Copy link
Contributor

@bors bors commented on dac9a1c Aug 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at apoelstra@dac9a1c

@bors
Copy link
Contributor

@bors bors commented on dac9a1c Aug 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging apoelstra/rust/decode-error = dac9a1c into auto

@bors
Copy link
Contributor

@bors bors commented on dac9a1c Aug 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apoelstra/rust/decode-error = dac9a1c merged ok, testing candidate = 9f0b919

@bors
Copy link
Contributor

@bors bors commented on dac9a1c Aug 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 9f0b919

Please sign in to comment.