diff options
| author | Edward Barnard | 2015-12-25 01:16:23 +0000 |
|---|---|---|
| committer | Edward Barnard | 2015-12-25 10:58:40 +0000 |
| commit | 35ee8e80f25d852620061fc6e9627485467d12ea (patch) | |
| tree | 0cc6071d933548fad8f2c2c25012c1a2c7f34ec0 /src/lib.rs | |
| parent | bce38e77de935d1d06ab220dacc8870a3362b80a (diff) | |
| download | rust-plist-35ee8e80f25d852620061fc6e9627485467d12ea.tar.bz2 | |
Better handling of u64 to usize conversions
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 17 |
1 files changed, 17 insertions, 0 deletions
@@ -157,6 +157,8 @@ pub enum PlistEvent { StartPlist, EndPlist, + // While the length of an array or dict cannot be feasably greater than max(usize) this better + // conveys the concept of an effectively unbounded event stream. StartArray(Option<u64>), EndArray, @@ -286,3 +288,18 @@ impl<R: Read + Seek> Iterator for EventReader<R> { pub trait EventWriter { fn write(&mut self, event: &PlistEvent) -> Result<()>; } + +fn u64_to_usize(len_u64: u64) -> Result<usize> { + let len = len_u64 as usize; + if len as u64 != len_u64 { + return Err(Error::InvalidData); // Too long + } + Ok(len) +} + +fn u64_option_to_usize(len: Option<u64>) -> Result<Option<usize>> { + match len { + Some(len) => Ok(Some(try!(u64_to_usize(len)))), + None => Ok(None), + } +} |
