Avoid nullptr subtraction using intptr_t casts
CI / build (-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++, clang-arm64, ubuntu-latest-arm64, true) (pull_request) Successful in 51s
CI / build (-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++, gcc-arm64, ubuntu-latest-arm64, false) (pull_request) Successful in 53s
CI / pre-commit (pull_request) Successful in 52s
CI / build (-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++, clang-amd64, ubuntu-latest-amd64, true) (pull_request) Successful in 1m30s
CI / build (-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++, gcc-amd64, ubuntu-latest-amd64, false) (pull_request) Successful in 1m27s

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
+6 -11
View File
@@ -83,10 +83,7 @@ struct Parser3 {
[[nodiscard]] WeaselJsonStatus parse(char *buf, int len);
void flushNumber(bool done, char *buf) {
int len = 0;
if (dataBegin != nullptr && buf != nullptr) {
len = buf - dataBegin;
}
int len = (intptr_t)buf - (intptr_t)dataBegin;
assert(len >= 0);
if (done || len > 0) {
callbacks->on_number_data(userdata, dataBegin ? dataBegin : "", len,
@@ -95,13 +92,11 @@ struct Parser3 {
}
void flushString(bool done, char *buf) {
int len = 0;
if (dataBegin != nullptr) {
if (!(flags & WeaselJsonRaw)) {
len = writeBuf - dataBegin;
} else if (buf != nullptr) {
len = buf - dataBegin;
}
int len;
if (!(flags & WeaselJsonRaw)) {
len = (intptr_t)writeBuf - (intptr_t)dataBegin;
} else {
len = (intptr_t)buf - (intptr_t)dataBegin;
}
assert(len >= 0);
if (done || len > 0) {