From 98a9722e4a82b5f7dc0411233bbec892a02c7667 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 9 Feb 2014 13:14:04 +0200 Subject: Issue #20437: Fixed 43 potential bugs when deleting objects references. --- Mac/Modules/carbonevt/_CarbonEvtmodule.c | 3 +-- Mac/Modules/list/_Listmodule.c | 3 +-- Misc/NEWS | 2 ++ Modules/_bsddb.c | 21 +++++++-------------- Modules/_ctypes/_ctypes.c | 18 ++++++------------ Modules/_elementtree.c | 8 ++++---- Modules/_sqlite/connection.c | 3 +-- Modules/_sqlite/cursor.c | 9 +++------ Modules/cPickle.c | 3 +-- Modules/cdmodule.c | 18 ++++++------------ Modules/flmodule.c | 12 ++++-------- Modules/readline.c | 3 +-- Modules/selectmodule.c | 3 +-- Modules/svmodule.c | 3 +-- Objects/tupleobject.c | 6 ++---- Objects/unicodeobject.c | 6 ++---- Python/ceval.c | 3 +-- Python/sysmodule.c | 3 +-- 18 files changed, 45 insertions(+), 82 deletions(-) diff --git a/Mac/Modules/carbonevt/_CarbonEvtmodule.c b/Mac/Modules/carbonevt/_CarbonEvtmodule.c index 623a3e5..30d40c9 100644 --- a/Mac/Modules/carbonevt/_CarbonEvtmodule.c +++ b/Mac/Modules/carbonevt/_CarbonEvtmodule.c @@ -1051,8 +1051,7 @@ static PyObject *EventHandlerRef_RemoveEventHandler(EventHandlerRefObject *_self _err = RemoveEventHandler(_self->ob_itself); if (_err != noErr) return PyMac_Error(_err); _self->ob_itself = NULL; - Py_DECREF(_self->ob_callback); - _self->ob_callback = NULL; + Py_CLEAR(_self->ob_callback); Py_INCREF(Py_None); _res = Py_None; return _res; diff --git a/Mac/Modules/list/_Listmodule.c b/Mac/Modules/list/_Listmodule.c index 3f43b06..9e63686 100644 --- a/Mac/Modules/list/_Listmodule.c +++ b/Mac/Modules/list/_Listmodule.c @@ -76,8 +76,7 @@ int ListObj_Convert(PyObject *v, ListHandle *p_itself) static void ListObj_dealloc(ListObject *self) { - Py_XDECREF(self->ob_ldef_func); - self->ob_ldef_func = NULL; + Py_CLEAR(self->ob_ldef_func); SetListRefCon(self->ob_itself, (long)0); if (self->ob_must_be_disposed && self->ob_itself) LDispose(self->ob_itself); self->ob_type->tp_free((PyObject *)self); diff --git a/Misc/NEWS b/Misc/NEWS index 05413fe..f63d2a6 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -9,6 +9,8 @@ What's New in Python 2.7.7? Core and Builtins ----------------- +- Issue #20437: Fixed 43 potential bugs when deleting objects references. + - Issue #20494: Ensure that free()d memory arenas are really released on POSIX systems supporting anonymous memory mappings. Patch by Charles-François Natali. diff --git a/Modules/_bsddb.c b/Modules/_bsddb.c index dad1dcd..203bba2 100644 --- a/Modules/_bsddb.c +++ b/Modules/_bsddb.c @@ -949,8 +949,7 @@ newDBObject(DBEnvObject* arg, int flags) * DBTxns and closing any open DBs first. */ if (makeDBError(err)) { if (self->myenvobj) { - Py_DECREF(self->myenvobj); - self->myenvobj = NULL; + Py_CLEAR(self->myenvobj); } Py_DECREF(self); self = NULL; @@ -982,20 +981,16 @@ DB_dealloc(DBObject* self) PyObject_ClearWeakRefs((PyObject *) self); } if (self->myenvobj) { - Py_DECREF(self->myenvobj); - self->myenvobj = NULL; + Py_CLEAR(self->myenvobj); } if (self->associateCallback != NULL) { - Py_DECREF(self->associateCallback); - self->associateCallback = NULL; + Py_CLEAR(self->associateCallback); } if (self->btCompareCallback != NULL) { - Py_DECREF(self->btCompareCallback); - self->btCompareCallback = NULL; + Py_CLEAR(self->btCompareCallback); } if (self->dupCompareCallback != NULL) { - Py_DECREF(self->dupCompareCallback); - self->dupCompareCallback = NULL; + Py_CLEAR(self->dupCompareCallback); } Py_DECREF(self->private_obj); PyObject_Del(self); @@ -1160,8 +1155,7 @@ DBEnv_dealloc(DBEnvObject* self) PyErr_Clear(); } - Py_XDECREF(self->event_notifyCallback); - self->event_notifyCallback = NULL; + Py_CLEAR(self->event_notifyCallback); if (self->in_weakreflist != NULL) { PyObject_ClearWeakRefs((PyObject *) self); @@ -1640,8 +1634,7 @@ DB_associate(DBObject* self, PyObject* args, PyObject* kwargs) MYDB_END_ALLOW_THREADS; if (err) { - Py_XDECREF(secondaryDB->associateCallback); - secondaryDB->associateCallback = NULL; + Py_CLEAR(secondaryDB->associateCallback); secondaryDB->primaryDBType = 0; } diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 599d90a..8ba6443 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -194,10 +194,8 @@ _DictRemover_call(PyObject *_self, PyObject *args, PyObject *kw) if (-1 == PyDict_DelItem(self->dict, self->key)) /* XXX Error context */ PyErr_WriteUnraisable(Py_None); - Py_DECREF(self->key); - self->key = NULL; - Py_DECREF(self->dict); - self->dict = NULL; + Py_CLEAR(self->key); + Py_CLEAR(self->dict); } Py_INCREF(Py_None); return Py_None; @@ -3040,10 +3038,8 @@ static int PyCFuncPtr_set_restype(PyCFuncPtrObject *self, PyObject *ob) { if (ob == NULL) { - Py_XDECREF(self->restype); - self->restype = NULL; - Py_XDECREF(self->checker); - self->checker = NULL; + Py_CLEAR(self->restype); + Py_CLEAR(self->checker); return 0; } if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) { @@ -3086,10 +3082,8 @@ PyCFuncPtr_set_argtypes(PyCFuncPtrObject *self, PyObject *ob) PyObject *converters; if (ob == NULL || ob == Py_None) { - Py_XDECREF(self->converters); - self->converters = NULL; - Py_XDECREF(self->argtypes); - self->argtypes = NULL; + Py_CLEAR(self->converters); + Py_CLEAR(self->argtypes); } else { converters = converters_from_argtypes(ob); if (!converters) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index ae07ca3..3e9f9df 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -2739,10 +2739,10 @@ xmlparser_setevents(XMLParserObject* self, PyObject* args) target->events = events; /* clear out existing events */ - Py_XDECREF(target->start_event_obj); target->start_event_obj = NULL; - Py_XDECREF(target->end_event_obj); target->end_event_obj = NULL; - Py_XDECREF(target->start_ns_event_obj); target->start_ns_event_obj = NULL; - Py_XDECREF(target->end_ns_event_obj); target->end_ns_event_obj = NULL; + Py_CLEAR(target->start_event_obj); + Py_CLEAR(target->end_event_obj); + Py_CLEAR(target->start_ns_event_obj); + Py_CLEAR(target->end_ns_event_obj); if (event_set == Py_None) { /* default is "end" only */ diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 59966dad..7a8a5a1 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -369,8 +369,7 @@ PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args) if (self->apsw_connection) { ret = PyObject_CallMethod(self->apsw_connection, "close", ""); Py_XDECREF(ret); - Py_XDECREF(self->apsw_connection); - self->apsw_connection = NULL; + Py_CLEAR(self->apsw_connection); self->db = NULL; } else { Py_BEGIN_ALLOW_THREADS diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index f06f92c..3b84484 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -231,8 +231,7 @@ int pysqlite_build_row_cast_map(pysqlite_Cursor* self) if (converter != Py_None) { Py_DECREF(converter); } - Py_XDECREF(self->row_cast_map); - self->row_cast_map = NULL; + Py_CLEAR(self->row_cast_map); return -1; } @@ -468,8 +467,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* allow_8bit_chars = ((self->connection->text_factory != (PyObject*)&PyUnicode_Type) && (self->connection->text_factory != pysqlite_OptimizedUnicode)); - Py_XDECREF(self->next_row); - self->next_row = NULL; + Py_CLEAR(self->next_row); if (multiple) { /* executemany() */ @@ -896,8 +894,7 @@ PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self) if (!self->next_row) { if (self->statement) { (void)pysqlite_statement_reset(self->statement); - Py_DECREF(self->statement); - self->statement = NULL; + Py_CLEAR(self->statement); } return NULL; } diff --git a/Modules/cPickle.c b/Modules/cPickle.c index 8145bbf..10199eb 100644 --- a/Modules/cPickle.c +++ b/Modules/cPickle.c @@ -324,8 +324,7 @@ Pdata_popList(Pdata *self, Py_ssize_t start) #define FREE_ARG_TUP(self) { \ if (Py_REFCNT(self->arg) > 1) { \ - Py_DECREF(self->arg); \ - self->arg=NULL; \ + Py_CLEAR(self->arg); \ } \ } diff --git a/Modules/cdmodule.c b/Modules/cdmodule.c index 8dfb769..9ee9b0b 100644 --- a/Modules/cdmodule.c +++ b/Modules/cdmodule.c @@ -535,10 +535,8 @@ CD_deleteparser(cdparserobject *self, PyObject *args) /* no sense in keeping the callbacks, so remove them */ for (i = 0; i < NCALLBACKS; i++) { - Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallback); - self->ob_cdcallbacks[i].ob_cdcallback = NULL; - Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallbackarg); - self->ob_cdcallbacks[i].ob_cdcallbackarg = NULL; + Py_CLEAR(self->ob_cdcallbacks[i].ob_cdcallback); + Py_CLEAR(self->ob_cdcallbacks[i].ob_cdcallbackarg); } Py_INCREF(Py_None); @@ -588,11 +586,9 @@ CD_removecallback(cdparserobject *self, PyObject *args) CDremovecallback(self->ob_cdparser, (CDDATATYPES) type); - Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallback); - self->ob_cdcallbacks[type].ob_cdcallback = NULL; + Py_CLEAR(self->ob_cdcallbacks[type].ob_cdcallback); - Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallbackarg); - self->ob_cdcallbacks[type].ob_cdcallbackarg = NULL; + Py_CLEAR(self->ob_cdcallbacks[type].ob_cdcallbackarg); Py_INCREF(Py_None); return Py_None; @@ -668,10 +664,8 @@ cdparser_dealloc(cdparserobject *self) int i; for (i = 0; i < NCALLBACKS; i++) { - Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallback); - self->ob_cdcallbacks[i].ob_cdcallback = NULL; - Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallbackarg); - self->ob_cdcallbacks[i].ob_cdcallbackarg = NULL; + Py_CLEAR(self->ob_cdcallbacks[i].ob_cdcallback); + Py_CLEAR(self->ob_cdcallbacks[i].ob_cdcallbackarg); } CDdeleteparser(self->ob_cdparser); PyObject_Del(self); diff --git a/Modules/flmodule.c b/Modules/flmodule.c index b5a78cf..0ddf600 100644 --- a/Modules/flmodule.c +++ b/Modules/flmodule.c @@ -97,10 +97,8 @@ forgetgeneric(genericobject *g) { int i, n; - Py_XDECREF(g->ob_callback); - g->ob_callback = NULL; - Py_XDECREF(g->ob_callback_arg); - g->ob_callback_arg = NULL; + Py_CLEAR(g->ob_callback); + Py_CLEAR(g->ob_callback_arg); if (allgenerics == NULL) return; /* No objects known yet */ n = PyList_Size(allgenerics); @@ -132,10 +130,8 @@ releaseobjects(FL_FORM *form) /* The object is now unreachable for do_forms and check_forms, so delete it from the list of known objects */ - Py_XDECREF(g->ob_callback); - g->ob_callback = NULL; - Py_XDECREF(g->ob_callback_arg); - g->ob_callback_arg = NULL; + Py_CLEAR(g->ob_callback); + Py_CLEAR(g->ob_callback_arg); PyList_SetItem(allgenerics, i, (PyObject *)NULL); nfreeslots++; } diff --git a/Modules/readline.c b/Modules/readline.c index a5d48c9..f19fa0b 100644 --- a/Modules/readline.c +++ b/Modules/readline.c @@ -206,8 +206,7 @@ set_hook(const char *funcname, PyObject **hook_var, PyObject *args) if (!PyArg_ParseTuple(args, buf, &function)) return NULL; if (function == Py_None) { - Py_XDECREF(*hook_var); - *hook_var = NULL; + Py_CLEAR(*hook_var); } else if (PyCallable_Check(function)) { PyObject *tmp = *hook_var; diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c index 6071996..2707b05 100644 --- a/Modules/selectmodule.c +++ b/Modules/selectmodule.c @@ -71,8 +71,7 @@ reap_obj(pylist fd2obj[FD_SETSIZE + 1]) { int i; for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) { - Py_XDECREF(fd2obj[i].obj); - fd2obj[i].obj = NULL; + Py_CLEAR(fd2obj[i].obj); } fd2obj[0].sentinel = -1; } diff --git a/Modules/svmodule.c b/Modules/svmodule.c index 6e419ce..1519065 100644 --- a/Modules/svmodule.c +++ b/Modules/svmodule.c @@ -279,8 +279,7 @@ capture_dealloc(captureobject *self) (void)svUnlockCaptureData(self->ob_svideo->ob_svideo, self->ob_capture); self->ob_capture = NULL; - Py_DECREF(self->ob_svideo); - self->ob_svideo = NULL; + Py_CLEAR(self->ob_svideo); } PyObject_Del(self); } diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 00f2e47..a3c185e 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -881,8 +881,7 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize) _Py_ForgetReference((PyObject *) v); /* DECREF items deleted by shrinkage */ for (i = newsize; i < oldsize; i++) { - Py_XDECREF(v->ob_item[i]); - v->ob_item[i] = NULL; + Py_CLEAR(v->ob_item[i]); } sv = PyObject_GC_Resize(PyTupleObject, v, newsize); if (sv == NULL) { @@ -928,8 +927,7 @@ PyTuple_Fini(void) #if PyTuple_MAXSAVESIZE > 0 /* empty tuples are used all over the place and applications may * rely on the fact that an empty tuple is a singleton. */ - Py_XDECREF(free_list[0]); - free_list[0] = NULL; + Py_CLEAR(free_list[0]); (void)PyTuple_ClearFreeList(); #endif diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 5ce9c88..09e41b9 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -3520,8 +3520,7 @@ static void make_encode_exception(PyObject **exceptionObject, goto onError; return; onError: - Py_DECREF(*exceptionObject); - *exceptionObject = NULL; + Py_CLEAR(*exceptionObject); } } @@ -4826,8 +4825,7 @@ static void make_translate_exception(PyObject **exceptionObject, goto onError; return; onError: - Py_DECREF(*exceptionObject); - *exceptionObject = NULL; + Py_CLEAR(*exceptionObject); } } diff --git a/Python/ceval.c b/Python/ceval.c index 06ada97..18bc66b 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -3240,8 +3240,7 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals, if (co->co_flags & CO_GENERATOR) { /* Don't need to keep the reference to f_back, it will be set * when the generator is resumed. */ - Py_XDECREF(f->f_back); - f->f_back = NULL; + Py_CLEAR(f->f_back); PCALL(PCALL_GENERATOR); diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 2c364af..560ea4e 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -367,8 +367,7 @@ trace_trampoline(PyObject *self, PyFrameObject *frame, result = call_trampoline(tstate, callback, frame, what, arg); if (result == NULL) { PyEval_SetTrace(NULL, NULL); - Py_XDECREF(frame->f_trace); - frame->f_trace = NULL; + Py_CLEAR(frame->f_trace); return -1; } if (result != Py_None) { -- cgit v0.12