Skip to content

Commit 7bdd036

Browse files
author
amaury.forgeotdarc
committed
Crashers of the day: Py_CLEAR must be used when there is a chance that the
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 git-svn-id: http://svn.python.org/projects/python/trunk@60860 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 8f87244 commit 7bdd036

2 files changed

Lines changed: 2 additions & 3 deletions

File tree

Modules/_struct.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,7 @@ s_init(PyObject *self, PyObject *args, PyObject *kwds)
14711471
return -1;
14721472

14731473
Py_INCREF(o_format);
1474-
Py_XDECREF(soself->s_format);
1474+
Py_CLEAR(soself->s_format);
14751475
soself->s_format = o_format;
14761476

14771477
ret = prepare_s(soself);

Modules/cStringIO.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,7 @@ newOobject(int size) {
575575

576576
static PyObject *
577577
I_close(Iobject *self, PyObject *unused) {
578-
Py_XDECREF(self->pbuf);
579-
self->pbuf = NULL;
578+
Py_CLEAR(self->pbuf);
580579
self->buf = NULL;
581580

582581
self->pos = self->string_size = 0;

0 commit comments

Comments
 (0)