From 09c0fb72ca2fa45999f690c7b5e88e7fc73ad1c7 Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Wed, 15 Jul 2026 12:30:05 -0400 Subject: [PATCH 1/3] Handle null parser in WeaselJsonParser_parse Closes #51 --- include/weaseljson.h | 2 ++ src/lib.cpp | 3 +++ src/test.cpp | 5 +++++ src/validate.cpp | 3 +++ 4 files changed, 13 insertions(+) diff --git a/include/weaseljson.h b/include/weaseljson.h index 4a42681..211dd1b 100644 --- a/include/weaseljson.h +++ b/include/weaseljson.h @@ -38,6 +38,8 @@ enum WeaselJsonStatus { WeaselJson_REJECT, /** json is too deeply nested */ WeaselJson_OVERFLOW, + /** Tried to call parse on a null parser */ + WeaselJson_NULL, }; typedef struct WeaselJsonParser WeaselJsonParser; diff --git a/src/lib.cpp b/src/lib.cpp index 715f2c6..b805e01 100644 --- a/src/lib.cpp +++ b/src/lib.cpp @@ -46,6 +46,9 @@ WeaselJsonParser_destroy(WeaselJsonParser *parser) { __attribute__((visibility("default"))) WeaselJsonStatus WeaselJsonParser_parse(WeaselJsonParser *parser, char *buf, int len) { + if (parser == nullptr) [[unlikely]] { + return WeaselJson_NULL; + } return ((Parser3 *)parser)->parse(buf, len); } } diff --git a/src/test.cpp b/src/test.cpp index 6fef3a7..0c63161 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -330,6 +330,11 @@ TEST_CASE("parse rejects negative length") { WeaselJsonParser_destroy(parser); } +TEST_CASE("Calling parse with nullptr doesn't crash") { + char buf[10] = "hello"; + REQUIRE(WeaselJsonParser_parse(nullptr, buf, -1) == WeaselJson_NULL); +} + TEST_CASE("streaming") { testStreaming(json); } TEST_CASE("reset clears inKey and transient state") { diff --git a/src/validate.cpp b/src/validate.cpp index abbd2fa..f8d1adc 100644 --- a/src/validate.cpp +++ b/src/validate.cpp @@ -33,6 +33,9 @@ int main(int argc, char **argv) { case WeaselJson_REJECT: case WeaselJson_OVERFLOW: return 1; + case WeaselJson_NULL: + fprintf(stderr, "Could not create parser\n"); + return 1; } if (l == 0) { return 1; From 6520039dc2055da5a4ede4c582a58fab6c8bd53c Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Wed, 15 Jul 2026 14:12:41 -0400 Subject: [PATCH 2/3] Address review feedback --- include/weaseljson.h | 2 +- src/test.cpp | 3 +-- src/validate.cpp | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/include/weaseljson.h b/include/weaseljson.h index 211dd1b..aa0ec52 100644 --- a/include/weaseljson.h +++ b/include/weaseljson.h @@ -67,7 +67,7 @@ void WeaselJsonParser_destroy(WeaselJsonParser *parser); /** Incrementally parse `len` more bytes starting at `buf`. `buf` may be * modified. Call with `len` 0 to indicate end of data. `buf` may be null if * `len` is 0. `len` must not be negative; a negative length is treated as a - * rejected input. */ + * rejected input. Returns WeaselJson_NULL if parser is null */ WeaselJsonStatus WeaselJsonParser_parse(WeaselJsonParser *parser, char *buf, int len); diff --git a/src/test.cpp b/src/test.cpp index 0c63161..10cb1fb 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -331,8 +331,7 @@ TEST_CASE("parse rejects negative length") { } TEST_CASE("Calling parse with nullptr doesn't crash") { - char buf[10] = "hello"; - REQUIRE(WeaselJsonParser_parse(nullptr, buf, -1) == WeaselJson_NULL); + REQUIRE(WeaselJsonParser_parse(nullptr, nullptr, 0) == WeaselJson_NULL); } TEST_CASE("streaming") { testStreaming(json); } diff --git a/src/validate.cpp b/src/validate.cpp index f8d1adc..9566056 100644 --- a/src/validate.cpp +++ b/src/validate.cpp @@ -34,7 +34,7 @@ int main(int argc, char **argv) { case WeaselJson_OVERFLOW: return 1; case WeaselJson_NULL: - fprintf(stderr, "Could not create parser\n"); + fprintf(stderr, "parse called with a null parser\n"); return 1; } if (l == 0) { From 6beb538b61de4e61bdc40feca04a4c05e59cc932 Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Wed, 15 Jul 2026 17:55:22 -0400 Subject: [PATCH 3/3] Add missing status enum to weaseljson.py --- weaseljson.py | 1 + 1 file changed, 1 insertion(+) diff --git a/weaseljson.py b/weaseljson.py index 7063c98..777d8fe 100644 --- a/weaseljson.py +++ b/weaseljson.py @@ -30,6 +30,7 @@ class WeaselJsonStatus(enum.Enum): AGAIN = 1 REJECT = 2 OVERFLOW = 3 + NULL = 4 class WeaselJsonCallbacksBase: