Skip to content

Commit dfe98a1

Browse files
Issue #20437: Fixed 22 potential bugs when deleting objects references.
2 parents 2623c8c + 505ff75 commit dfe98a1

File tree

15 files changed

+34
-63
lines changed

15 files changed

+34
-63
lines changed

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Release date: 2014-02-09
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #20437: Fixed 22 potential bugs when deleting objects references.
14+
1315
- Issue #20500: Displaying an exception at interpreter shutdown no longer
1416
risks triggering an assertion failure in PyObject_Str.
1517

Modules/_ctypes/_ctypes.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,8 @@ _DictRemover_call(PyObject *myself, PyObject *args, PyObject *kw)
159159
if (-1 == PyDict_DelItem(self->dict, self->key))
160160
/* XXX Error context */
161161
PyErr_WriteUnraisable(Py_None);
162-
Py_DECREF(self->key);
163-
self->key = NULL;
164-
Py_DECREF(self->dict);
165-
self->dict = NULL;
162+
Py_CLEAR(self->key);
163+
Py_CLEAR(self->dict);
166164
}
167165
Py_INCREF(Py_None);
168166
return Py_None;
@@ -2934,10 +2932,8 @@ static int
29342932
PyCFuncPtr_set_restype(PyCFuncPtrObject *self, PyObject *ob)
29352933
{
29362934
if (ob == NULL) {
2937-
Py_XDECREF(self->restype);
2938-
self->restype = NULL;
2939-
Py_XDECREF(self->checker);
2940-
self->checker = NULL;
2935+
Py_CLEAR(self->restype);
2936+
Py_CLEAR(self->checker);
29412937
return 0;
29422938
}
29432939
if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) {
@@ -2980,10 +2976,8 @@ PyCFuncPtr_set_argtypes(PyCFuncPtrObject *self, PyObject *ob)
29802976
PyObject *converters;
29812977

29822978
if (ob == NULL || ob == Py_None) {
2983-
Py_XDECREF(self->converters);
2984-
self->converters = NULL;
2985-
Py_XDECREF(self->argtypes);
2986-
self->argtypes = NULL;
2979+
Py_CLEAR(self->converters);
2980+
Py_CLEAR(self->argtypes);
29872981
} else {
29882982
converters = converters_from_argtypes(ob);
29892983
if (!converters)

Modules/_sqlite/cursor.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,7 @@ int pysqlite_build_row_cast_map(pysqlite_Cursor* self)
229229
if (converter != Py_None) {
230230
Py_DECREF(converter);
231231
}
232-
Py_XDECREF(self->row_cast_map);
233-
self->row_cast_map = NULL;
232+
Py_CLEAR(self->row_cast_map);
234233

235234
return -1;
236235
}
@@ -447,8 +446,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
447446
self->locked = 1;
448447
self->reset = 0;
449448

450-
Py_XDECREF(self->next_row);
451-
self->next_row = NULL;
449+
Py_CLEAR(self->next_row);
452450

453451
if (multiple) {
454452
/* executemany() */
@@ -864,8 +862,7 @@ PyObject* pysqlite_cursor_iternext(pysqlite_Cursor *self)
864862
if (!self->next_row) {
865863
if (self->statement) {
866864
(void)pysqlite_statement_reset(self->statement);
867-
Py_DECREF(self->statement);
868-
self->statement = NULL;
865+
Py_CLEAR(self->statement);
869866
}
870867
return NULL;
871868
}

Modules/posixmodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,7 @@ typedef struct {
780780
static void
781781
path_cleanup(path_t *path) {
782782
if (path->cleanup) {
783-
Py_DECREF(path->cleanup);
784-
path->cleanup = NULL;
783+
Py_CLEAR(path->cleanup);
785784
}
786785
}
787786

Modules/pyexpat.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,7 @@ call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args,
317317
}
318318
else {
319319
if (trace_frame(tstate, f, PyTrace_RETURN, res) < 0) {
320-
Py_XDECREF(res);
321-
res = NULL;
320+
Py_CLEAR(res);
322321
}
323322
}
324323
#else

Modules/readline.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
281281
if (!PyArg_ParseTuple(args, buf, &function))
282282
return NULL;
283283
if (function == Py_None) {
284-
Py_XDECREF(*hook_var);
285-
*hook_var = NULL;
284+
Py_CLEAR(*hook_var);
286285
}
287286
else if (PyCallable_Check(function)) {
288287
PyObject *tmp = *hook_var;
@@ -885,7 +884,7 @@ on_completion_display_matches_hook(char **matches,
885884
(r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
886885
goto error;
887886
}
888-
Py_XDECREF(r); r=NULL;
887+
Py_CLEAR(r);
889888

890889
if (0) {
891890
error:

Modules/selectmodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ reap_obj(pylist fd2obj[FD_SETSIZE + 1])
6666
{
6767
int i;
6868
for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) {
69-
Py_XDECREF(fd2obj[i].obj);
70-
fd2obj[i].obj = NULL;
69+
Py_CLEAR(fd2obj[i].obj);
7170
}
7271
fd2obj[0].sentinel = -1;
7372
}

Modules/signalmodule.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,12 +1305,9 @@ finisignal(void)
13051305
Py_XDECREF(func);
13061306
}
13071307

1308-
Py_XDECREF(IntHandler);
1309-
IntHandler = NULL;
1310-
Py_XDECREF(DefaultHandler);
1311-
DefaultHandler = NULL;
1312-
Py_XDECREF(IgnoreHandler);
1313-
IgnoreHandler = NULL;
1308+
Py_CLEAR(IntHandler);
1309+
Py_CLEAR(DefaultHandler);
1310+
Py_CLEAR(IgnoreHandler);
13141311
}
13151312

13161313

Modules/syslogmodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ syslog_closelog(PyObject *self, PyObject *unused)
197197
{
198198
if (S_log_open) {
199199
closelog();
200-
Py_XDECREF(S_ident_o);
201-
S_ident_o = NULL;
200+
Py_CLEAR(S_ident_o);
202201
S_log_open = 0;
203202
}
204203
Py_INCREF(Py_None);

Modules/zlibmodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,7 @@ zlib_compressobj_impl(PyModuleDef *module, int level, int method, int wbits, int
463463
}
464464

465465
error:
466-
Py_XDECREF(self);
467-
self = NULL;
466+
Py_CLEAR(self);
468467
success:
469468
return (PyObject*)self;
470469
}

0 commit comments

Comments
 (0)