Skip to content

Commit

Permalink
Reject "+" and "-" when parsing floats.
Browse files Browse the repository at this point in the history
Fixes #29042
  • Loading branch information
Robin Kruppe committed Oct 14, 2015
1 parent e3cd872 commit 71dcd7f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/libcore/num/dec2flt/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
12 changes: 12 additions & 0 deletions src/libcoretest/num/dec2flt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ fn lonely_dot() {
assert_eq!(".".parse(), Ok(0.0));
}

#[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());
Expand Down

0 comments on commit 71dcd7f

Please sign in to comment.