Skip to content

Commit 69bfbfc

Browse files
author
Yorhel
committed
Return EOF error on cut-off negative sign or exponent
1 parent 367a1de commit 69bfbfc

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

src/de.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,14 @@ impl<'de, R: Read<'de>> Deserializer<R> {
380380
}
381381

382382
fn parse_integer(&mut self, positive: bool) -> Result<ParserNumber> {
383-
match try!(self.next_char_or_null()) {
383+
let next = match try!(self.next_char()) {
384+
Some(b) => b,
385+
None => {
386+
return Err(self.error(ErrorCode::EofWhileParsingValue));
387+
}
388+
};
389+
390+
match next {
384391
b'0' => {
385392
// There can be only one leading '0'.
386393
match try!(self.peek_or_null()) {
@@ -528,8 +535,15 @@ impl<'de, R: Read<'de>> Deserializer<R> {
528535
_ => true,
529536
};
530537

538+
let next = match try!(self.next_char()) {
539+
Some(b) => b,
540+
None => {
541+
return Err(self.error(ErrorCode::EofWhileParsingValue));
542+
}
543+
};
544+
531545
// Make sure a digit follows the exponent place.
532-
let mut exp = match try!(self.next_char_or_null()) {
546+
let mut exp = match next {
533547
c @ b'0'...b'9' => (c - b'0') as i32,
534548
_ => {
535549
return Err(self.error(ErrorCode::InvalidNumber));
@@ -626,19 +640,19 @@ impl<'de, R: Read<'de>> Deserializer<R> {
626640
}
627641

628642
#[cfg(feature = "arbitrary_precision")]
629-
fn scan_or_null(&mut self, buf: &mut String) -> Result<u8> {
643+
fn scan_or_eof(&mut self, buf: &mut String) -> Result<u8> {
630644
match try!(self.next_char()) {
631645
Some(b) => {
632646
buf.push(b as char);
633647
Ok(b)
634648
}
635-
None => Ok(b'\x00'),
649+
None => Err(self.error(ErrorCode::EofWhileParsingValue))
636650
}
637651
}
638652

639653
#[cfg(feature = "arbitrary_precision")]
640654
fn scan_integer(&mut self, buf: &mut String) -> Result<()> {
641-
match try!(self.scan_or_null(buf)) {
655+
match try!(self.scan_or_eof(buf)) {
642656
b'0' => {
643657
// There can be only one leading '0'.
644658
match try!(self.peek_or_null()) {
@@ -712,7 +726,7 @@ impl<'de, R: Read<'de>> Deserializer<R> {
712726
}
713727

714728
// Make sure a digit follows the exponent place.
715-
match try!(self.scan_or_null(buf)) {
729+
match try!(self.scan_or_eof(buf)) {
716730
b'0'...b'9' => {}
717731
_ => {
718732
return Err(self.error(ErrorCode::InvalidNumber));

tests/stream.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ fn test_json_stream_truncated_decimal() {
9191
});
9292
}
9393

94+
#[test]
95+
fn test_json_stream_truncated_negative() {
96+
let data = "{\"x\":-";
97+
98+
test_stream!(data, Value, |stream| {
99+
assert!(stream.next().unwrap().unwrap_err().is_eof());
100+
assert_eq!(stream.byte_offset(), 0);
101+
});
102+
}
103+
104+
#[test]
105+
fn test_json_stream_truncated_exponent() {
106+
let data = "{\"x\":4e";
107+
108+
test_stream!(data, Value, |stream| {
109+
assert!(stream.next().unwrap().unwrap_err().is_eof());
110+
assert_eq!(stream.byte_offset(), 0);
111+
});
112+
}
113+
94114
#[test]
95115
fn test_json_stream_empty() {
96116
let data = "";

tests/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -737,15 +737,15 @@ fn test_parse_number_errors() {
737737
test_parse_err::<f64>(&[
738738
("+", "expected value at line 1 column 1"),
739739
(".", "expected value at line 1 column 1"),
740-
("-", "invalid number at line 1 column 1"),
740+
("-", "EOF while parsing a value at line 1 column 1"),
741741
("00", "invalid number at line 1 column 2"),
742742
("0x80", "trailing characters at line 1 column 2"),
743743
("\\0", "expected value at line 1 column 1"),
744744
("1.", "EOF while parsing a value at line 1 column 2"),
745745
("1.a", "invalid number at line 1 column 3"),
746746
("1.e1", "invalid number at line 1 column 3"),
747-
("1e", "invalid number at line 1 column 2"),
748-
("1e+", "invalid number at line 1 column 3"),
747+
("1e", "EOF while parsing a value at line 1 column 2"),
748+
("1e+", "EOF while parsing a value at line 1 column 3"),
749749
("1a", "trailing characters at line 1 column 2"),
750750
(
751751
"100e777777777777777777777777777",

0 commit comments

Comments
 (0)