lib/ldb: Additionally accept unicode as string param in Py2
authorNoel Power <noel.power@suse.com>
Thu, 12 Apr 2018 13:46:59 +0000 (14:46 +0100)
committerNoel Power <npower@samba.org>
Mon, 30 Apr 2018 13:43:19 +0000 (15:43 +0200)
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 <noel.power@suse.com>
Reviewed-by: Alexander Bokovoy <ab@samba.org>
lib/ldb/pyldb.c
lib/ldb/pyldb_util.c

index 67ecafbc42013ae10e73f25c0e238d9d6747915f..110ec8e60ada3c0bf2c140be9123e3ef765233b0 100644 (file)
@@ -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 {
index 3bda1dbc2a86a77fe223310420feec68089d8ec5..46ee40341228a0dd0f5b3b70d5a7678b84c0581a 100644 (file)
@@ -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;