aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2017-05-30 19:37:44 -0500
committerGitHub <noreply@github.com>2017-05-30 19:37:44 -0500
commit95e1c738467c87f4072ac29923e7df399ebfe9ea (patch)
tree0dc8202f51434c0179ea6cf9ea404caecb570741
parentdfe67d6c60ab96e80d02747f60d0635acc67f718 (diff)
parent2a6b365f7c931feb5e870d54c6d4a02e8aee75bf (diff)
downloadmilf-rs-95e1c738467c87f4072ac29923e7df399ebfe9ea.tar.gz
milf-rs-95e1c738467c87f4072ac29923e7df399ebfe9ea.zip
Merge pull request #183 from alanhdu/invalid_number
Better invalid number handling
-rw-r--r--src/de.rs6
-rw-r--r--src/ser.rs17
-rw-r--r--tests/invalid-encoder-misc.rs14
-rw-r--r--tests/invalid-misc.rs4
4 files changed, 39 insertions, 2 deletions
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))
+ }
})
}
diff --git a/src/ser.rs b/src/ser.rs
index de5d41c..22eda81 100644
--- a/src/ser.rs
+++ b/src/ser.rs
@@ -80,7 +80,7 @@ pub fn to_vec<T: ?Sized>(value: &T) -> Result<Vec<u8>, 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!(),
diff --git a/tests/invalid-encoder-misc.rs b/tests/invalid-encoder-misc.rs
new file mode 100644
index 0000000..272f58f
--- /dev/null
+++ b/tests/invalid-encoder-misc.rs
@@ -0,0 +1,14 @@
+extern crate toml;
+
+use std::f64;
+
+#[test]
+fn test_invalid_float_encode() {
+ fn bad(value: toml::Value) {
+ assert!(toml::to_string(&value).is_err());
+ }
+
+ bad(toml::Value::Float(f64::INFINITY));
+ bad(toml::Value::Float(f64::NEG_INFINITY));
+ bad(toml::Value::Float(f64::NAN));
+}
diff --git a/tests/invalid-misc.rs b/tests/invalid-misc.rs
index 53f625e..bb70b97 100644
--- a/tests/invalid-misc.rs
+++ b/tests/invalid-misc.rs
@@ -10,4 +10,8 @@ fn bad() {
bad("a = 1__1");
bad("a = 1_");
bad("''");
+ bad("a = nan");
+ bad("a = -inf");
+ bad("a = inf");
+ bad("a = 9e99999");
}