From c53b6b5a7377892c4302b55f5422b81fbcb77be6 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Sat, 20 Jun 2020 07:59:49 +0200 Subject: [PATCH] python: return NULL if set_list_of_strings fails (#44287) --- bindings/python/lang.py | 4 ++-- bindings/python/tests/binding_tests.py | 8 ++++++-- bindings/python/wrapper_top.c | 13 +++++++++---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/bindings/python/lang.py b/bindings/python/lang.py index 075a3274..50fdfb03 100644 --- a/bindings/python/lang.py +++ b/bindings/python/lang.py @@ -821,7 +821,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): @@ -984,7 +984,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 feca4694..9a801be2 100755 --- a/bindings/python/tests/binding_tests.py +++ b/bindings/python/tests/binding_tests.py @@ -336,9 +336,13 @@ class BindingTestCase(unittest.TestCase): assert isinstance(str(cm.exception), str) with self.assertRaises(lasso.Error) as cm: lasso.Error.raise_on_rc(lasso._lasso.XML_ERROR_SCHEMA_INVALID_FRAGMENT) - self.assertEqual(str(cm.exception), '') - + self.assertEqual(str(cm.exception), + '') + 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] bindingSuite = unittest.makeSuite(BindingTestCase, 'test') diff --git a/bindings/python/wrapper_top.c b/bindings/python/wrapper_top.c index c1dac5ee..6214148a 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 +static 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