8000 gh-96735: Fix undefined behaviour in struct unpacking functions by mdickinson · Pull Request #96739 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-96735: Fix undefined behaviour in struct unpacking functions #96739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Prev Previous commit
Fix unconventional spacing
  • Loading branch information
mdickinson committed Sep 13, 2022
commit 1294440b5c04c807f36911ab7d8721f87045ca90
12 changes: 6 additions & 6 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ bu_short(_structmodulestate *state, const char *p, const formatdef *f)
} while (--i > 0);
/* Extend sign, avoiding implementation-defined or undefined behaviour. */
x = (x ^ 0x8000U) - 0x8000U;
return PyLong_FromLong(x & 0x8000U ? -1 - (long)(~x): (long)x);
return PyLong_FromLong(x & 0x8000U ? -1 - (long)(~x) : (long)x);
}

static PyObject *
Expand All @@ -836,7 +836,7 @@ bu_int(_structmodulestate *state, const char *p, const formatdef *f)
} while (--i > 0);
/* Extend sign, avoiding implementation-defined or undefined behaviour. */
x = (x ^ 0x80000000U) - 0x80000000U;
return PyLong_FromLong(x & 0x80000000U ? -1 - (long)(~x): (long)x);
return PyLong_FromLong(x & 0x80000000U ? -1 - (long)(~x) : (long)x);
}

static PyObject *
Expand Down Expand Up @@ -866,7 +866,7 @@ bu_longlong(_structmodulestate *state, const char *p, const formatdef *f)
/* Extend sign, avoiding implementation-defined or undefined behaviour. */
x = (x ^ 0x8000000000000000U) - 0x8000000000000000U;
return PyLong_FromLongLong(
x & 0x8000000000000000U ? -1 - (long long)(~x): (long long)x);
x & 0x8000000000000000U ? -1 - (long long)(~x) : (long long)x);
}

static PyObject *
Expand Down Expand Up @@ -1062,7 +1062,7 @@ lu_short(_structmodulestate *state, const char *p, const formatdef *f)
} while (i > 0);
/* Extend sign, avoiding implementation-defined or undefined behaviour. */
x = (x ^ 0x8000U) - 0x8000U;
return PyLong_FromLong(x & 0x8000U ? -1 - (long)(~x): (long)x);
return PyLong_FromLong(x & 0x8000U ? -1 - (long)(~x) : (long)x);
}

static PyObject *
Expand All @@ -1079,7 +1079,7 @@ lu_int(_structmodulestate *state, const char *p, const formatdef *f)
} while (i > 0);
/* Extend sign, avoiding implementation-defined or undefined behaviour. */
x = (x ^ 0x80000000U) - 0x80000000U;
return PyLong_FromLong(x & 0x80000000U ? -1 - (long)(~x): (long)x);
return PyLong_FromLong(x & 0x80000000U ? -1 - (long)(~x) : (long)x);
}

static PyObject *
Expand Down Expand Up @@ -1109,7 +1109,7 @@ lu_longlong(_structmodulestate *state, const char *p, const formatdef *f)
/* Extend sign, avoiding implementation-defined or undefined behaviour. */
x = (x ^ 0x8000000000000000U) - 0x8000000000000000U;
return PyLong_FromLongLong(
x & 0x8000000000000000U ? -1 - (long long)(~x): (long long)x);
x & 0x8000000000000000U ? -1 - (long long)(~x) : (long long)x);
}

static PyObject *
Expand Down
0