diff options
author | Victor Stinner <vstinner@python.org> | 2022-11-22 13:22:22 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-22 13:22:22 (GMT) |
commit | 7e3f09cad9b783d8968aa79ff6a8ee57beb8b83e (patch) | |
tree | a8d4dda9e9128545442794595f5ebc96a5c1c7b4 /Doc/includes/custom3.c | |
parent | 135ec7cefbaffd516b77362ad2b2ad1025af462e (diff) | |
download | cpython-7e3f09cad9b783d8968aa79ff6a8ee57beb8b83e.zip cpython-7e3f09cad9b783d8968aa79ff6a8ee57beb8b83e.tar.gz cpython-7e3f09cad9b783d8968aa79ff6a8ee57beb8b83e.tar.bz2 |
gh-99537: Use Py_SETREF() function in C code (#99656)
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 'Doc/includes/custom3.c')
-rw-r--r-- | Doc/includes/custom3.c | 20 |
1 files changed, 5 insertions, 15 deletions
diff --git a/Doc/includes/custom3.c b/Doc/includes/custom3.c index 0faf2bd..1a68bc4 100644 --- a/Doc/includes/custom3.c +++ b/Doc/includes/custom3.c @@ -42,7 +42,7 @@ static int Custom_init(CustomObject *self, PyObject *args, PyObject *kwds) { static char *kwlist[] = {"first", "last", "number", NULL}; - PyObject *first = NULL, *last = NULL, *tmp; + PyObject *first = NULL, *last = NULL; if (!PyArg_ParseTupleAndKeywords(args, kwds, "|UUi", kwlist, &first, &last, @@ -50,14 +50,10 @@ Custom_init(CustomObject *self, PyObject *args, PyObject *kwds) return -1; if (first) { - tmp = self->first; - self->first = Py_NewRef(first); - Py_DECREF(tmp); + Py_SETREF(self->first, Py_NewRef(first)); } if (last) { - tmp = self->last; - self->last = Py_NewRef(last); - Py_DECREF(tmp); + Py_SETREF(self->last, Py_NewRef(last)); } return 0; } @@ -77,7 +73,6 @@ Custom_getfirst(CustomObject *self, void *closure) static int Custom_setfirst(CustomObject *self, PyObject *value, void *closure) { - PyObject *tmp; if (value == NULL) { PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute"); return -1; @@ -87,9 +82,7 @@ Custom_setfirst(CustomObject *self, PyObject *value, void *closure) "The first attribute value must be a string"); return -1; } - tmp = self->first; - self->first = Py_NewRef(value); - Py_DECREF(tmp); + Py_SETREF(self->first, Py_NewRef(value)); return 0; } @@ -102,7 +95,6 @@ Custom_getlast(CustomObject *self, void *closure) static int Custom_setlast(CustomObject *self, PyObject *value, void *closure) { - PyObject *tmp; if (value == NULL) { PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute"); return -1; @@ -112,9 +104,7 @@ Custom_setlast(CustomObject *self, PyObject *value, void *closure) "The last attribute value must be a string"); return -1; } - tmp = self->last; - self->last = Py_NewRef(value); - Py_DECREF(tmp); + Py_SETREF(self->last, Py_NewRef(value)); return 0; } |