From e902c19c0e2bf7f0e9bc83b2c58d19ec024d56db Mon Sep 17 00:00:00 2001 From: Matthew Borkowski Date: Thu, 13 May 2021 05:11:28 -0400 Subject: [PATCH] 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 --- lib/std/json.zig | 9 ++++++++- lib/std/json/test.zig | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/std/json.zig b/lib/std/json.zig index ed26c13229..7515f8682d 100644 --- a/lib/std/json.zig +++ b/lib/std/json.zig @@ -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 { diff --git a/lib/std/json/test.zig b/lib/std/json/test.zig index e37ba72113..027f6acaca 100644 --- a/lib/std/json/test.zig +++ b/lib/std/json/test.zig @@ -76,6 +76,12 @@ test "y_trailing_comma_after_empty" { ); } +test "n_object_closed_missing_value" { + try err( + \\{"a":} + ); +} + //////////////////////////////////////////////////////////////////////////////////////////////////// test "y_array_arraysWithSpaces" {