Commit 75ccea37 authored by Raymond Hettinger's avatar Raymond Hettinger

SF patch #1020188: Use Py_CLEAR where necessary to avoid crashes

(Contributed by Dima Dorfman)
parent 410eb84a
...@@ -623,7 +623,7 @@ PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force); ...@@ -623,7 +623,7 @@ PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
#define Py_CLEAR(op) \ #define Py_CLEAR(op) \
do { \ do { \
if (op) { \ if (op) { \
PyObject *tmp = (op); \ PyObject *tmp = (PyObject *)(op); \
(op) = NULL; \ (op) = NULL; \
Py_DECREF(tmp); \ Py_DECREF(tmp); \
} \ } \
......
...@@ -250,8 +250,7 @@ _grouper_next(_grouperobject *igo) ...@@ -250,8 +250,7 @@ _grouper_next(_grouperobject *igo)
r = gbo->currvalue; r = gbo->currvalue;
gbo->currvalue = NULL; gbo->currvalue = NULL;
Py_DECREF(gbo->currkey); Py_CLEAR(gbo->currkey);
gbo->currkey = NULL;
return r; return r;
} }
......
...@@ -230,10 +230,7 @@ reversed_next(reversedobject *ro) ...@@ -230,10 +230,7 @@ reversed_next(reversedobject *ro)
PyErr_Clear(); PyErr_Clear();
} }
ro->index = -1; ro->index = -1;
if (ro->seq != NULL) { Py_CLEAR(ro->seq);
Py_DECREF(ro->seq);
ro->seq = NULL;
}
return NULL; return NULL;
} }
......
...@@ -51,8 +51,7 @@ gen_iternext(PyGenObject *gen) ...@@ -51,8 +51,7 @@ gen_iternext(PyGenObject *gen)
* may keep a chain of frames alive or it could create a reference * may keep a chain of frames alive or it could create a reference
* cycle. */ * cycle. */
assert(f->f_back != NULL); assert(f->f_back != NULL);
Py_DECREF(f->f_back); Py_CLEAR(f->f_back);
f->f_back = NULL;
/* If the generator just returned (as opposed to yielding), signal /* If the generator just returned (as opposed to yielding), signal
* that the generator is exhausted. */ * that the generator is exhausted. */
......
...@@ -192,18 +192,14 @@ calliter_iternext(calliterobject *it) ...@@ -192,18 +192,14 @@ calliter_iternext(calliterobject *it)
return result; /* Common case, fast path */ return result; /* Common case, fast path */
Py_DECREF(result); Py_DECREF(result);
if (ok > 0) { if (ok > 0) {
Py_DECREF(it->it_callable); Py_CLEAR(it->it_callable);
it->it_callable = NULL; Py_CLEAR(it->it_sentinel);
Py_DECREF(it->it_sentinel);
it->it_sentinel = NULL;
} }
} }
else if (PyErr_ExceptionMatches(PyExc_StopIteration)) { else if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
PyErr_Clear(); PyErr_Clear();
Py_DECREF(it->it_callable); Py_CLEAR(it->it_callable);
it->it_callable = NULL; Py_CLEAR(it->it_sentinel);
Py_DECREF(it->it_sentinel);
it->it_sentinel = NULL;
} }
} }
return NULL; return NULL;
......
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