Skip to content

Commit 0b89836

Browse files
authored
Merge pull request #956 from dtolnay/decimal
Require at least one digit after decimal point
2 parents 586fefb + 9d94e92 commit 0b89836

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/de.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,30 +451,33 @@ impl<'de, R: Read<'de>> Deserializer<R> {
451451
&mut self,
452452
positive: bool,
453453
mut significand: u64,
454-
mut exponent: i32,
454+
exponent_before_decimal_point: i32,
455455
) -> Result<f64> {
456456
self.eat_char();
457457

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

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

465467
self.eat_char();
466468
significand = significand * 10 + digit;
467-
exponent -= 1;
469+
exponent_after_decimal_point -= 1;
468470
}
469471

470472
// Error if there is not at least one digit after the decimal point.
471-
if exponent == 0 {
473+
if exponent_after_decimal_point == 0 {
472474
match tri!(self.peek()) {
473475
Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)),
474476
None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
475477
}
476478
}
477479

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

tests/regression/issue953.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use serde_json::Value;
2+
3+
#[test]
4+
fn test() {
5+
let x1 = serde_json::from_str::<Value>("18446744073709551615.");
6+
assert!(x1.is_err());
7+
let x2 = serde_json::from_str::<Value>("18446744073709551616.");
8+
assert!(x2.is_err());
9+
}

0 commit comments

Comments
 (0)