diff options
author | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2008-07-14 15:39:02 +0100 |
---|---|---|
committer | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2008-07-14 15:39:02 +0100 |
commit | ac83797b39b1d0eacaafcdf42772eab8ca91ce63 (patch) | |
tree | 9b85a30ecd8789278a73569e155ee7fb200ae60c | |
parent | 9774cdade2306b9bb641162a14645510fc822c86 (diff) | |
download | dbus-python-ac83797b39b1d0eacaafcdf42772eab8ca91ce63.tar.gz |
DBusPyServer: refactor set_auth_mechanisms
* save a malloc
* return a boolean
* don't crash if the sequence isn't a sequence
* don't coerce items to strings too hard (we only want to accept str or
unicode, accepting FooObject and trying to use it as an authentication
method "<FooObject at 0xdeadbeef>" would be silly)
-rw-r--r-- | _dbus_bindings/server.c | 52 |
1 files changed, 29 insertions, 23 deletions
diff --git a/_dbus_bindings/server.c b/_dbus_bindings/server.c index ec92b3b..d1d9c9d 100644 --- a/_dbus_bindings/server.c +++ b/_dbus_bindings/server.c @@ -76,42 +76,46 @@ DBusPyServer_BorrowDBusServer(PyObject *self) /* Internal C API =================================================== */ -static void -DBusPyServer_set_auth_mechanisms(Server *self, +static dbus_bool_t +DBusPyServer_set_auth_mechanisms(Server *self, PyObject *auth_mechanisms) { - const char **list = NULL; + PyObject *fast_seq; + Py_ssize_t length; + Py_ssize_t i; - if (auth_mechanisms) { - Py_ssize_t length; - Py_ssize_t i; + fast_seq = PySequence_Fast(auth_mechanisms, + "Expecting sequence for auth_mechanisms parameter"); - if (!PySequence_Check(auth_mechanisms)) { - PyErr_SetString(PyExc_TypeError, "Expecting sequence for auth_mechanisms parameter"); - return; - } + if (!fast_seq) + return FALSE; - length = PySequence_Fast_GET_SIZE(auth_mechanisms); + length = PySequence_Fast_GET_SIZE(fast_seq); - Py_BEGIN_ALLOW_THREADS - list = PyMem_New(const char*, length + 1); - Py_END_ALLOW_THREADS + /* scope for list */ + { + const char *list[length + 1]; for (i = 0; i < length; ++i) { - PyObject* am; + PyObject *am; am = PySequence_Fast_GET_ITEM(auth_mechanisms, i); - am = PyObject_Str(am); + /* this supports either str or unicode, raising TypeError + * on failure */ + list[i] = PyString_AsString(am); - list[i] = PyString_AS_STRING(am); + if (!list[i]) + return FALSE; } - } - Py_BEGIN_ALLOW_THREADS - dbus_server_set_auth_mechanisms(self->server, list); - Py_END_ALLOW_THREADS + list[length] = NULL; - PyMem_Free(list); + Py_BEGIN_ALLOW_THREADS + dbus_server_set_auth_mechanisms(self->server, list); + Py_END_ALLOW_THREADS + } + + return TRUE; } /* Return a new reference to a Python Server or subclass corresponding @@ -279,7 +283,9 @@ DBusPyServer_NewDBusServer(PyTypeObject *cls, !dbus_py_set_up_server((PyObject *)self, self->mainloop)) goto err; - DBusPyServer_set_auth_mechanisms(self, auth_mechanisms); + if (auth_mechanisms && auth_mechanisms != Py_None && + !DBusPyServer_set_auth_mechanisms(self, auth_mechanisms)) + goto err; Py_BEGIN_ALLOW_THREADS dbus_server_set_new_connection_function(self->server, |