Skip to content

Commit 367a1de

Browse files
author
Yorhel
committed
Return EOF error on cut-off decimal number
Fixes #524
1 parent e6b02d1 commit 367a1de

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/de.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,10 @@ impl<'de, R: Read<'de>> Deserializer<R> {
496496
}
497497

498498
if !at_least_one_digit {
499-
return Err(self.peek_error(ErrorCode::InvalidNumber));
499+
match try!(self.peek()) {
500+
Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)),
501+
None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
502+
}
500503
}
501504

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

682685
if !at_least_one_digit {
683-
return Err(self.peek_error(ErrorCode::InvalidNumber));
686+
match try!(self.peek()) {
687+
Some(_) => return Err(self.peek_error(ErrorCode::InvalidNumber)),
688+
None => return Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
689+
}
684690
}
685691

686692
match try!(self.peek_or_null()) {

tests/stream.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ fn test_json_stream_truncated() {
8181
});
8282
}
8383

84+
#[test]
85+
fn test_json_stream_truncated_decimal() {
86+
let data = "{\"x\":4.";
87+
88+
test_stream!(data, Value, |stream| {
89+
assert!(stream.next().unwrap().unwrap_err().is_eof());
90+
assert_eq!(stream.byte_offset(), 0);
91+
});
92+
}
93+
8494
#[test]
8595
fn test_json_stream_empty() {
8696
let data = "";

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ fn test_parse_number_errors() {
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"),
744-
("1.", "invalid number at line 1 column 2"),
744+
("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"),
747747
("1e", "invalid number at line 1 column 2"),

0 commit comments

Comments
 (0)