gh-99537: Use Py_SETREF() function in C code (#99657)

Fix potential race condition in code patterns:

* Replace "Py_DECREF(var); var = new;" with "Py_SETREF(var, new);"
* Replace "Py_XDECREF(var); var = new;" with "Py_XSETREF(var, new);"
* Replace "Py_CLEAR(var); var = new;" with "Py_XSETREF(var, new);"

Other changes:

* Replace "old = var; var = new; Py_DECREF(var)"
  with "Py_SETREF(var, new);"
* Replace "old = var; var = new; Py_XDECREF(var)"
  with "Py_XSETREF(var, new);"
* And remove the "old" variable.
This commit is contained in:
Victor Stinner 2022-11-22 13:39:11 +01:00 committed by GitHub
parent 3db0a21f73
commit 135ec7cefb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 34 additions and 76 deletions

View file

@ -801,8 +801,7 @@ next_external_frame(PyFrameObject *frame)
{
do {
PyFrameObject *back = PyFrame_GetBack(frame);
Py_DECREF(frame);
frame = back;
Py_SETREF(frame, back);
} while (frame != NULL && is_internal_frame(frame));
return frame;
@ -828,8 +827,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
if (stack_level <= 0 || is_internal_frame(f)) {
while (--stack_level > 0 && f != NULL) {
PyFrameObject *back = PyFrame_GetBack(f);
Py_DECREF(f);
f = back;
Py_SETREF(f, back);
}
}
else {