Error when trying to serialize invalid float
This commit is contained in:
parent
90040b34b6
commit
87f51b38d1
17
src/ser.rs
17
src/ser.rs
|
@ -80,7 +80,7 @@ pub fn to_vec<T: ?Sized>(value: &T) -> Result<Vec<u8>, Error>
|
||||||
/// enabled: false,
|
/// enabled: false,
|
||||||
/// },
|
/// },
|
||||||
/// };
|
/// };
|
||||||
///
|
///
|
||||||
/// let toml = toml::to_string(&config).unwrap();
|
/// let toml = toml::to_string(&config).unwrap();
|
||||||
/// println!("{}", toml)
|
/// println!("{}", toml)
|
||||||
/// }
|
/// }
|
||||||
|
@ -122,6 +122,9 @@ pub enum Error {
|
||||||
/// A serialized date was invalid.
|
/// A serialized date was invalid.
|
||||||
DateInvalid,
|
DateInvalid,
|
||||||
|
|
||||||
|
/// A serialized number was invalid.
|
||||||
|
NumberInvalid,
|
||||||
|
|
||||||
/// None was attempted to be serialized, but it's not supported.
|
/// None was attempted to be serialized, but it's not supported.
|
||||||
UnsupportedNone,
|
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> {
|
fn serialize_f32(mut self, v: f32) -> Result<(), Self::Error> {
|
||||||
|
if !v.is_finite() {
|
||||||
|
return Err(Error::NumberInvalid);
|
||||||
|
}
|
||||||
|
|
||||||
self.emit_key("float")?;
|
self.emit_key("float")?;
|
||||||
drop(write!(self.dst, "{}", v));
|
drop(write!(self.dst, "{}", v));
|
||||||
if v % 1.0 == 0.0 {
|
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> {
|
fn serialize_f64(mut self, v: f64) -> Result<(), Self::Error> {
|
||||||
|
if !v.is_finite() {
|
||||||
|
return Err(Error::NumberInvalid);
|
||||||
|
}
|
||||||
|
|
||||||
self.emit_key("float")?;
|
self.emit_key("float")?;
|
||||||
drop(write!(self.dst, "{}", v));
|
drop(write!(self.dst, "{}", v));
|
||||||
if v % 1.0 == 0.0 {
|
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::KeyNewline => "map keys cannot contain newlines".fmt(f),
|
||||||
Error::ArrayMixedType => "arrays cannot have mixed types".fmt(f),
|
Error::ArrayMixedType => "arrays cannot have mixed types".fmt(f),
|
||||||
Error::ValueAfterTable => "values must be emitted before tables".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::UnsupportedNone => "unsupported None value".fmt(f),
|
||||||
Error::Custom(ref s) => s.fmt(f),
|
Error::Custom(ref s) => s.fmt(f),
|
||||||
Error::__Nonexhaustive => panic!(),
|
Error::__Nonexhaustive => panic!(),
|
||||||
|
@ -1036,6 +1048,7 @@ impl error::Error for Error {
|
||||||
Error::ArrayMixedType => "arrays cannot have mixed types",
|
Error::ArrayMixedType => "arrays cannot have mixed types",
|
||||||
Error::ValueAfterTable => "values must be emitted before tables",
|
Error::ValueAfterTable => "values must be emitted before tables",
|
||||||
Error::DateInvalid => "a serialized date was invalid",
|
Error::DateInvalid => "a serialized date was invalid",
|
||||||
|
Error::NumberInvalid => "a serialized number was invalid",
|
||||||
Error::UnsupportedNone => "unsupported None value",
|
Error::UnsupportedNone => "unsupported None value",
|
||||||
Error::Custom(_) => "custom error",
|
Error::Custom(_) => "custom error",
|
||||||
Error::__Nonexhaustive => panic!(),
|
Error::__Nonexhaustive => panic!(),
|
||||||
|
|
14
tests/invalid-encoder-misc.rs
Normal file
14
tests/invalid-encoder-misc.rs
Normal file
|
@ -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));
|
||||||
|
}
|
Loading…
Reference in a new issue