Skip to content
This repository has been archived by the owner on Sep 24, 2022. It is now read-only.

toml::from_str can not parse valid u64 integer. #256

Closed
fragrans opened this issue Aug 10, 2018 · 5 comments
Closed

toml::from_str can not parse valid u64 integer. #256

fragrans opened this issue Aug 10, 2018 · 5 comments
Labels
A-parsing Deserialization bug

Comments

@fragrans
Copy link

Here is the test code :

extern crate toml;

#[macro_use]
extern crate serde_derive;

#[derive(Debug, Serialize, Deserialize)]
struct T {
    i:u64
}

fn main() {
    let mut i = 1u128;
    
    loop {
        if i > std::u64::MAX as u128 { break }
        let t = T {
            i: i as u64
        };

        println!("u64: {}", t.i);
        
        let toml_s = toml::to_string(&t).unwrap();
        
        let new_t = toml::from_str::<T>(&toml_s);
        if new_t.is_err() {
            println!("{:?}", new_t);
            println!("toml_s: {}", toml_s);
            println!("t.i: {}", t.i);
            println!("max: {}", std::u64::MAX);
        }
        i = i * 10;
    }
}

Here is the result.

u64: 1
u64: 10
u64: 100
u64: 1000
u64: 10000
u64: 100000
u64: 1000000
u64: 10000000
u64: 100000000
u64: 1000000000
u64: 10000000000
u64: 100000000000
u64: 1000000000000
u64: 10000000000000
u64: 100000000000000
u64: 1000000000000000
u64: 10000000000000000
u64: 100000000000000000
u64: 1000000000000000000
u64: 10000000000000000000
Err(Error { inner: ErrorInner { kind: NumberInvalid, line: Some(0), col: 4, message: "", key: [] } })
toml_s: i = 10000000000000000000

t.i: 10000000000000000000
max: 18446744073709551615

see, 10000000000000000000 is less than u64 max: 18446744073709551615
image

Is this a bug or not?

@alexcrichton
Copy link
Collaborator

Thanks for the report! This is due to the fact that integers are signed when stored, but if you use Serde to deserialize into a u64 this should work!

@fragrans
Copy link
Author

fragrans commented Aug 23, 2018

Thanks.

@dtolnay
Copy link
Contributor

dtolnay commented Aug 23, 2018

It looks like integer deserialization always uses an i64 even if not deserializing to toml::Value. This is a bug. https://github.com/alexcrichton/toml-rs/blob/0.4.6/src/de.rs#L815

@ehuss ehuss added A-parsing Deserialization bug labels Apr 5, 2019
robbert-vdh added a commit to robbert-vdh/yabridge that referenced this issue Jul 27, 2020
The parser parses everything as signed integers so the configuration
file will fail to parse large numbers that don't fit in an i64.

toml-rs/toml-rs#256
@Lancern
Copy link

Lancern commented Apr 27, 2022

Are there any plans or WIP to solve this issue? Maybe I can help craft a patch.

@epage
Copy link
Member

epage commented Sep 23, 2022

Maintenance of this crate has moved to the https://github.com/toml-rs/toml repo. As a heads up, we plan to move toml to be on top of toml_edit, see toml-rs/toml#340.

Closing this out. If this is still a problem, feel free to recreate this issue in the new repo.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-parsing Deserialization bug
Projects
None yet
Development

No branches or pull requests

6 participants