std/json: Fix premature closing brace being considered valid JSON

return error from StreamingParser when reading closing brace when expecting value for an object key
This commit is contained in:
Matthew Borkowski 2021-05-13 05:11:28 -04:00 committed by GitHub
parent 4f71852c10
commit e902c19c0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -623,7 +623,7 @@ pub const StreamingParser = struct {
.ObjectSeparator => switch (c) {
':' => {
p.state = .ValueBegin;
p.state = .ValueBeginNoClosing;
p.after_string_state = .ValueEnd;
},
0x09, 0x0A, 0x0D, 0x20 => {
@ -1205,6 +1205,13 @@ test "json.token mismatched close" {
try testing.expectError(error.UnexpectedClosingBrace, p.next());
}
test "json.token premature object close" {
var p = TokenStream.init("{ \"key\": }");
try checkNext(&p, .ObjectBegin);
try checkNext(&p, .String);
try testing.expectError(error.InvalidValueBegin, p.next());
}
/// Validate a JSON string. This does not limit number precision so a decoder may not necessarily
/// be able to decode the string even if this returns true.
pub fn validate(s: []const u8) bool {

View File

@ -76,6 +76,12 @@ test "y_trailing_comma_after_empty" {
);
}
test "n_object_closed_missing_value" {
try err(
\\{"a":}
);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
test "y_array_arraysWithSpaces" {