11 Commits
Author SHA1 Message Date
weaselbot 681892107f schemagen: escape control characters in generated C++ string literals
Fixes #35.

Add a helper to escape C++ string literals so that JSON control characters
(\n, \r, \t, and other bytes below 0x20) are emitted as escape sequences
instead of raw bytes. Use it for:

- field comments that include the JSON property key
- object key comparison literals in matchKey()
- enum name arrays

Also add regression tests that generate and syntax-check headers for
schemas containing newlines and other control characters in property keys
and enum values.
2026-06-30 12:26:10 -04:00
andrew e22bc039ae Merge pull request 'schemagen: fix quadratic leading-zero strip in integer fallback' (#47) from weaselbot/weaseljson:weaselbot/issue-34 into main
Reviewed-on: weaselab/weaseljson#47
2026-06-30 16:16:44 +00:00
weaselbot ceb16e5405 schemagen: fix quadratic leading-zero strip in integer fallback
Replace the O(k^2) loop that erased leading zeros one byte at a time
from the front of a std::string with a single linear scan and one
erase(0, n) call.

Also adds regression tests for issue #34:
- correctness cases for numbers with leading fractional zeros
- a static check that the generated code no longer contains the
  quadratic pattern
- a large-input case (100k leading zeros) that reproduces the
  vulnerable shape

Closes #34
2026-06-30 12:09:26 -04:00
andrew 241c29073b Merge pull request 'schemagen: handle WeaselJsonParser_create failure in RootBuilder' (#46) from weaselbot/weaseljson:weaselbot/issue-36 into main
Reviewed-on: weaselab/weaseljson#46
2026-06-30 15:35:20 +00:00
weaselbot abeaae7ed7 schemagen: handle WeaselJsonParser_create failure in RootBuilder
If WeaselJsonParser_create returns nullptr (e.g. negative stack size or allocation failure), set the existing error_ flag so that subsequent feed()/finish() calls return WeaselJson_REJECT instead of dereferencing the null parser_.

Also add a regression test in test_gen.cpp that constructs a RootBuilder with an invalid stack size and verifies it rejects without crashing.

Closes #36
2026-06-30 11:26:59 -04:00
andrew 4bd1088018 Merge pull request 'Include <cstdint> in json_value.h for uint8_t' (#45) from weaselbot/weaseljson:weaselbot/issue-37 into main
Reviewed-on: weaselab/weaseljson#45
2026-06-29 18:57:40 +00:00
andrew 82bdc8a080 Merge pull request 'python: raise OSError when shared library is missing' (#44) from weaselbot/weaseljson:weaselbot/issue-38 into main
Reviewed-on: weaselab/weaseljson#44
2026-06-29 18:53:49 +00:00
andrew 6508616edc Merge pull request 'Handle null parser in WeaselJsonParser_reset and _destroy' (#42) from weaselbot/weaseljson:weaselbot/issue-41 into main
Reviewed-on: weaselab/weaseljson#42
2026-06-29 18:26:48 +00:00
weaselbot e5c970a605 Include <cstdint> in json_value.h for uint8_t
`escapeAsJsonString` uses `uint8_t` but the header did not include
`<cstdint>`, making it dependent on other headers to define the type.
Add the missing include so `json_value.h` is self-contained.
2026-06-29 14:04:04 -04:00
weaselbot 96f61665bf python: raise OSError when shared library is missing
Replace sys.exit(1) in WeaselJsonParser.__init__ with an OSError so
callers can handle a missing libweaseljson gracefully. Also add a test
that verifies the constructor raises OSError for a non-existent build
directory.

Closes #38
2026-06-29 14:03:03 -04:00
weaselbot 34fc22a7c2 Handle null parser in WeaselJsonParser_reset and _destroy
WeaselJsonParser_create can return nullptr when allocation fails or the
requested stack size is too small. Previously, passing that nullptr to
WeaselJsonParser_reset or WeaselJsonParser_destroy dereferenced it before
doing any work, causing immediate undefined behavior.

Add an early null check to both functions so they behave like free(nullptr)
(i.e., are a safe no-op). Also add a doctest case covering both a null
returned from create and a literal nullptr.

Closes #41
2026-06-29 13:53:31 -04:00
8 changed files with 236 additions and 15 deletions
+9
View File
@@ -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 É",
+144
View File
@@ -382,6 +382,8 @@ class SchemagenIntegerBoundaryTest(unittest.TestCase):
("1e-3", "WeaselJson_REJECT", 0),
("1000e-3", "WeaselJson_OK", 1),
("100.0e-2", "WeaselJson_OK", 1),
("0.0001e4", "WeaselJson_OK", 1),
("0.001e3", "WeaselJson_OK", 1),
("123.0", "WeaselJson_OK", 123),
("9e18", "WeaselJson_OK", 9000000000000000000),
("10e18", "WeaselJson_REJECT", 0),
@@ -425,6 +427,7 @@ class SchemagenIntegerBoundaryTest(unittest.TestCase):
('{"age":-9223372036854775809}', "WeaselJson_REJECT", 0),
('{"age":1e3}', "WeaselJson_OK", 1000),
('{"age":2.0}', "WeaselJson_OK", 2),
('{"age":0.0001e4}', "WeaselJson_OK", 1),
('{"age":0.001}', "WeaselJson_REJECT", 0),
(
'{"age":-9223372036854775808.0}',
@@ -468,6 +471,147 @@ class SchemagenIntegerBoundaryTest(unittest.TestCase):
with tempfile.TemporaryDirectory() as tmpdir:
self._compile_harness(tmpdir, schema, harness)
def test_integer_no_quadratic_leading_zero_loop(self):
"""Regression test for issue #34: leading-zero stripping must not be quadratic."""
with tempfile.TemporaryDirectory() as tmpdir:
schema_path = os.path.join(tmpdir, "schema.json")
with open(schema_path, "w") as fp:
json.dump({"type": "integer"}, fp)
result = subprocess.run(
[sys.executable, SCRIPT, schema_path],
capture_output=True,
text=True,
check=False,
)
self.assertEqual(result.returncode, 0, msg=result.stderr)
self.assertIn("parseJsonInt64", result.stdout)
self.assertNotIn("digits.erase(digits.begin())", result.stdout)
def test_integer_large_fractional_leading_zeros(self):
"""Numbers with many leading fractional zeros must parse correctly."""
if not self.compiler:
self.skipTest("C++ compiler not available")
harness = textwrap.dedent(
"""
#include "gen.h"
#include <cstdio>
#include <string>
int main() {
const int n = 100000;
std::string s = std::string("0.") + std::string(n - 1, '0') + "1e" + std::to_string(n);
test_schema::RootBuilder b;
WeaselJsonStatus st = b.feed(s.data(), static_cast<int>(s.size()));
st = b.finish();
if (st != WeaselJson_OK) {
std::printf("expected OK, got %d\\n", st);
return 1;
}
if (b.take() != 1) {
std::printf("expected value 1\\n");
return 2;
}
return 0;
}
"""
)
with tempfile.TemporaryDirectory() as tmpdir:
self._compile_harness(tmpdir, {"type": "integer"}, harness)
class SchemagenStringEscapeTest(unittest.TestCase):
"""Regression tests for issue #35: control characters in string literals."""
def setUp(self):
self.repo_root = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
self.include_dir = os.path.join(self.repo_root, "include")
self.compiler = shutil.which("c++")
def generate_and_compile(self, schema):
"""Run schemagen on schema and syntax-check the resulting header."""
with tempfile.TemporaryDirectory() as tmpdir:
schema_path = os.path.join(tmpdir, "schema.json")
with open(schema_path, "w") as fp:
json.dump(schema, fp)
header_path = os.path.join(tmpdir, "gen.h")
cmd = [
sys.executable,
SCRIPT,
schema_path,
"-o",
header_path,
"--namespace",
"test_schema",
]
result = subprocess.run(cmd, capture_output=True, text=True, check=False)
self.assertEqual(result.returncode, 0, msg=result.stderr)
if self.compiler:
cpp_path = os.path.join(tmpdir, "test.cpp")
with open(cpp_path, "w") as fp:
fp.write(
'#include "gen.h"\n'
"int main() {\n"
" test_schema::RootBuilder b;\n"
" test_schema::Root r = b.take();\n"
" (void)r;\n"
"}\n"
)
comp = subprocess.run(
[
self.compiler,
"-std=c++20",
"-fsyntax-only",
"-I",
self.include_dir,
"-I",
tmpdir,
cpp_path,
],
capture_output=True,
text=True,
check=False,
)
self.assertEqual(comp.returncode, 0, msg=comp.stderr)
with open(header_path) as fp:
return fp.read()
def test_newline_in_property_key(self):
"""A JSON key containing a newline must become a valid C++ literal."""
schema = {"type": "object", "properties": {"a\nb": {"type": "string"}}}
out = self.generate_and_compile(schema)
self.assertIn('// "a\\nb"', out)
self.assertIn('if (key == "a\\nb") return 0;', out)
def test_newline_in_enum_value(self):
"""An enum value containing a newline must become a valid C++ literal."""
schema = {"type": "object", "properties": {"x": {"enum": ["a\nb"]}}}
out = self.generate_and_compile(schema)
self.assertIn('static constexpr const char *X_names[] = { "a\\nb" };', out)
def test_mixed_control_chars_in_enum_value(self):
"""Mixed control characters in an enum value must be escaped."""
schema = {
"type": "object",
"properties": {
"x": {"enum": ["x\ny\rz\tw\vq\x00\x01"]},
},
}
out = self.generate_and_compile(schema)
self.assertIn(
'static constexpr const char *X_names[] = { "x\\ny\\rz\\tw\\vq\\u0000\\u0001" };',
out,
)
def test_backslash_and_quote_still_escaped(self):
"""Existing escaping for backslash and double quote must remain correct."""
schema = {"type": "object", "properties": {'a"b\\c': {"type": "string"}}}
out = self.generate_and_compile(schema)
self.assertIn('// "a\\"b\\\\c"', out)
self.assertIn('if (key == "a\\"b\\\\c") return 0;', out)
if __name__ == "__main__":
unittest.main()
+49 -8
View File
@@ -18,6 +18,43 @@ import keyword
import sys
def _escape_cpp_string(s):
"""Return *s* escaped for use inside a C++ double-quoted string literal.
JSON strings may contain control characters; emitting them verbatim into
generated C++ source breaks tokenization. This helper escapes backslashes
and double quotes, maps common control characters to their short escape
sequences, and uses universal character names (\\u00XX) for any other
character below 0x20.
"""
out = []
for ch in s:
cp = ord(ch)
if cp == 0x09:
out.append("\\t")
elif cp == 0x0A:
out.append("\\n")
elif cp == 0x0B:
out.append("\\v")
elif cp == 0x0C:
out.append("\\f")
elif cp == 0x0D:
out.append("\\r")
elif cp == 0x08:
out.append("\\b")
elif cp == 0x07:
out.append("\\a")
elif cp < 0x20:
out.append(f"\\u{cp:04X}")
elif ch == "\\":
out.append("\\\\")
elif ch == '"':
out.append('\\"')
else:
out.append(ch)
return "".join(out)
class GenError(Exception):
pass
@@ -618,7 +655,8 @@ namespace {ns} {{"""
and f.ty.kind in ("int", "dbl", "bool")
):
init = " = 0" if f.ty.kind != "bool" else " = false"
out.append(f' {store} {f.cpp}{init}; // "{f.key}"')
esc_key = _escape_cpp_string(f.key)
out.append(f' {store} {f.cpp}{init}; // "{esc_key}"')
out.append("};")
out.append("")
return "\n".join(out)
@@ -746,7 +784,7 @@ namespace {ns} {{"""
for name, obj in self.b.objects.items():
lines.append(f" case Kind::{name}:")
for i, fld in enumerate(obj.fields):
esc = fld.key.replace("\\", "\\\\").replace('"', '\\"')
esc = _escape_cpp_string(fld.key)
lines.append(f' if (key == "{esc}") return {i};')
lines.append(" return -1;")
lines.append(" default: return -1;")
@@ -829,10 +867,7 @@ namespace {ns} {{"""
def _enum_name_arrays(self):
out = []
for e in self.b.enums.values():
lits = ", ".join(
'"' + v.replace("\\", "\\\\").replace('"', '\\"') + '"'
for v in e.values
)
lits = ", ".join('"' + _escape_cpp_string(v) + '"' for v in e.values)
out.append(
f" static constexpr const char *{e.name}_names[] = {{ {lits} }};"
)
@@ -892,6 +927,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_); }}
@@ -996,8 +1035,10 @@ private:
}}
int64_t finalExp = exp - fracDigits + trim;
if (finalExp < 0) return false;
while (!digits.empty() && digits.front() == '0') digits.erase(digits.begin());
if (digits.empty()) {{ out = 0; return true; }}
size_t leadingZeros = 0;
while (leadingZeros < digits.size() && digits[leadingZeros] == '0') ++leadingZeros;
if (leadingZeros == digits.size()) {{ out = 0; return true; }}
if (leadingZeros > 0) digits.erase(0, leadingZeros);
constexpr uint64_t kMaxNeg = 9223372036854775808ULL;
constexpr uint64_t kMaxPos = 9223372036854775807ULL;
+1
View File
@@ -1,6 +1,7 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <map>
#include <memory>
#include <optional>
+6
View File
@@ -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);
}
+14
View File
@@ -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);
+12
View File
@@ -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
View File
@@ -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,