From: Noel Power Date: Thu, 12 Apr 2018 13:46:59 +0000 (+0100) Subject: lib/ldb: Additionally accept unicode as string param in Py2 X-Git-Tag: ldb-1.4.0~516 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=db8da077ec5d2b23f2219dd8d667c85d38f56ca8;p=thirdparty%2Fsamba.git lib/ldb: Additionally accept unicode as string param in Py2 With the changes to make samba python code Py2/Py3 compatible there now are many instances where string content is decoded. Decoded string variables in Py2 are returned as the unicode type. Many Py2 c-module functions that take string arguments only check for the string type. However now it's quite possibe the content formally passed as a string argument is now passed as unicode after being decoded, such arguments are rejected and code can fail subtly. This only affects places where the type is directly checked e.g. via PyStr_Check etc. arguments that are parsed by ParseTuple* functions generally already accept both string and unicode (if 's', 'z', 's*' format specifiers are used) Signed-off-by: Noel Power Reviewed-by: Alexander Bokovoy --- diff --git a/lib/ldb/pyldb.c b/lib/ldb/pyldb.c index 67ecafbc420..110ec8e60ad 100644 --- a/lib/ldb/pyldb.c +++ b/lib/ldb/pyldb.c @@ -1078,7 +1078,7 @@ static const char **PyList_AsStrList(TALLOC_CTX *mem_ctx, PyObject *list, const char *str = NULL; Py_ssize_t size; PyObject *item = PyList_GetItem(list, i); - if (!PyStr_Check(item)) { + if (!(PyStr_Check(item) || PyUnicode_Check(item))) { PyErr_Format(PyExc_TypeError, "%s should be strings", paramname); talloc_free(ret); return NULL; @@ -2861,7 +2861,7 @@ static struct ldb_message_element *PyObject_AsMessageElement( me->name = talloc_strdup(me, attr_name); me->flags = flags; - if (PyBytes_Check(set_obj) || PyStr_Check(set_obj)) { + if (PyBytes_Check(set_obj) || PyUnicode_Check(set_obj)) { me->num_values = 1; me->values = talloc_array(me, struct ldb_val, me->num_values); if (PyBytes_Check(set_obj)) { @@ -2897,7 +2897,7 @@ static struct ldb_message_element *PyObject_AsMessageElement( return NULL; } msg = _msg; - } else if (PyStr_Check(obj)) { + } else if (PyUnicode_Check(obj)) { msg = PyStr_AsUTF8AndSize(obj, &size); if (msg == NULL) { talloc_free(me); @@ -3069,7 +3069,7 @@ static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyOb if (py_elements != NULL) { Py_ssize_t i; - if (PyBytes_Check(py_elements) || PyStr_Check(py_elements)) { + if (PyBytes_Check(py_elements) || PyUnicode_Check(py_elements)) { char *_msg = NULL; el->num_values = 1; el->values = talloc_array(el, struct ldb_val, 1); @@ -3110,7 +3110,7 @@ static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyOb char *_msg = NULL; result = PyBytes_AsStringAndSize(item, &_msg, &size); msg = _msg; - } else if (PyStr_Check(item)) { + } else if (PyUnicode_Check(item)) { msg = PyStr_AsUTF8AndSize(item, &size); result = (msg == NULL) ? -1 : 0; } else { diff --git a/lib/ldb/pyldb_util.c b/lib/ldb/pyldb_util.c index 3bda1dbc2a8..46ee4034122 100644 --- a/lib/ldb/pyldb_util.c +++ b/lib/ldb/pyldb_util.c @@ -70,7 +70,7 @@ bool pyldb_Object_AsDn(TALLOC_CTX *mem_ctx, PyObject *object, struct ldb_dn *odn; PyTypeObject *PyLdb_Dn_Type; - if (ldb_ctx != NULL && PyStr_Check(object)) { + if (ldb_ctx != NULL && (PyStr_Check(object) || PyUnicode_Check(object))) { odn = ldb_dn_new(mem_ctx, ldb_ctx, PyStr_AsUTF8(object)); *dn = odn; return true;