summaryrefslogtreecommitdiff
path: root/Objects/sliceobject.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/sliceobject.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/sliceobject.c')
-rw-r--r--Objects/sliceobject.c22
1 files changed, 6 insertions, 16 deletions
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
index 1da1df3090..5694bd9c66 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -448,8 +448,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
if (_PyLong_Sign(start) < 0) {
/* start += length */
PyObject *tmp = PyNumber_Add(start, length);
- Py_DECREF(start);
- start = tmp;
+ Py_SETREF(start, tmp);
if (start == NULL)
goto error;
@@ -457,9 +456,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
if (cmp_result < 0)
goto error;
if (cmp_result) {
- Py_INCREF(lower);
- Py_DECREF(start);
- start = lower;
+ Py_SETREF(start, Py_NewRef(lower));
}
}
else {
@@ -467,9 +464,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
if (cmp_result < 0)
goto error;
if (cmp_result) {
- Py_INCREF(upper);
- Py_DECREF(start);
- start = upper;
+ Py_SETREF(start, Py_NewRef(upper));
}
}
}
@@ -486,8 +481,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
if (_PyLong_Sign(stop) < 0) {
/* stop += length */
PyObject *tmp = PyNumber_Add(stop, length);
- Py_DECREF(stop);
- stop = tmp;
+ Py_SETREF(stop, tmp);
if (stop == NULL)
goto error;
@@ -495,9 +489,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
if (cmp_result < 0)
goto error;
if (cmp_result) {
- Py_INCREF(lower);
- Py_DECREF(stop);
- stop = lower;
+ Py_SETREF(stop, Py_NewRef(lower));
}
}
else {
@@ -505,9 +497,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
if (cmp_result < 0)
goto error;
if (cmp_result) {
- Py_INCREF(upper);
- Py_DECREF(stop);
- stop = upper;
+ Py_SETREF(stop, Py_NewRef(upper));
}
}
}