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 {
623623
624624 .ObjectSeparator => switch (c) {
625625 ':' => {
626 p.state = .ValueBegin;
626 p.state = .ValueBeginNoClosing;
627627 p.after_string_state = .ValueEnd;
628628 },
629629 0x09, 0x0A, 0x0D, 0x20 => {
......@@ -1205,6 +1205,13 @@ test "json.token mismatched close" {
12051205 try testing.expectError(error.UnexpectedClosingBrace, p.next());
12061206}
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
12081215/// Validate a JSON string. This does not limit number precision so a decoder may not necessarily
12091216/// be able to decode the string even if this returns true.
12101217pub fn validate(s: []const u8) bool {
lib/std/json/test.zig+6
......@@ -76,6 +76,12 @@ test "y_trailing_comma_after_empty" {
7676 );
7777}
7878
79test "n_object_closed_missing_value" {
80 try err(
81 \\{"a":}
82 );
83}
84
7985////////////////////////////////////////////////////////////////////////////////////////////////////
8086
8187test "y_array_arraysWithSpaces" {