Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

f64::INFINITY causes panic #159

Closed
ckaran opened this issue Apr 15, 2019 · 5 comments · Fixed by #163
Closed

f64::INFINITY causes panic #159

ckaran opened this issue Apr 15, 2019 · 5 comments · Fixed by #163
Labels

Comments

@ckaran
Copy link

ckaran commented Apr 15, 2019

Problem summary

I need to be able to serialize and deserialize f64::INFINITY in my code. I believe that this should be possible, but the following code causes a panic when run:

use ron::de;
use ron::ser;
use serde::*;
use std::cmp::{Eq, PartialEq};
use std::f64;

#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)]
pub struct Test1 {
    field: f64,
}

impl PartialEq for Test1 {
    fn eq(&self, other: &Test1) -> bool {
        self.field == other.field
    }
}

impl Eq for Test1 {}

fn main() {
    let original = Test1 {
        field: f64::INFINITY,
    };

    let pretty: ser::PrettyConfig = Default::default();
    let serialized = ser::to_string_pretty(&original, pretty.clone()).unwrap();
    let duplicate: Test1 = de::from_str(&serialized).unwrap();
    assert_eq!(original, duplicate);
}

There is no panic for finite f64 values.

The panic:

$ cargo run
   Compiling serde_float_issue v0.1.0 (/home/cfkaran2/Desktop/bug_reports/serde_float_issue)
    Finished dev [unoptimized + debuginfo] target(s) in 0.49s
     Running `target/debug/serde_float_issue`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Parser(ExpectedFloat, Position { col: 12, line: 2 })', src/libcore/result.rs:997:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

Meta information:

Linux EMANE 4.15.0-47-generic #50-Ubuntu SMP Wed Mar 13 10:44:52 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.2 LTS
Release:	18.04
Codename:	bionic

rustc 1.34.0 (91856ed52 2019-04-10)
binary: rustc
commit-hash: 91856ed52c58aa5ba66a015354d1cc69e9779bdf
commit-date: 2019-04-10
host: x86_64-unknown-linux-gnu
release: 1.34.0
LLVM version: 8.0

cargo 1.34.0 (6789d8a0a 2019-04-01)
release: 1.34.0
commit-hash: 6789d8a0a54a96d95365c4e1fb01d47a5eed9937
commit-date: 2019-04-01
@torkleyy torkleyy added the bug label Apr 18, 2019
@torkleyy
Copy link
Contributor

@kvark Any idea how to handle this? Should we support infinity at all?

@torkleyy
Copy link
Contributor

Right now, it serializes to inf.

Code to use for the integration test (in case this gets implemented):

use std::f64;

use ron::{de, ser};
use serde::*;

#[derive(Copy, Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Test1 {
    field: f64,
}

#[test]
fn roundtrip_infinity() {
    let original = Test1 {
        field: f64::INFINITY,
    };

    let pretty: ser::PrettyConfig = Default::default();
    let serialized = ser::to_string_pretty(&original, pretty.clone()).unwrap();
    println!("serialized: {}", serialized);
    let duplicate: Test1 = de::from_str(&serialized).unwrap();
    assert_eq!(original, duplicate);
}

@kvark
Copy link
Collaborator

kvark commented Apr 22, 2019

@torkleyy sounds like we need special ser/deser for "inf" and "-inf".

@ckaran
Copy link
Author

ckaran commented Apr 23, 2019

@kvark @torkleyy I'd suggest making sure that RON can handle all possible floating point values; I'm not too sure what your goals for RON are, but as an end user RON sounds like python pickle, but for rust. In short, as an end user I kind of expect RON to be able to handle absolutely everything that rust can throw at it, unless there is an explicit marking that the thing can't be serialized for some reason (and I expect that to happen via serde, not via RON panicking).

@kvark
Copy link
Collaborator

kvark commented Apr 23, 2019

@ckaran thanks for the advice. It sounds good to me to follow.

bors bot added a commit that referenced this issue May 13, 2019
163: Implement inf, -inf and NaN handling r=torkleyy a=artemshein

FIxes #159

Co-authored-by: Artem Shein <artem.shein@esrlabs.com>
bors bot added a commit that referenced this issue May 26, 2019
163: Implement inf, -inf and NaN handling r=torkleyy a=artemshein

FIxes #159

Co-authored-by: Artem Shein <artem.shein@esrlabs.com>
@bors bors bot closed this as completed in #163 May 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants