aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
authorEdward Barnard2015-12-25 01:16:23 +0000
committerEdward Barnard2015-12-25 10:58:40 +0000
commit35ee8e80f25d852620061fc6e9627485467d12ea (patch)
tree0cc6071d933548fad8f2c2c25012c1a2c7f34ec0 /src/lib.rs
parentbce38e77de935d1d06ab220dacc8870a3362b80a (diff)
downloadrust-plist-35ee8e80f25d852620061fc6e9627485467d12ea.tar.bz2
Better handling of u64 to usize conversions
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 5f3275f..def6bfd 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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),
+ }
+}