From fd9fbe26a1fb0aecafaf249a48bf5644a5e175b2 Mon Sep 17 00:00:00 2001 From: tonis2 Date: Thu, 7 Aug 2025 09:07:55 +0300 Subject: [PATCH] JSON parser trailing issue fix --- .gitignore | 2 +- lib/std/encoding/json.c3 | 10 +-- test/unit/stdlib/encoding/json.c3 | 111 ------------------------------ 3 files changed, 2 insertions(+), 121 deletions(-) diff --git a/.gitignore b/.gitignore index 0e5a5bb63..92f990597 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ # Prerequisites *.d - +testrun # Object files *.o *.ko diff --git a/lib/std/encoding/json.c3 b/lib/std/encoding/json.c3 index 8b2f9a7bf..75a6c18ae 100644 --- a/lib/std/encoding/json.c3 +++ b/lib/std/encoding/json.c3 @@ -26,15 +26,7 @@ fn Object*? parse(Allocator allocator, InStream s) JsonContext context = { .last_string = dstring::new_with_capacity(smem, 64), .stream = s, .allocator = allocator }; @pool() { - Object* o = parse_any(&context)!; - defer catch o.free(); - while (char c = read_next(&context)!, c != 0) - { - if (c.is_space()) continue; - return UNEXPECTED_CHARACTER?; - } - if (!@catch(context.stream.read_byte())) return UNEXPECTED_CHARACTER?; - return o; + return parse_any(&context)!; }; }; } diff --git a/test/unit/stdlib/encoding/json.c3 b/test/unit/stdlib/encoding/json.c3 index 29b563289..e1f50227f 100644 --- a/test/unit/stdlib/encoding/json.c3 +++ b/test/unit/stdlib/encoding/json.c3 @@ -221,79 +221,6 @@ fn void test_invalid_numbers() } } -fn void test_trailing_characters() -{ - // Test invalid: trailing characters after string - if (catch err = json::parse_string(mem, `""x`)) - { - assert(err == json::UNEXPECTED_CHARACTER); - } - else - { - assert(false, "Expected UNEXPECTED_CHARACTER error for \"\"x"); - } - - // Test invalid: trailing characters after number - if (catch err = json::parse_string(mem, "123abc")) - { - assert(err == json::UNEXPECTED_CHARACTER); - } - else - { - assert(false, "Expected UNEXPECTED_CHARACTER error for 123abc"); - } - - // Test invalid: trailing characters after boolean - if (catch err = json::parse_string(mem, "truex")) - { - assert(err == json::UNEXPECTED_CHARACTER); - } - else - { - assert(false, "Expected UNEXPECTED_CHARACTER error for truex"); - } - - // Test invalid: trailing characters after null - if (catch err = json::parse_string(mem, "nullx")) - { - assert(err == json::UNEXPECTED_CHARACTER); - } - else - { - assert(false, "Expected UNEXPECTED_CHARACTER error for nullx"); - } - - // Test invalid: trailing characters after array - if (catch err = json::parse_string(mem, "[1,2,3]x")) - { - assert(err == json::UNEXPECTED_CHARACTER); - } - else - { - assert(false, "Expected UNEXPECTED_CHARACTER error for [1,2,3]x"); - } - - // Test invalid: trailing characters after object - if (catch err = json::parse_string(mem, `{"a":1}x`)) - { - assert(err == json::UNEXPECTED_CHARACTER); - } - else - { - assert(false, "Expected UNEXPECTED_CHARACTER error for {\"a\":1}x"); - } - - // Test valid: trailing whitespace should be allowed - Object* o1 = json::parse_string(mem, `"" `)!!; - defer o1.free(); - assert(o1.s == ""); - - // Test valid: trailing newlines and tabs should be allowed - Object* o2 = json::parse_string(mem, "123\n\t ")!!; - defer o2.free(); - assert(o2.f == 123.0); -} - fn void test_depth_limit() { // Test that deeply nested arrays with 150 opening brackets exceed the depth limit of 128 @@ -311,26 +238,6 @@ fn void test_depth_limit() fn void test_comments_rejected() { - // Test the specific case: {"a":"b"}// - if (catch err = json::parse_string(mem, `{"a":"b"}//comment`)) - { - // Should fail with some error - comments are not valid JSON - } - else - { - assert(false, "Expected error for {\"a\":\"b\"}// but parsing succeeded"); - } - - // Test that comments are rejected (JSON spec doesn't allow comments) - if (catch err = json::parse_string(mem, `{"a":"b"}//`)) - { - // Should fail with some error - comments are not valid JSON - } - else - { - assert(false, "Expected error for JSON with comments but parsing succeeded"); - } - // Test that just a slash is rejected if (catch err = json::parse_string(mem, "/")) { @@ -341,21 +248,3 @@ fn void test_comments_rejected() assert(false, "Expected error for single slash but parsing succeeded"); } } - -fn void test_null_bytes_rejected() -{ - // Test that null bytes in JSON are rejected (like n_multidigit_number_then_00.json) - // This simulates "123" + null byte + newline - char[5] json_with_null = { '1', '2', '3', 0, '\n' }; - ByteReader reader; - reader.init(json_with_null[..]); - - if (catch err = json::parse(mem, &reader)) - { - assert(err == json::UNEXPECTED_CHARACTER); - } - else - { - assert(false, "Expected UNEXPECTED_CHARACTER error for JSON with null byte"); - } -}