Author SHA1 Message Date
weaselbot bf3f2fe810 Avoid nullptr subtraction using intptr_t casts
Andrew's review on the previous fix noted that the nullptr checks produced slightly worse codegen. Replace the pointer subtraction with intptr_t subtraction, which avoids the undefined behaviour of subtracting two null pointers without introducing extra branches.
2026-06-29 15:14:36 -04:00
weaselbot bd53e57b8e Avoid nullptr subtraction when flushing scalars at EOF
When a scalar ends exactly at a chunk boundary, Parser3::parse resets
dataBegin and writeBuf to the new buf at the start of every call. On the
EOF call buf is null, so both pointers become null. The final
flushNumber/flushString then computed len as buf - dataBegin, i.e.
nullptr - nullptr, which is undefined behaviour in C++.

Compute the flush length safely: if dataBegin is null (or, for raw mode,
buf is null), treat the length as zero. Use an empty string literal as a
non-null data pointer for the zero-length, done=true callback so callers
still receive the completion signal.

Add a regression test covering a number that fills its chunk exactly and
is finalized by an EOF call.

Fixes #40
2026-06-29 13:57:58 -04:00
6 changed files with 33 additions and 40 deletions
-1
View File
@@ -1,7 +1,6 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <map>
#include <memory>
#include <optional>
-6
View File
@@ -29,17 +29,11 @@ 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);
}
+8 -6
View File
@@ -83,26 +83,28 @@ struct Parser3 {
[[nodiscard]] WeaselJsonStatus parse(char *buf, int len);
void flushNumber(bool done, char *buf) {
int len = buf - dataBegin;
int len = (intptr_t)buf - (intptr_t)dataBegin;
assert(len >= 0);
if (done || len > 0) {
callbacks->on_number_data(userdata, dataBegin, len, done);
callbacks->on_number_data(userdata, dataBegin ? dataBegin : "", len,
done);
}
}
void flushString(bool done, char *buf) {
int len;
if (!(flags & WeaselJsonRaw)) {
len = writeBuf - dataBegin;
len = (intptr_t)writeBuf - (intptr_t)dataBegin;
} else {
len = buf - dataBegin;
len = (intptr_t)buf - (intptr_t)dataBegin;
}
assert(len >= 0);
if (done || len > 0) {
const char *data = dataBegin ? dataBegin : "";
if (inKey) {
callbacks->on_key_data(userdata, dataBegin, len, done);
callbacks->on_key_data(userdata, data, len, done);
} else {
callbacks->on_string_data(userdata, dataBegin, len, done);
callbacks->on_string_data(userdata, data, len, done);
}
}
}
+18 -14
View File
@@ -246,20 +246,6 @@ 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);
@@ -317,6 +303,24 @@ TEST_CASE("reset clears inKey and transient state") {
WeaselJsonParser_destroy(parser);
}
TEST_CASE("scalar ending at chunk boundary is finalized at EOF") {
// A number whose digits exactly fill the first chunk must not invoke
// undefined behaviour on the EOF call, and must still signal completion.
auto c = serializeCallbacks();
SerializeState state;
auto *parser = WeaselJsonParser_create(1024, &c, &state, 0);
REQUIRE(parser != nullptr);
std::string chunk = "123";
REQUIRE(WeaselJsonParser_parse(parser, chunk.data(), chunk.size()) ==
WeaselJson_AGAIN);
REQUIRE(WeaselJsonParser_parse(parser, nullptr, 0) == WeaselJson_OK);
CHECK(state.result == "(123)");
WeaselJsonParser_destroy(parser);
}
void doTestUnescapingUtf8(std::string const &escaped,
std::string const &expected, int stride, int flags) {
CAPTURE(escaped);
-12
View File
@@ -95,20 +95,8 @@ 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")
+7 -1
View File
@@ -84,7 +84,13 @@ class WeaselJsonParser:
pass
if self._lib is None:
raise OSError(f"Could not load libweaseljson from {build_dir}")
import sys
print(
"Could not find libweaseljson implementation",
file=sys.stderr,
)
sys.exit(1)
self._lib.WeaselJsonParser_create.argtypes = (
ctypes.c_int,