authorgravatar for matthew.h.borkowski@gmail.comMatthew Borkowski <matthew.h.borkowski@gmail.com> 2021-05-13 05:11:28-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-05-13 11:11:28+02:00
loge902c19c0e2bf7f0e9bc83b2c58d19ec024d56db
treede1f648b1339d0c01e799d9d40e7fb602b282b8a
parent4f71852c103f8dbf6ce78b36e5abd41a0c4ccf06
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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

2 files changed, 14 insertions(+), 1 deletions(-)

lib/std/json.zig+8-1
...@@ -623,7 +623,7 @@ pub const StreamingParser = struct {...@@ -623,7 +623,7 @@ pub const StreamingParser = struct {
623623
624 .ObjectSeparator => switch (c) {624 .ObjectSeparator => switch (c) {
625 ':' => {625 ':' => {
626 p.state = .ValueBegin;626 p.state = .ValueBeginNoClosing;
627 p.after_string_state = .ValueEnd;627 p.after_string_state = .ValueEnd;
628 },628 },
629 0x09, 0x0A, 0x0D, 0x20 => {629 0x09, 0x0A, 0x0D, 0x20 => {
...@@ -1205,6 +1205,13 @@ test "json.token mismatched close" {...@@ -1205,6 +1205,13 @@ test "json.token mismatched close" {
1205 try testing.expectError(error.UnexpectedClosingBrace, p.next());1205 try testing.expectError(error.UnexpectedClosingBrace, p.next());
1206}1206}
12071207
1208test "json.token premature object close" {
1209 var p = TokenStream.init("{ \"key\": }");
1210 try checkNext(&p, .ObjectBegin);
1211 try checkNext(&p, .String);
1212 try testing.expectError(error.InvalidValueBegin, p.next());
1213}
1214
1208/// Validate a JSON string. This does not limit number precision so a decoder may not necessarily1215/// Validate a JSON string. This does not limit number precision so a decoder may not necessarily
1209/// be able to decode the string even if this returns true.1216/// be able to decode the string even if this returns true.
1210pub fn validate(s: []const u8) bool {1217pub fn validate(s: []const u8) bool {
lib/std/json/test.zig+6
...@@ -76,6 +76,12 @@ test "y_trailing_comma_after_empty" {...@@ -76,6 +76,12 @@ test "y_trailing_comma_after_empty" {
76 );76 );
77}77}
7878
79test "n_object_closed_missing_value" {
80 try err(
81 \\{"a":}
82 );
83}
84
79////////////////////////////////////////////////////////////////////////////////////////////////////85////////////////////////////////////////////////////////////////////////////////////////////////////
8086
81test "y_array_arraysWithSpaces" {87test "y_array_arraysWithSpaces" {