summaryrefslogtreecommitdiff
path: root/Objects/weakrefobject.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2022-11-22 13:39:11 +0100
committerGitHub <noreply@github.com>2022-11-22 13:39:11 +0100
commit135ec7cefbaffd516b77362ad2b2ad1025af462e (patch)
tree1f92fbda32d21f0efc9f54432c32af03fe49a608 /Objects/weakrefobject.c
parent3db0a21f731cec28a89f7495a82ee2670bce75fe (diff)
downloadcpython-git-135ec7cefbaffd516b77362ad2b2ad1025af462e.tar.gz
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.
Diffstat (limited to 'Objects/weakrefobject.c')
-rw-r--r--Objects/weakrefobject.c6
1 files changed, 2 insertions, 4 deletions
diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c
index ff284c882b..bd7720e275 100644
--- a/Objects/weakrefobject.c
+++ b/Objects/weakrefobject.c
@@ -824,8 +824,7 @@ PyWeakref_NewRef(PyObject *ob, PyObject *callback)
during GC. Return that one instead of this one
to avoid violating the invariants of the list
of weakrefs for ob. */
- Py_DECREF(result);
- result = (PyWeakReference*)Py_NewRef(ref);
+ Py_SETREF(result, (PyWeakReference*)Py_NewRef(ref));
}
}
else {
@@ -888,8 +887,7 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
during GC. Return that one instead of this one
to avoid violating the invariants of the list
of weakrefs for ob. */
- Py_DECREF(result);
- result = (PyWeakReference*)Py_NewRef(proxy);
+ Py_SETREF(result, (PyWeakReference*)Py_NewRef(proxy));
goto skip_insert;
}
prev = ref;