From: Douglas Bagnall Date: Tue, 9 Jul 2019 08:59:19 +0000 (+0000) Subject: talloc: add pytalloc_get_name() helper X-Git-Tag: talloc-2.3.0~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4496e073cbd0f78bcaa2cf340336e1a14bd6e8e6;p=thirdparty%2Fsamba.git talloc: add pytalloc_get_name() helper In several places we go talloc_get_name(pytalloc_get_ptr(py_obj)) which is a certain NULL derefernce if py_obj is not a talloc object. This is a helper function that chooses to say "non-talloc object" rather than crash. Signed-off-by: Douglas Bagnall Reviewed-by: Gary Lockyer Reviewed-by: Andrew Bartlett --- diff --git a/lib/talloc/pytalloc.h b/lib/talloc/pytalloc.h index 7db6c33cf01..8ab1e16fe47 100644 --- a/lib/talloc/pytalloc.h +++ b/lib/talloc/pytalloc.h @@ -54,6 +54,10 @@ void *_pytalloc_get_ptr(PyObject *py_obj); TALLOC_CTX *_pytalloc_get_mem_ctx(PyObject *py_obj); #define pytalloc_get_mem_ctx(py_obj) _pytalloc_get_mem_ctx((PyObject *)(py_obj)) +const char *_pytalloc_get_name(PyObject *py_obj); +#define pytalloc_get_name(py_obj) _pytalloc_get_name((PyObject *)(py_obj)) + + PyObject *pytalloc_steal_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx, void *ptr); PyObject *pytalloc_steal(PyTypeObject *py_type, void *ptr); PyObject *pytalloc_reference_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx, void *ptr); diff --git a/lib/talloc/pytalloc_util.c b/lib/talloc/pytalloc_util.c index 7a426d6c2a6..82b95e7f144 100644 --- a/lib/talloc/pytalloc_util.c +++ b/lib/talloc/pytalloc_util.c @@ -331,3 +331,12 @@ _PUBLIC_ int pytalloc_BaseObject_PyType_Ready(PyTypeObject *type) return PyType_Ready(type); } + +_PUBLIC_ const char *_pytalloc_get_name(PyObject *obj) +{ + void *ptr = pytalloc_get_ptr(obj); + if (ptr == NULL) { + return "non-talloc object"; + } + return talloc_get_name(ptr); +}