Commit e902c19c0e

Matthew Borkowski <matthew.h.borkowski@gmail.com>
2021-05-13 11:11:28
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
1 parent 4f71852
Changed files (2)
lib
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" {
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 {