]> granicus.if.org Git - python/commitdiff
SF patch #1020188: Use Py_CLEAR where necessary to avoid crashes
authorRaymond Hettinger <python@rcn.com>
Wed, 1 Sep 2004 07:02:44 +0000 (07:02 +0000)
committerRaymond Hettinger <python@rcn.com>
Wed, 1 Sep 2004 07:02:44 +0000 (07:02 +0000)
(Contributed by Dima Dorfman)

Include/object.h
Modules/itertoolsmodule.c
Objects/enumobject.c
Objects/genobject.c
Objects/iterobject.c

index 5db4dac06109e80abf974e0f43f0bf81390f9501..258b0744968c47f4b2ab166b0855114a8f432489 100644 (file)
@@ -623,7 +623,7 @@ PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
 #define Py_CLEAR(op)                           \
         do {                                   \
                 if (op) {                      \
-                        PyObject *tmp = (op);  \
+                        PyObject *tmp = (PyObject *)(op);      \
                         (op) = NULL;           \
                         Py_DECREF(tmp);                \
                 }                              \
index 21659fbca2e1606bcfb71fae0c6364f2814e5a84..bf2c49311ffb36154f59e0ab40e020f1342f53ef 100644 (file)
@@ -250,8 +250,7 @@ _grouper_next(_grouperobject *igo)
 
        r = gbo->currvalue;
        gbo->currvalue = NULL;
-       Py_DECREF(gbo->currkey);
-       gbo->currkey = NULL;
+       Py_CLEAR(gbo->currkey);
 
        return r;
 }
index ebe7e7c10f27fba1e26fd68b3fae889f13f19ae8..aac88dbb0129698656979943913e7d2c029299f6 100644 (file)
@@ -230,10 +230,7 @@ reversed_next(reversedobject *ro)
                        PyErr_Clear();
        }
        ro->index = -1;
-       if (ro->seq != NULL) {
-               Py_DECREF(ro->seq);
-               ro->seq = NULL;
-       }
+       Py_CLEAR(ro->seq);
        return NULL;
 }
 
index 66a5106487aae2bacd6a299654572dd2e99b392f..174c697137349d6a2e8c42e26c8ca7eb25f807c1 100644 (file)
@@ -51,8 +51,7 @@ gen_iternext(PyGenObject *gen)
         * may keep a chain of frames alive or it could create a reference
         * cycle. */
        assert(f->f_back != NULL);
-       Py_DECREF(f->f_back);
-       f->f_back = NULL;
+       Py_CLEAR(f->f_back);
 
        /* If the generator just returned (as opposed to yielding), signal
         * that the generator is exhausted. */
index 25e4e11154a937bbcd825036e8ff4975a2981981..b414cc2e9f47ffebfb3f6f499fde769feb423b4e 100644 (file)
@@ -192,18 +192,14 @@ calliter_iternext(calliterobject *it)
                                return result; /* Common case, fast path */
                        Py_DECREF(result);
                        if (ok > 0) {
-                               Py_DECREF(it->it_callable);
-                               it->it_callable = NULL;
-                               Py_DECREF(it->it_sentinel);
-                               it->it_sentinel = NULL;
+                               Py_CLEAR(it->it_callable);
+                               Py_CLEAR(it->it_sentinel);
                        }
                }
                else if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
                        PyErr_Clear();
-                       Py_DECREF(it->it_callable);
-                       it->it_callable = NULL;
-                       Py_DECREF(it->it_sentinel);
-                       it->it_sentinel = NULL;
+                       Py_CLEAR(it->it_callable);
+                       Py_CLEAR(it->it_sentinel);
                }
        }
        return NULL;