From f573177c352c2df89c7d5ffd425a37b46b12166c Mon Sep 17 00:00:00 2001 From: Joseph Sutton Date: Mon, 24 Apr 2023 10:42:39 +1200 Subject: python: Safely clear structure members Using Py_CLEAR() ensures that these structures are observed in a consistent state by any Python code that may run during deconstruction. Signed-off-by: Joseph Sutton Reviewed-by: Andrew Bartlett --- lib/ldb/pyldb.c | 19 ++++++++----------- lib/tdb/pytdb.c | 2 +- lib/tevent/pytevent.c | 4 ++-- source3/libsmb/pylibsmb.c | 8 ++------ source4/librpc/rpc/pyrpc.c | 5 ++--- 5 files changed, 15 insertions(+), 23 deletions(-) diff --git a/lib/ldb/pyldb.c b/lib/ldb/pyldb.c index aa38e115ce4..11d093c0429 100644 --- a/lib/ldb/pyldb.c +++ b/lib/ldb/pyldb.c @@ -2134,10 +2134,7 @@ static int py_ldb_search_iterator_reply_destructor(struct py_ldb_search_iterator reply->py_iter = NULL; } - if (reply->obj != NULL) { - Py_DECREF(reply->obj); - reply->obj = NULL; - } + Py_CLEAR(reply->obj); return 0; } @@ -2679,9 +2676,9 @@ static PyTypeObject PyLdb = { static void py_ldb_result_dealloc(PyLdbResultObject *self) { talloc_free(self->mem_ctx); - Py_DECREF(self->msgs); - Py_DECREF(self->referals); - Py_DECREF(self->controls); + Py_CLEAR(self->msgs); + Py_CLEAR(self->referals); + Py_CLEAR(self->controls); Py_TYPE(self)->tp_free(self); } @@ -2775,10 +2772,10 @@ static PyTypeObject PyLdbResult = { static void py_ldb_search_iterator_dealloc(PyLdbSearchIteratorObject *self) { - Py_XDECREF(self->state.exception); + Py_CLEAR(self->state.exception); TALLOC_FREE(self->mem_ctx); ZERO_STRUCT(self->state); - Py_DECREF(self->ldb); + Py_CLEAR(self->ldb); Py_TYPE(self)->tp_free(self); } @@ -2885,7 +2882,7 @@ static PyObject *py_ldb_search_iterator_abandon(PyLdbSearchIteratorObject *self, return NULL; } - Py_XDECREF(self->state.exception); + Py_CLEAR(self->state.exception); TALLOC_FREE(self->mem_ctx); ZERO_STRUCT(self->state); Py_RETURN_NONE; @@ -4289,7 +4286,7 @@ static int py_module_del_transaction(struct ldb_module *mod) static int py_module_destructor(struct ldb_module *mod) { - Py_DECREF((PyObject *)mod->private_data); + Py_CLEAR(mod->private_data); return 0; } diff --git a/lib/tdb/pytdb.c b/lib/tdb/pytdb.c index 85df1b18621..ed22803328c 100644 --- a/lib/tdb/pytdb.c +++ b/lib/tdb/pytdb.c @@ -450,7 +450,7 @@ static PyObject *tdb_iter_next(PyTdbIteratorObject *self) static void tdb_iter_dealloc(PyTdbIteratorObject *self) { - Py_DECREF(self->iteratee); + Py_CLEAR(self->iteratee); PyObject_Del(self); } diff --git a/lib/tevent/pytevent.c b/lib/tevent/pytevent.c index 1af6f16c0fb..aa2331c1d6c 100644 --- a/lib/tevent/pytevent.c +++ b/lib/tevent/pytevent.c @@ -241,7 +241,7 @@ static void py_tevent_timer_dealloc(TeventTimer_Object *self) if (self->timer) { talloc_free(self->timer); } - Py_DECREF(self->callback); + Py_CLEAR(self->callback); PyObject_Del(self); } @@ -282,7 +282,7 @@ struct TeventTimer_Object_ref { static int TeventTimer_Object_ref_destructor(struct TeventTimer_Object_ref *ref) { ref->obj->timer = NULL; - Py_DECREF(ref->obj); + Py_CLEAR(ref->obj); return 0; } diff --git a/source3/libsmb/pylibsmb.c b/source3/libsmb/pylibsmb.c index 571681aee91..bace98ddd3a 100644 --- a/source3/libsmb/pylibsmb.c +++ b/source3/libsmb/pylibsmb.c @@ -1668,10 +1668,7 @@ struct py_cli_notify_state { static void py_cli_notify_state_dealloc(struct py_cli_notify_state *self) { TALLOC_FREE(self->req); - if (self->py_cli_state != NULL) { - Py_DECREF(self->py_cli_state); - self->py_cli_state = NULL; - } + Py_CLEAR(self->py_cli_state); Py_TYPE(self)->tp_free(self); } @@ -1823,8 +1820,7 @@ static PyObject *py_cli_notify_get_changes(struct py_cli_notify_state *self, ok = py_tevent_req_wait_exc(py_cli_state, req); self->req = NULL; - Py_DECREF(self->py_cli_state); - self->py_cli_state = NULL; + Py_CLEAR(self->py_cli_state); if (!ok) { return NULL; } diff --git a/source4/librpc/rpc/pyrpc.c b/source4/librpc/rpc/pyrpc.c index 7b6bcef9eba..61b3c6357b0 100644 --- a/source4/librpc/rpc/pyrpc.c +++ b/source4/librpc/rpc/pyrpc.c @@ -492,8 +492,7 @@ static void py_dcerpc_ndr_pointer_dealloc(PyObject* self) struct py_dcerpc_ndr_pointer *obj = pytalloc_get_type(self, struct py_dcerpc_ndr_pointer); - Py_DECREF(obj->value); - obj->value = NULL; + Py_CLEAR(obj->value); self->ob_type->tp_free(self); } @@ -512,7 +511,7 @@ static int py_dcerpc_ndr_pointer_set_value(PyObject *self, PyObject *value, void struct py_dcerpc_ndr_pointer *obj = pytalloc_get_type(self, struct py_dcerpc_ndr_pointer); - Py_DECREF(obj->value); + Py_CLEAR(obj->value); obj->value = value; Py_INCREF(obj->value); return 0; -- cgit v1.2.1