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

Commit 6a28a88

Browse files
committed
Merge pull request #69 from SimonSapin/overflow
Upgrade to rustc 1.0.0-nightly (fed12499e 2015-03-03) (built 2015-03-04)
2 parents 4fceb09 + a7d8612 commit 6a28a88

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/json.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,6 @@ 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.
14851484

14861485
match self.ch_or_null() {
14871486
'0' => {
@@ -1497,11 +1496,16 @@ impl<T: Iterator<Item = char>> Parser<T> {
14971496
while !self.eof() {
14981497
match self.ch_or_null() {
14991498
c @ '0' ... '9' => {
1500-
accum *= 10;
1501-
accum += (c as u64) - ('0' as u64);
1502-
1503-
// Detect overflow by comparing to the last value.
1504-
if accum <= last_accum { return self.error(InvalidNumber); }
1499+
macro_rules! try_or_invalid {
1500+
($e: expr) => {
1501+
match $e {
1502+
Some(v) => v,
1503+
None => return self.error(InvalidNumber)
1504+
}
1505+
}
1506+
}
1507+
accum = try_or_invalid!(accum.checked_mul(10));
1508+
accum = try_or_invalid!(accum.checked_add((c as u64) - ('0' as u64)));
15051509

15061510
self.bump();
15071511
}
@@ -1581,7 +1585,7 @@ impl<T: Iterator<Item = char>> Parser<T> {
15811585

15821586
fn decode_hex_escape(&mut self) -> Result<u16, ParserError> {
15831587
let mut i = 0;
1584-
let mut n = 016;
1588+
let mut n = 0;
15851589
while i < 4 && !self.eof() {
15861590
self.bump();
15871591
n = match self.ch_or_null() {
@@ -2870,6 +2874,7 @@ mod tests {
28702874
assert_eq!(Json::from_str("1e+"), Err(SyntaxError(InvalidNumber, 1, 4)));
28712875

28722876
assert_eq!(Json::from_str("18446744073709551616"), Err(SyntaxError(InvalidNumber, 1, 20)));
2877+
assert_eq!(Json::from_str("18446744073709551617"), Err(SyntaxError(InvalidNumber, 1, 20)));
28732878
assert_eq!(Json::from_str("-9223372036854775809"), Err(SyntaxError(InvalidNumber, 1, 21)));
28742879

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

0 commit comments

Comments
 (0)