Store stackEnd instead of stackSize

This commit is contained in:
2025-06-24 14:57:11 -04:00
parent 575b6e5c62
commit c2f5d6983a

View File

@@ -2,6 +2,7 @@
#include <cassert> #include <cassert>
#include <cctype> #include <cctype>
#include <cstddef>
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
@@ -63,7 +64,8 @@ enum Symbol : uint8_t {
}; };
struct Parser3 { struct Parser3 {
Parser3(const WeaselJsonCallbacks *callbacks, void *userdata, int stackSize) Parser3(const WeaselJsonCallbacks *callbacks, void *userdata, int stackSize)
: callbacks(callbacks), userdata(userdata), stackSize(stackSize) { : callbacks(callbacks), userdata(userdata),
stackEnd(stack() + stackSize) {
reset(); reset();
} }
@@ -91,7 +93,7 @@ struct Parser3 {
--stackPtr; --stackPtr;
} }
[[nodiscard]] WeaselJsonStatus push(std::initializer_list<Symbol> symbols) { [[nodiscard]] WeaselJsonStatus push(std::initializer_list<Symbol> symbols) {
if (stackPtr >= stack() + stackSize - symbols.size()) [[unlikely]] { if (stackEnd - stackPtr < ptrdiff_t(symbols.size())) [[unlikely]] {
return WeaselJson_OVERFLOW; return WeaselJson_OVERFLOW;
} }
for (int i = symbols.size() - 1; i >= 0; --i) { for (int i = symbols.size() - 1; i >= 0; --i) {
@@ -121,10 +123,10 @@ struct Parser3 {
WeaselJsonCallbacks const *const callbacks; WeaselJsonCallbacks const *const callbacks;
void *const userdata; void *const userdata;
Symbol *stackPtr; Symbol *stackPtr;
Symbol *const stackEnd;
uint32_t utf8Codepoint; uint32_t utf8Codepoint;
uint32_t utf16Surrogate; uint32_t utf16Surrogate;
uint32_t minCodepoint; uint32_t minCodepoint;
int const stackSize;
NumDfa numDfa; NumDfa numDfa;
Utf8Dfa strDfa; Utf8Dfa strDfa;
}; };