Return EOF error on cut-off negative sign or exponent

This commit is contained in:
Yorhel 2019-03-19 10:10:27 +01:00
parent 367a1de69e
commit 69bfbfc71f
3 changed files with 43 additions and 9 deletions

View File

@ -380,7 +380,14 @@ impl<'de, R: Read<'de>> Deserializer<R> {
}
fn parse_integer(&mut self, positive: bool) -> Result<ParserNumber> {
match try!(self.next_char_or_null()) {
let next = match try!(self.next_char()) {
Some(b) => b,
None => {
return Err(self.error(ErrorCode::EofWhileParsingValue));
}
};
match next {
b'0' => {
// There can be only one leading '0'.
match try!(self.peek_or_null()) {
@ -528,8 +535,15 @@ impl<'de, R: Read<'de>> Deserializer<R> {
_ => true,
};
let next = match try!(self.next_char()) {
Some(b) => b,
None => {
return Err(self.error(ErrorCode::EofWhileParsingValue));
}
};
// Make sure a digit follows the exponent place.
let mut exp = match try!(self.next_char_or_null()) {
let mut exp = match next {
c @ b'0'...b'9' => (c - b'0') as i32,
_ => {
return Err(self.error(ErrorCode::InvalidNumber));
@ -626,19 +640,19 @@ impl<'de, R: Read<'de>> Deserializer<R> {
}
#[cfg(feature = "arbitrary_precision")]
fn scan_or_null(&mut self, buf: &mut String) -> Result<u8> {
fn scan_or_eof(&mut self, buf: &mut String) -> Result<u8> {
match try!(self.next_char()) {
Some(b) => {
buf.push(b as char);
Ok(b)
}
None => Ok(b'\x00'),
None => Err(self.error(ErrorCode::EofWhileParsingValue))
}
}
#[cfg(feature = "arbitrary_precision")]
fn scan_integer(&mut self, buf: &mut String) -> Result<()> {
match try!(self.scan_or_null(buf)) {
match try!(self.scan_or_eof(buf)) {
b'0' => {
// There can be only one leading '0'.
match try!(self.peek_or_null()) {
@ -712,7 +726,7 @@ impl<'de, R: Read<'de>> Deserializer<R> {
}
// Make sure a digit follows the exponent place.
match try!(self.scan_or_null(buf)) {
match try!(self.scan_or_eof(buf)) {
b'0'...b'9' => {}
_ => {
return Err(self.error(ErrorCode::InvalidNumber));

View File

@ -91,6 +91,26 @@ fn test_json_stream_truncated_decimal() {
});
}
#[test]
fn test_json_stream_truncated_negative() {
let data = "{\"x\":-";
test_stream!(data, Value, |stream| {
assert!(stream.next().unwrap().unwrap_err().is_eof());
assert_eq!(stream.byte_offset(), 0);
});
}
#[test]
fn test_json_stream_truncated_exponent() {
let data = "{\"x\":4e";
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 = "";

View File

@ -737,15 +737,15 @@ fn test_parse_number_errors() {
test_parse_err::<f64>(&[
("+", "expected value at line 1 column 1"),
(".", "expected value at line 1 column 1"),
("-", "invalid number at line 1 column 1"),
("-", "EOF while parsing a value at line 1 column 1"),
("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.", "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"),
("1e+", "invalid number at line 1 column 3"),
("1e", "EOF while parsing a value at line 1 column 2"),
("1e+", "EOF while parsing a value at line 1 column 3"),
("1a", "trailing characters at line 1 column 2"),
(
"100e777777777777777777777777777",