forked from weaselab/weaseljson
Compare commits
7
Commits
08b864d31b
...
abeaae7ed7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
abeaae7ed7 | ||
|
|
4bd1088018 | ||
|
|
82bdc8a080 | ||
|
|
6508616edc | ||
|
|
e5c970a605 | ||
|
|
96f61665bf | ||
|
|
34fc22a7c2 |
@@ -68,6 +68,15 @@ int main() {
|
||||
expectReject(json, "unknown key in strict root");
|
||||
}
|
||||
|
||||
// ---- invalid stack size is rejected without crashing ----
|
||||
{
|
||||
RootBuilder b(-1);
|
||||
char buf[] = "null";
|
||||
WeaselJsonStatus s = b.feed(buf, sizeof(buf) - 1);
|
||||
CHECK(s == WeaselJson_REJECT);
|
||||
printf("ok invalid stack size rejected, not crashed\n");
|
||||
}
|
||||
|
||||
{
|
||||
std::string json = R"({
|
||||
"name": "Ada É",
|
||||
|
||||
@@ -892,6 +892,10 @@ public:
|
||||
explicit RootBuilder(int stackSize = 1024) {{
|
||||
cb_ = makeCallbacks();
|
||||
parser_ = WeaselJsonParser_create(stackSize, &cb_, this, 0);
|
||||
if (!parser_) {{
|
||||
error_ = true;
|
||||
return;
|
||||
}}
|
||||
{self._ctor_body()}
|
||||
}}
|
||||
~RootBuilder() {{ if (parser_) WeaselJsonParser_destroy(parser_); }}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
@@ -29,11 +29,17 @@ WeaselJsonParser_create(int stackSize, const WeaselJsonCallbacks *callbacks,
|
||||
|
||||
__attribute__((visibility("default"))) void
|
||||
WeaselJsonParser_reset(WeaselJsonParser *parser) {
|
||||
if (parser == nullptr) {
|
||||
return;
|
||||
}
|
||||
((Parser3 *)parser)->reset();
|
||||
}
|
||||
|
||||
__attribute__((visibility("default"))) void
|
||||
WeaselJsonParser_destroy(WeaselJsonParser *parser) {
|
||||
if (parser == nullptr) {
|
||||
return;
|
||||
}
|
||||
((Parser3 *)parser)->~Parser3();
|
||||
free(parser);
|
||||
}
|
||||
|
||||
@@ -246,6 +246,20 @@ TEST_CASE("create rejects too-small stack") {
|
||||
WeaselJsonParser_destroy(parser);
|
||||
}
|
||||
|
||||
TEST_CASE("reset and destroy accept null parser") {
|
||||
// Creation can legitimately fail and return null. The cleanup functions must
|
||||
// tolerate a null pointer the same way free(nullptr) is a no-op.
|
||||
auto c = noopCallbacks();
|
||||
WeaselJsonParser *parser = WeaselJsonParser_create(-1, &c, nullptr, 0);
|
||||
REQUIRE(parser == nullptr);
|
||||
WeaselJsonParser_reset(parser); // must not crash
|
||||
WeaselJsonParser_destroy(parser); // must not crash
|
||||
|
||||
// Calling reset/destroy on literal nullptr directly must also be safe.
|
||||
WeaselJsonParser_reset(nullptr);
|
||||
WeaselJsonParser_destroy(nullptr);
|
||||
}
|
||||
|
||||
TEST_CASE("parse rejects negative length") {
|
||||
auto c = noopCallbacks();
|
||||
auto *parser = WeaselJsonParser_create(1024, &c, nullptr, 0);
|
||||
|
||||
@@ -95,8 +95,20 @@ def test_create_rejects_too_small_stack():
|
||||
raise AssertionError(f"expected ValueError for stackSize={stack_size}")
|
||||
|
||||
|
||||
def test_missing_library_raises_oserror():
|
||||
try:
|
||||
weaseljson.WeaselJsonParser(
|
||||
weaseljson.WeaselJsonCallbacksBase(),
|
||||
build_dir="/nonexistent",
|
||||
)
|
||||
except OSError:
|
||||
return
|
||||
raise AssertionError("expected OSError when the shared library is missing")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_object_keys_routed_correctly()
|
||||
test_mixed_values()
|
||||
test_create_rejects_too_small_stack()
|
||||
test_missing_library_raises_oserror()
|
||||
print("python bindings ok")
|
||||
|
||||
+1
-7
@@ -84,13 +84,7 @@ class WeaselJsonParser:
|
||||
pass
|
||||
|
||||
if self._lib is None:
|
||||
import sys
|
||||
|
||||
print(
|
||||
"Could not find libweaseljson implementation",
|
||||
file=sys.stderr,
|
||||
)
|
||||
sys.exit(1)
|
||||
raise OSError(f"Could not load libweaseljson from {build_dir}")
|
||||
|
||||
self._lib.WeaselJsonParser_create.argtypes = (
|
||||
ctypes.c_int,
|
||||
|
||||
Reference in New Issue
Block a user