Skip to content

Commit

Permalink
Require at least one digit after decimal point
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 22, 2022
1 parent c27b023 commit 9d94e92
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/de.rs
Expand Up @@ -451,30 +451,33 @@ impl<'de, R: Read<'de>> Deserializer<R> {
&mut self,
positive: bool,
mut significand: u64,
mut exponent: i32,
exponent_before_decimal_point: i32,
) -> Result<f64> {
self.eat_char();

let mut exponent_after_decimal_point = 0;
while let c @ b'0'..=b'9' = tri!(self.peek_or_null()) {
let digit = (c - b'0') as u64;

if overflow!(significand * 10 + digit, u64::max_value()) {
let exponent = exponent_before_decimal_point + exponent_after_decimal_point;
return self.parse_decimal_overflow(positive, significand, exponent);
}

self.eat_char();
significand = significand * 10 + digit;
exponent -= 1;
exponent_after_decimal_point -= 1;
}

// Error if there is not at least one digit after the decimal point.
if exponent == 0 {
if exponent_after_decimal_point == 0 {
match tri!(self.peek()) {
Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)),
None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
}
}

let exponent = exponent_before_decimal_point + exponent_after_decimal_point;
match tri!(self.peek_or_null()) {
b'e' | b'E' => self.parse_exponent(positive, significand, exponent),
_ => self.f64_from_parts(positive, significand, exponent),
Expand Down

0 comments on commit 9d94e92

Please sign in to comment.