From 3322bcd086399a658253e119323f4b4c44aa91ff Mon Sep 17 00:00:00 2001 From: Alan Du Date: Wed, 31 May 2017 00:07:15 +0100 Subject: Error when parsing numbers outside of f64's range --- src/de.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src') diff --git a/src/de.rs b/src/de.rs index bd4ead0..d5af272 100644 --- a/src/de.rs +++ b/src/de.rs @@ -893,6 +893,12 @@ impl<'a> Deserializer<'a> { } number.parse().map_err(|_e| { self.error(start, ErrorKind::NumberInvalid) + }).and_then(|n: f64| { + if n.is_finite() { + Ok(n) + } else { + Err(self.error(start, ErrorKind::NumberInvalid)) + } }) } -- cgit v1.2.3 From 2a6b365f7c931feb5e870d54c6d4a02e8aee75bf Mon Sep 17 00:00:00 2001 From: Alan Du Date: Wed, 31 May 2017 00:30:19 +0100 Subject: Error when trying to serialize invalid float --- src/ser.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/ser.rs b/src/ser.rs index 9af304f..97febb5 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -80,7 +80,7 @@ pub fn to_vec(value: &T) -> Result, Error> /// enabled: false, /// }, /// }; -/// +/// /// let toml = toml::to_string(&config).unwrap(); /// println!("{}", toml) /// } @@ -122,6 +122,9 @@ pub enum Error { /// A serialized date was invalid. DateInvalid, + /// A serialized number was invalid. + NumberInvalid, + /// None was attempted to be serialized, but it's not supported. UnsupportedNone, @@ -391,6 +394,10 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> { } fn serialize_f32(mut self, v: f32) -> Result<(), Self::Error> { + if !v.is_finite() { + return Err(Error::NumberInvalid); + } + self.emit_key("float")?; drop(write!(self.dst, "{}", v)); if v % 1.0 == 0.0 { @@ -403,6 +410,10 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> { } fn serialize_f64(mut self, v: f64) -> Result<(), Self::Error> { + if !v.is_finite() { + return Err(Error::NumberInvalid); + } + self.emit_key("float")?; drop(write!(self.dst, "{}", v)); if v % 1.0 == 0.0 { @@ -1019,7 +1030,8 @@ impl fmt::Display for Error { Error::KeyNewline => "map keys cannot contain newlines".fmt(f), Error::ArrayMixedType => "arrays cannot have mixed types".fmt(f), Error::ValueAfterTable => "values must be emitted before tables".fmt(f), - Error::DateInvalid => "a serialize date was invalid".fmt(f), + Error::DateInvalid => "a serialized date was invalid".fmt(f), + Error::NumberInvalid => "a serialized number was invalid".fmt(f), Error::UnsupportedNone => "unsupported None value".fmt(f), Error::Custom(ref s) => s.fmt(f), Error::__Nonexhaustive => panic!(), @@ -1036,6 +1048,7 @@ impl error::Error for Error { Error::ArrayMixedType => "arrays cannot have mixed types", Error::ValueAfterTable => "values must be emitted before tables", Error::DateInvalid => "a serialized date was invalid", + Error::NumberInvalid => "a serialized number was invalid", Error::UnsupportedNone => "unsupported None value", Error::Custom(_) => "custom error", Error::__Nonexhaustive => panic!(), -- cgit v1.2.3