Unverified Kaydet (Commit) dcb68f47 authored tarafından Victor Stinner's avatar Victor Stinner Kaydeden (comit) GitHub

bpo-35961: Fix a crash in slice_richcompare() (GH-11830)

Fix a crash in slice_richcompare(): use strong references rather than
stolen references for the two temporary internal tuples.

The crash (or assertion error) occurred if a garbage collection
occurred during slice_richcompare(), especially while calling
PyObject_RichCompare(t1, t2, op).
üst 5680f654
Fix a crash in slice_richcompare(): use strong references rather than stolen
references for the two temporary internal tuples.
...@@ -565,14 +565,11 @@ static PyMethodDef slice_methods[] = { ...@@ -565,14 +565,11 @@ static PyMethodDef slice_methods[] = {
static PyObject * static PyObject *
slice_richcompare(PyObject *v, PyObject *w, int op) slice_richcompare(PyObject *v, PyObject *w, int op)
{ {
PyObject *t1;
PyObject *t2;
PyObject *res;
if (!PySlice_Check(v) || !PySlice_Check(w)) if (!PySlice_Check(v) || !PySlice_Check(w))
Py_RETURN_NOTIMPLEMENTED; Py_RETURN_NOTIMPLEMENTED;
if (v == w) { if (v == w) {
PyObject *res;
/* XXX Do we really need this shortcut? /* XXX Do we really need this shortcut?
There's a unit test for it, but is that fair? */ There's a unit test for it, but is that fair? */
switch (op) { switch (op) {
...@@ -589,34 +586,27 @@ slice_richcompare(PyObject *v, PyObject *w, int op) ...@@ -589,34 +586,27 @@ slice_richcompare(PyObject *v, PyObject *w, int op)
return res; return res;
} }
t1 = PyTuple_New(3);
if (t1 == NULL) PyObject *t1 = PyTuple_Pack(3,
((PySliceObject *)v)->start,
((PySliceObject *)v)->stop,
((PySliceObject *)v)->step);
if (t1 == NULL) {
return NULL; return NULL;
t2 = PyTuple_New(3); }
PyObject *t2 = PyTuple_Pack(3,
((PySliceObject *)w)->start,
((PySliceObject *)w)->stop,
((PySliceObject *)w)->step);
if (t2 == NULL) { if (t2 == NULL) {
Py_DECREF(t1); Py_DECREF(t1);
return NULL; return NULL;
} }
PyTuple_SET_ITEM(t1, 0, ((PySliceObject *)v)->start); PyObject *res = PyObject_RichCompare(t1, t2, op);
PyTuple_SET_ITEM(t1, 1, ((PySliceObject *)v)->stop);
PyTuple_SET_ITEM(t1, 2, ((PySliceObject *)v)->step);
PyTuple_SET_ITEM(t2, 0, ((PySliceObject *)w)->start);
PyTuple_SET_ITEM(t2, 1, ((PySliceObject *)w)->stop);
PyTuple_SET_ITEM(t2, 2, ((PySliceObject *)w)->step);
res = PyObject_RichCompare(t1, t2, op);
PyTuple_SET_ITEM(t1, 0, NULL);
PyTuple_SET_ITEM(t1, 1, NULL);
PyTuple_SET_ITEM(t1, 2, NULL);
PyTuple_SET_ITEM(t2, 0, NULL);
PyTuple_SET_ITEM(t2, 1, NULL);
PyTuple_SET_ITEM(t2, 2, NULL);
Py_DECREF(t1); Py_DECREF(t1);
Py_DECREF(t2); Py_DECREF(t2);
return res; return res;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment