From 4641992fa5f01b0d56f667c41f4c346cc3fe4592 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Sat, 20 Jun 2020 07:59:49 +0200 Subject: [PATCH 14/21] python: return NULL if set_list_of_strings fails (#44287) --- bindings/python/lang.py | 4 ++-- bindings/python/tests/binding_tests.py | 4 ++++ bindings/python/wrapper_top.c | 13 +++++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/bindings/python/lang.py b/bindings/python/lang.py index 376a0e41..22b1afae 100644 --- a/bindings/python/lang.py +++ b/bindings/python/lang.py @@ -818,7 +818,7 @@ register_constants(PyObject *d) elif is_glist(m): el_type = element_type(m) if is_cstring(el_type): - print_(' set_list_of_strings(&this->%s, cvt_value);' % name, file=fd) + print_(' RETURN_IF_FAIL(set_list_of_strings(&this->%s, cvt_value));' % name, file=fd) elif is_xml_node(el_type): print_(' set_list_of_xml_nodes(&this->%s, cvt_value);' % name, file=fd) elif is_object(el_type): @@ -981,7 +981,7 @@ register_constants(PyObject *d) if is_list(arg): qualifier = element_type(arg) if is_cstring(qualifier): - print_(' set_list_of_strings(&%s, cvt_%s);' % (arg[1], arg[1]), file=fd) + print_(' EXIT_IF_FAIL(set_list_of_strings(&%s, cvt_%s));' % (arg[1], arg[1]), file=fd) elif is_xml_node(qualifier): print_(' set_list_of_xml_nodes(&%s, cvt_%s);' % (arg[1], arg[1]), file=fd) elif isinstance(qualifier, str) and qualifier.startswith('Lasso'): diff --git a/bindings/python/tests/binding_tests.py b/bindings/python/tests/binding_tests.py index 936e8b3b..5f71587d 100755 --- a/bindings/python/tests/binding_tests.py +++ b/bindings/python/tests/binding_tests.py @@ -329,6 +329,10 @@ class BindingTestCase(unittest.TestCase): node.sessionIndexes = () assert node.sessionIndexes == (), node.sessionIndexes + def test_set_list_of_strings(self): + node = lasso.Samlp2RequestedAuthnContext() + with self.assertRaises(TypeError, msg='value should be a tuple of strings'): + node.authnContextClassRef = [None] diff --git a/bindings/python/wrapper_top.c b/bindings/python/wrapper_top.c index aa5f60a3..7d26dd8c 100644 --- a/bindings/python/wrapper_top.c +++ b/bindings/python/wrapper_top.c @@ -91,7 +91,7 @@ G_GNUC_UNUSED static PyObject* get_dict_from_hashtable_of_strings(GHashTable *va G_GNUC_UNUSED static PyObject* PyGObjectPtr_New(GObject *obj); G_GNUC_UNUSED static int set_hashtable_of_pygobject(GHashTable *a_hash, PyObject *dict); G_GNUC_UNUSED static int set_hashtable_of_strings(GHashTable *a_hash, PyObject *dict); -G_GNUC_UNUSED static void set_list_of_strings(GList **a_list, PyObject *seq); +G_GNUC_UNUSED static int set_list_of_strings(GList **a_list, PyObject *seq); G_GNUC_UNUSED static void set_list_of_xml_nodes(GList **a_list, PyObject *seq); G_GNUC_UNUSED static void set_list_of_pygobject(GList **a_list, PyObject *seq); G_GNUC_UNUSED static PyObject *get_list_of_strings(const GList *a_list); @@ -324,12 +324,16 @@ failure: /** Set the GList* pointer, pointed by a_list, to a pointer on a new GList * created by converting the python seq into a GList of char*. */ -static void +int set_list_of_strings(GList **a_list, PyObject *seq) { GList *list = NULL; int l = 0,i; - lasso_return_if_fail(valid_seq(seq)); + if (! valid_seq(seq)) { + PyErr_SetString(PyExc_TypeError, + "value should be a tuple of strings"); + return 0; + } if (seq != Py_None) { l = PySequence_Length(seq); } @@ -348,9 +352,10 @@ set_list_of_strings(GList **a_list, PyObject *seq) { } free_list(a_list, (GFunc)g_free); *a_list = list; - return; + return 1; failure: free_list(&list, (GFunc)g_free); + return 0; } /** Set the GList* pointer, pointed by a_list, to a pointer on a new GList -- 2.26.2