Skip to content

Commit

Permalink
Return EOF error on cut-off decimal number
Browse files Browse the repository at this point in the history
Fixes #524
  • Loading branch information
Yorhel committed Mar 18, 2019
1 parent e6b02d1 commit 367a1de
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,10 @@ impl<'de, R: Read<'de>> Deserializer<R> {
}

if !at_least_one_digit {
return Err(self.peek_error(ErrorCode::InvalidNumber));
match try!(self.peek()) {
Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)),
None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
}
}

match try!(self.peek_or_null()) {
Expand Down Expand Up @@ -680,7 +683,10 @@ impl<'de, R: Read<'de>> Deserializer<R> {
}

if !at_least_one_digit {
return Err(self.peek_error(ErrorCode::InvalidNumber));
match try!(self.peek()) {
Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)),
None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
}
}

match try!(self.peek_or_null()) {
Expand Down
10 changes: 10 additions & 0 deletions tests/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ fn test_json_stream_truncated() {
});
}

#[test]
fn test_json_stream_truncated_decimal() {
let data = "{\"x\":4.";

test_stream!(data, Value, |stream| {
assert!(stream.next().unwrap().unwrap_err().is_eof());
assert_eq!(stream.byte_offset(), 0);
});
}

#[test]
fn test_json_stream_empty() {
let data = "";
Expand Down
2 changes: 1 addition & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ fn test_parse_number_errors() {
("00", "invalid number at line 1 column 2"),
("0x80", "trailing characters at line 1 column 2"),
("\\0", "expected value at line 1 column 1"),
("1.", "invalid number at line 1 column 2"),
("1.", "EOF while parsing a value at line 1 column 2"),
("1.a", "invalid number at line 1 column 3"),
("1.e1", "invalid number at line 1 column 3"),
("1e", "invalid number at line 1 column 2"),
Expand Down

0 comments on commit 367a1de

Please sign in to comment.