]> granicus.if.org Git - python/commitdiff
Crashers of the day: Py_CLEAR must be used when there is a chance that the
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Sat, 16 Feb 2008 14:34:57 +0000 (14:34 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Sat, 16 Feb 2008 14:34:57 +0000 (14:34 +0000)
function can be called recursively.
This was discussed in issue1020188.

In python codebase, all occurrences of Py_[X]DECREF(xxx->yyy) are suspect,
except when they appear in tp_new or tp_dealloc functions, or when
the member cannot be of a user-defined class.
Note that tp_init is not safe.

I do have a (crashing) example for every changed line.
Is it worth adding them to the test suite?

Example:

class SpecialStr(str):
    def __del__(self):
        s.close()

import cStringIO
s = cStringIO.StringIO(SpecialStr("text"))
s.close() # Segfault

Modules/_struct.c
Modules/cStringIO.c

index 6149964eccf2da9ba4affc229b8a086f62970247..8e5420c7f7e018661c87b58daf1fa3c5a9a10dfe 100644 (file)
@@ -1471,7 +1471,7 @@ s_init(PyObject *self, PyObject *args, PyObject *kwds)
                return -1;
 
        Py_INCREF(o_format);
-       Py_XDECREF(soself->s_format);
+       Py_CLEAR(soself->s_format);
        soself->s_format = o_format;
 
        ret = prepare_s(soself);
index 3529047b909b9ff9bb0b9421d234eb501afc38b3..139a4a83918651b325745818ce1d290bf2a8e76b 100644 (file)
@@ -575,8 +575,7 @@ newOobject(int  size) {
 
 static PyObject *
 I_close(Iobject *self, PyObject *unused) {
-        Py_XDECREF(self->pbuf);
-        self->pbuf = NULL;
+        Py_CLEAR(self->pbuf);
         self->buf = NULL;
 
         self->pos = self->string_size = 0;