make WeaselJson_OVERFLOW a terminal state like WeaselJson_REJECT #53

Merged
andrew merged 2 commits from weaselbot/weaseljson:weaselbot/issue-52 into main 2026-07-13 20:58:15 +00:00
2 changed files with 65 additions and 9 deletions
Showing only changes of commit 5e462f1477 - Show all commits
+9 -9
View File
@@ -139,7 +139,7 @@ struct Parser3 {
stackPtr = stack();
std::ignore = push({N_VALUE, N_WHITESPACE, T_EOF});
inKey = false;
rejected = false;
terminalStatus = WeaselJson_OK;
utf8Codepoint = 0;
utf16Surrogate = 0;
minCodepoint = 0;
@@ -162,7 +162,7 @@ struct Parser3 {
NumDfa numDfa;
Utf8Dfa strDfa;
bool inKey = false;
bool rejected = false;
WeaselJsonStatus terminalStatus = WeaselJson_OK;
#ifndef HAS_MUSTTAIL
char *stashBufForTrampoline;
@@ -1070,12 +1070,12 @@ constexpr inline struct ContinuationTable {
inline WeaselJsonStatus Parser3::parse(char *buf, int len) {
this->dataBegin = this->writeBuf = buf;
if (this->rejected) [[unlikely]] {
return WeaselJson_REJECT;
if (this->terminalStatus != WeaselJson_OK) [[unlikely]] {
return this->terminalStatus;
}
if (len < 0) [[unlikely]] {
this->rejected = true;
this->terminalStatus = WeaselJson_REJECT;
return WeaselJson_REJECT;
Outdated
Review

We should just have one "sticky status" and if it's non-zero return that. That way we don't need separate branches for rejected and overflowed

We should just have one "sticky status" and if it's non-zero return that. That way we don't need separate branches for rejected and overflowed
}
@@ -1085,8 +1085,8 @@ inline WeaselJsonStatus Parser3::parse(char *buf, int len) {
// range.
ContinuationStatus status =
symbolTables.continuations[top()](this, buf, buf + len);
if (status == WeaselJson_REJECT) {
this->rejected = true;
if (status == WeaselJson_REJECT || status == WeaselJson_OVERFLOW) {
this->terminalStatus = WeaselJsonStatus(status);
}
return WeaselJsonStatus(status);
#else
@@ -1095,8 +1095,8 @@ inline WeaselJsonStatus Parser3::parse(char *buf, int len) {
while ((result = symbolTables.continuations[top()](
this, stashBufForTrampoline, buf + len)) == kBounce)
;
if (result == WeaselJson_REJECT) {
this->rejected = true;
if (result == WeaselJson_REJECT || result == WeaselJson_OVERFLOW) {
this->terminalStatus = WeaselJsonStatus(result);
}
return WeaselJsonStatus(result);
#endif
+56
View File
@@ -202,6 +202,62 @@ TEST_CASE("parser3") {
}
}
TEST_CASE("overflow state is sticky") {
auto c = noopCallbacks();
// stackSize 3 is exactly big enough to hold reset()'s bootstrap, but too
// small for nested arrays. Overflows must be terminal like rejects: a later
// end-of-data call must never report OK for an incomplete document.
auto *parser = WeaselJsonParser_create(3, &c, nullptr, 0);
REQUIRE(parser != nullptr);
std::string doc = "[[";
REQUIRE(WeaselJsonParser_parse(parser, doc.data(), doc.size()) ==
WeaselJson_OVERFLOW);
// After overflow, the end-of-data call must not return OK.
REQUIRE(WeaselJsonParser_parse(parser, nullptr, 0) != WeaselJson_OK);
// It should keep reporting a terminal failure.
REQUIRE(WeaselJsonParser_parse(parser, nullptr, 0) != WeaselJson_OK);
// Further data chunks must also stay terminal.
std::string more = "]]";
REQUIRE(WeaselJsonParser_parse(parser, more.data(), more.size()) !=
WeaselJson_OK);
WeaselJsonParser_destroy(parser);
}
TEST_CASE("overflow is sticky for nested objects") {
auto c = noopCallbacks();
auto *parser = WeaselJsonParser_create(4, &c, nullptr, 0);
REQUIRE(parser != nullptr);
std::string doc = "{\"a\":{ \"a\":";
REQUIRE(WeaselJsonParser_parse(parser, doc.data(), doc.size()) ==
WeaselJson_OVERFLOW);
REQUIRE(WeaselJsonParser_parse(parser, nullptr, 0) != WeaselJson_OK);
WeaselJsonParser_destroy(parser);
}
TEST_CASE("reset clears overflow state") {
auto c = noopCallbacks();
auto *parser = WeaselJsonParser_create(3, &c, nullptr, 0);
REQUIRE(parser != nullptr);
std::string doc = "[[";
REQUIRE(WeaselJsonParser_parse(parser, doc.data(), doc.size()) ==
WeaselJson_OVERFLOW);
// After reset the parser should accept a minimal document again.
WeaselJsonParser_reset(parser);
std::string copy = "1";
REQUIRE(WeaselJsonParser_parse(parser, copy.data(), copy.size()) ==
WeaselJson_AGAIN);
REQUIRE(WeaselJsonParser_parse(parser, nullptr, 0) == WeaselJson_OK);
WeaselJsonParser_destroy(parser);
}
TEST_CASE("rejected state is sticky") {
auto c = noopCallbacks();
auto *parser = WeaselJsonParser_create(1024, &c, nullptr, 0);