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.
This commit is contained in:
2026-06-29 15:14:36 -04:00
parent bd53e57b8e
commit bf3f2fe810
+5 -10
View File
@@ -83,10 +83,7 @@ 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 = 0; int len = (intptr_t)buf - (intptr_t)dataBegin;
if (dataBegin != nullptr && buf != nullptr) {
len = buf - dataBegin;
}
assert(len >= 0); assert(len >= 0);
if (done || len > 0) { if (done || len > 0) {
callbacks->on_number_data(userdata, dataBegin ? dataBegin : "", len, callbacks->on_number_data(userdata, dataBegin ? dataBegin : "", len,
@@ -95,13 +92,11 @@ struct Parser3 {
} }
void flushString(bool done, char *buf) { void flushString(bool done, char *buf) {
int len = 0; int len;
if (dataBegin != nullptr) {
if (!(flags & WeaselJsonRaw)) { if (!(flags & WeaselJsonRaw)) {
len = writeBuf - dataBegin; len = (intptr_t)writeBuf - (intptr_t)dataBegin;
} else if (buf != nullptr) { } 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) {