Skip to content

filter for float finiteness in proc_macro2 literal construction #895

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

Merged
merged 1 commit into from Sep 6, 2020
Merged

filter for float finiteness in proc_macro2 literal construction #895

merged 1 commit into from Sep 6, 2020

Conversation

sameer
Copy link
Contributor

@sameer sameer commented Sep 6, 2020

Ran into another panic while fuzzing:

fn a() {
    X.-0.9E999999
}

panics with thread 'main' panicked at 'assertion failed: f.is_finite()', /home/purisame/.cargo/registry/src/github.com-1ecc6299db9ec823/proc-macro2-1.0.19/src/lib.rs:1073:9

but:

fn a() {
    -0.9E999999
}

does not. Syn is calling a function that panics for non-finite floats when parsing expressions, so I've filtered those out.

Parsing non-finite floats

This also raises some other questions -- why does parsing a non-finite float work in the first place?

                                        expr: Lit(
                                            ExprLit {
                                                attrs: [],
                                                lit: Float(
                                                    LitFloat {
                                                        token: 0.9E999999,
                                                    },
                                                ),
                                            },
                                        ),

If we ever have f128, f80, etc. in Rust I think this behavior is fine. In which case, should the other_suffix case should fall back to

            let stream = repr.parse::<TokenStream>().unwrap();
            match stream.into_iter().next().unwrap() {
                TokenTree::Literal(l) => Some(l),
                _ => unreachable!(),
            }

if parsing returns an error?

@dtolnay dtolnay merged commit 4b73bb6 into dtolnay:master Sep 6, 2020
@sameer sameer deleted the finite_floats branch September 6, 2020 17:39
@dtolnay
Copy link
Owner

dtolnay commented Sep 6, 2020

why does parsing a non-finite float work?

0.9E999999 isn't non-finite. Someone making an arbitrary precision macro like apfloat!(0.9E999999) would need the value.

other_suffix case should fall back to

TokenStream::from_str can't make negative literal tokens.

@sameer
Copy link
Contributor Author

sameer commented Sep 6, 2020

Ah ok, that makes sense. Thanks for explaining! 👍

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

Successfully merging this pull request may close these issues.

None yet

2 participants