Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #77, integer overflow when parsing JSON scientific notation number. #80

Merged
merged 2 commits into from
Jun 8, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix #77, integer overflow when parsing JSON scientific notation number.
  • Loading branch information
ProtectedMode committed May 20, 2015
commit 745a95b60724a47d5b51e7ad0ee42e5804851fc5
16 changes: 12 additions & 4 deletions serde/src/json/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl<Iter> Deserializer<Iter>
fn parse_exponent(&mut self, mut res: f64) -> Result<f64, Error> {
try!(self.bump());

let mut exp = 0;
let mut exp: u64 = 0;
let mut neg_exp = false;

if self.ch_is(b'+') {
Expand All @@ -267,16 +267,24 @@ impl<Iter> Deserializer<Iter>
while !self.eof() {
match self.ch_or_null() {
c @ b'0' ... b'9' => {
exp *= 10;
exp += (c as i32) - (b'0' as i32);
macro_rules! try_or_invalid {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you pull this macro out to the top level since I also use it for parse_decimal?

($e: expr) => {
match $e {
Some(v) => v,
None => { return Err(self.error(ErrorCode::InvalidNumber)); }
}
}
}
exp = try_or_invalid!(exp.checked_mul(10));
exp = try_or_invalid!(exp.checked_add((c as u64) - (b'0' as u64)));

try!(self.bump());
}
_ => break
}
}

let exp: f64 = 10_f64.powi(exp);
let exp: f64 = 10_f64.powf(exp as f64);
if neg_exp {
res /= exp;
} else {
Expand Down