diff options
| author | Robin Kruppe <robin.kruppe@gmail.com> | 2015-10-14 19:27:49 +0200 |
|---|---|---|
| committer | Robin Kruppe <robin.kruppe@gmail.com> | 2015-10-14 19:55:59 +0200 |
| commit | 71dcd7f70c084ea4f8e7788b617c9c2ea786e6b7 (patch) | |
| tree | 84b50f4a26af5f7b5a9c6e66591ae8db89938aca /src | |
| parent | e3cd87241898cd88e4348e9c6142a03d8909c4e0 (diff) | |
| download | rust-71dcd7f70c084ea4f8e7788b617c9c2ea786e6b7.tar.gz rust-71dcd7f70c084ea4f8e7788b617c9c2ea786e6b7.zip | |
Reject "+" and "-" when parsing floats.
Fixes #29042
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/num/dec2flt/parse.rs | 7 | ||||
| -rw-r--r-- | src/libcoretest/num/dec2flt/mod.rs | 12 |
2 files changed, 18 insertions, 1 deletions
diff --git a/src/libcore/num/dec2flt/parse.rs b/src/libcore/num/dec2flt/parse.rs index 58e2a6e9bba..414bcc874ea 100644 --- a/src/libcore/num/dec2flt/parse.rs +++ b/src/libcore/num/dec2flt/parse.rs @@ -59,7 +59,12 @@ pub fn parse_decimal(s: &str) -> ParseResult { let s = s.as_bytes(); let (integral, s) = eat_digits(s); match s.first() { - None => Valid(Decimal::new(integral, b"", 0)), + None => { + if integral.is_empty() { + return Invalid; // No digits at all + } + Valid(Decimal::new(integral, b"", 0)) + } Some(&b'e') | Some(&b'E') => { if integral.is_empty() { return Invalid; // No digits before 'e' diff --git a/src/libcoretest/num/dec2flt/mod.rs b/src/libcoretest/num/dec2flt/mod.rs index 51fea6e0171..0c92b2fe2a7 100644 --- a/src/libcoretest/num/dec2flt/mod.rs +++ b/src/libcoretest/num/dec2flt/mod.rs @@ -102,6 +102,18 @@ fn lonely_dot() { } #[test] +fn lonely_sign() { + assert!("+".parse::<f32>().is_err()); + assert!("-".parse::<f64>().is_err()); +} + +#[test] +fn whitespace() { + assert!(" 1.0".parse::<f32>().is_err()); + assert!("1.0 ".parse::<f64>().is_err()); +} + +#[test] fn nan() { assert!("NaN".parse::<f32>().unwrap().is_nan()); assert!("NaN".parse::<f64>().unwrap().is_nan()); |
