Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libserialize: add new error() to Decoder for Decodable objects to signal parse errors #16130

Merged
merged 2 commits into from
Aug 1, 2014
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
libuuid: use Decoder::error() rather than failing on bad decode
  • Loading branch information
apoelstra committed Aug 1, 2014
commit dac9a1c5207cb33a0b40813896b74d00bbbd1d36
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