forked from weaselab/weaseljson
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:
+6
-11
@@ -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 = (intptr_t)writeBuf - (intptr_t)dataBegin;
|
||||||
len = writeBuf - dataBegin;
|
} else {
|
||||||
} else if (buf != nullptr) {
|
len = (intptr_t)buf - (intptr_t)dataBegin;
|
||||||
len = buf - dataBegin;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
assert(len >= 0);
|
assert(len >= 0);
|
||||||
if (done || len > 0) {
|
if (done || len > 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user