1
Fork 0

json: Properly handle trailing comma error in object decoding.

Closes #16945
This commit is contained in:
Jonas Hietala 2014-09-15 19:12:15 +02:00
parent 1f5ee97184
commit fb299bd85f

View file

@ -234,6 +234,7 @@ pub enum ErrorCode {
KeyMustBeAString, KeyMustBeAString,
ExpectedColon, ExpectedColon,
TrailingCharacters, TrailingCharacters,
TrailingComma,
InvalidEscape, InvalidEscape,
InvalidUnicodeCodePoint, InvalidUnicodeCodePoint,
LoneLeadingSurrogateInHexEscape, LoneLeadingSurrogateInHexEscape,
@ -274,6 +275,7 @@ pub fn error_str(error: ErrorCode) -> &'static str {
KeyMustBeAString => "key must be a string", KeyMustBeAString => "key must be a string",
ExpectedColon => "expected `:`", ExpectedColon => "expected `:`",
TrailingCharacters => "trailing characters", TrailingCharacters => "trailing characters",
TrailingComma => "trailing comma",
InvalidEscape => "invalid escape", InvalidEscape => "invalid escape",
UnrecognizedHex => "invalid \\u escape (unrecognized hex)", UnrecognizedHex => "invalid \\u escape (unrecognized hex)",
NotFourDigit => "invalid \\u escape (not four digits)", NotFourDigit => "invalid \\u escape (not four digits)",
@ -1681,8 +1683,12 @@ impl<T: Iterator<char>> Parser<T> {
fn parse_object(&mut self, first: bool) -> JsonEvent { fn parse_object(&mut self, first: bool) -> JsonEvent {
if self.ch_is('}') { if self.ch_is('}') {
if !first { if !first {
if self.stack.is_empty() {
return self.error_event(TrailingComma);
} else {
self.stack.pop(); self.stack.pop();
} }
}
if self.stack.is_empty() { if self.stack.is_empty() {
self.state = ParseBeforeFinish; self.state = ParseBeforeFinish;
} else { } else {
@ -2377,7 +2383,7 @@ mod tests {
F64Value, StringValue, NullValue, SyntaxError, Key, Index, Stack, F64Value, StringValue, NullValue, SyntaxError, Key, Index, Stack,
InvalidSyntax, InvalidNumber, EOFWhileParsingObject, EOFWhileParsingList, InvalidSyntax, InvalidNumber, EOFWhileParsingObject, EOFWhileParsingList,
EOFWhileParsingValue, EOFWhileParsingString, KeyMustBeAString, ExpectedColon, EOFWhileParsingValue, EOFWhileParsingString, KeyMustBeAString, ExpectedColon,
TrailingCharacters}; TrailingCharacters, TrailingComma};
use std::{i64, u64, f32, f64, io}; use std::{i64, u64, f32, f64, io};
use std::collections::TreeMap; use std::collections::TreeMap;
@ -3379,6 +3385,7 @@ mod tests {
} }
} }
} }
#[test] #[test]
#[ignore(cfg(target_word_size = "32"))] // FIXME(#14064) #[ignore(cfg(target_word_size = "32"))] // FIXME(#14064)
fn test_read_object_streaming() { fn test_read_object_streaming() {
@ -3393,6 +3400,7 @@ mod tests {
assert_eq!(last_event("{\"a\":1"), Error(SyntaxError(EOFWhileParsingObject, 1, 7))); assert_eq!(last_event("{\"a\":1"), Error(SyntaxError(EOFWhileParsingObject, 1, 7)));
assert_eq!(last_event("{\"a\":1 1"), Error(SyntaxError(InvalidSyntax, 1, 8))); assert_eq!(last_event("{\"a\":1 1"), Error(SyntaxError(InvalidSyntax, 1, 8)));
assert_eq!(last_event("{\"a\":1,"), Error(SyntaxError(EOFWhileParsingObject, 1, 8))); assert_eq!(last_event("{\"a\":1,"), Error(SyntaxError(EOFWhileParsingObject, 1, 8)));
assert_eq!(last_event("{\"a\":1,}"), Error(SyntaxError(TrailingComma, 1, 8)));
assert_stream_equal( assert_stream_equal(
"{}", "{}",