Avoid nullptr subtraction when flushing scalars at EOF #43

Closed
weaselbot wants to merge 2 commits from weaselbot/weaseljson:weaselbot/issue-40 into main
2 changed files with 26 additions and 6 deletions
+8 -6
View File
@@ -83,26 +83,28 @@ struct Parser3 {
[[nodiscard]] WeaselJsonStatus parse(char *buf, int len); [[nodiscard]] WeaselJsonStatus parse(char *buf, int len);
void flushNumber(bool done, char *buf) { void flushNumber(bool done, char *buf) {
int len = buf - dataBegin; int len = (intptr_t)buf - (intptr_t)dataBegin;
assert(len >= 0); assert(len >= 0);
if (done || 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) { void flushString(bool done, char *buf) {
int len; int len;
if (!(flags & WeaselJsonRaw)) { if (!(flags & WeaselJsonRaw)) {
len = writeBuf - dataBegin; len = (intptr_t)writeBuf - (intptr_t)dataBegin;
} else { } else {
len = buf - dataBegin; len = (intptr_t)buf - (intptr_t)dataBegin;
} }
assert(len >= 0); assert(len >= 0);
if (done || len > 0) { if (done || len > 0) {
const char *data = dataBegin ? dataBegin : "";
if (inKey) { if (inKey) {
callbacks->on_key_data(userdata, dataBegin, len, done); callbacks->on_key_data(userdata, data, len, done);
} else { } else {
callbacks->on_string_data(userdata, dataBegin, len, done); callbacks->on_string_data(userdata, data, len, done);
} }
} }
} }
+18
View File
@@ -303,6 +303,24 @@ TEST_CASE("reset clears inKey and transient state") {
WeaselJsonParser_destroy(parser); 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, void doTestUnescapingUtf8(std::string const &escaped,
std::string const &expected, int stride, int flags) { std::string const &expected, int stride, int flags) {
CAPTURE(escaped); CAPTURE(escaped);