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

Unchecked integer range #224

Closed
BratSinot opened this issue Aug 23, 2021 · 4 comments
Closed

Unchecked integer range #224

BratSinot opened this issue Aug 23, 2021 · 4 comments

Comments

@BratSinot
Copy link
Contributor

Greetings!

config lib is casting all integers like explicit type conversion through as. Such behaviour by design, or this is an error?

use config::Config;

fn main() {
    let mut conf = Config::new();

    conf.merge(config::File::from_str(
        r#"
        [server]
        port = 66000
        "#,
        config::FileFormat::Toml,
    ))
    .unwrap();

    let port: u16 = conf.get("server.port").unwrap();

    println!("{}", port); // output is 464
}
@BratSinot
Copy link
Contributor Author

Maybe duplicate of this #93.

@matthiasbeyer
Copy link
Member

Yes, this is actually a limitation.

The problem is here that the format (toml in this case, but others apply here as well) does not define the datatype (besides "Number"). By specifying let port: u16 in your code, the value is inferred to be u16 and the library just assumes that the number is actually a 16 bit unsigned integer.

We cannot do much here, on the library side of things, actually (or I'm wrong and someone comes up with something clever...).

We need you to know about your data, so you have to make sure that you ask for a u32 here and all will be fine I guess.

@BratSinot
Copy link
Contributor Author

BratSinot commented Aug 23, 2021

Yes, this is actually a limitation.

The problem is here that the format (toml in this case, but others apply here as well) does not define the datatype (besides "Number"). By specifying let port: u16 in your code, the value is inferred to be u16 and the library just assumes that the number is actually a 16 bit unsigned integer.

We cannot do much here, on the library side of things, actually (or I'm wrong and someone comes up with something clever...).

We need you to know about your data, so you have to make sure that you ask for a u32 here and all will be fine I guess.

Thanks for answer!
For now I'm using this workaround:

    let port: u16 = conf
        .get_int("server.port")
        .unwrap()
        .try_into() // here
        .unwrap();

or this:

    let port = conf
        .get_string("server.port")
        .unwrap()
        .parse::<u16>()
        .unwrap();

@matthiasbeyer
Copy link
Member

Okay, I'm closing this therefore, feel free to ping me if you have more questions on the matter! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants