Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.

Commit f184947

Browse files
committed
Fix integer overflow detection.
The previous code only detected overflow to exactly zero (`u64::MAX + 1`). Tested on `multirust override nightly-2015-03-01` (before rust-lang/rust#22532)
1 parent 4fceb09 commit f184947

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/json.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,7 @@ impl<T: Iterator<Item = char>> Parser<T> {
14811481

14821482
fn parse_u64(&mut self) -> Result<u64, ParserError> {
14831483
let mut accum = 0;
1484-
let last_accum = 0; // necessary to detect overflow.
1484+
let mut last_accum = 0; // necessary to detect overflow.
14851485

14861486
match self.ch_or_null() {
14871487
'0' => {
@@ -1502,6 +1502,7 @@ impl<T: Iterator<Item = char>> Parser<T> {
15021502

15031503
// Detect overflow by comparing to the last value.
15041504
if accum <= last_accum { return self.error(InvalidNumber); }
1505+
last_accum = accum;
15051506

15061507
self.bump();
15071508
}
@@ -2870,6 +2871,7 @@ mod tests {
28702871
assert_eq!(Json::from_str("1e+"), Err(SyntaxError(InvalidNumber, 1, 4)));
28712872

28722873
assert_eq!(Json::from_str("18446744073709551616"), Err(SyntaxError(InvalidNumber, 1, 20)));
2874+
assert_eq!(Json::from_str("18446744073709551617"), Err(SyntaxError(InvalidNumber, 1, 20)));
28732875
assert_eq!(Json::from_str("-9223372036854775809"), Err(SyntaxError(InvalidNumber, 1, 21)));
28742876

28752877
assert_eq!(Json::from_str("3"), Ok(U64(3)));

0 commit comments

Comments
 (0)